#!/bin/bash

if [ $1 ]; then

source "$(dirname "$0")/../../../scripts/t2utils.sh"

sort -k1,1 -k2,2 $1 | awk -F"\t" -M 'BEGIN{i = 0;G[0] = 0;t = 0;l=0;}
 !/^#/{ 
    OFS = "\t";
    c = and(strtonum($1),0xffffffffffff0000);
    e = and(strtonum($1),0xffff);
    if (c != a) {
        if (j > 1) {
	    for (k = 1; k <= j; k++) {
		split(A[k], B, "\t");
		if (e) print B[1], B[2], B[3], or(lshift(b,1), B[4]), B[5], B[6];
		else {
		    print A[k];
		}
	    }
	} else if (j == 1) print A[1];
	j = 0;
	a = strtonum(c);
    }
    i++;
    if ($2 == 48) t = G[l]; 
    else if (and(strtonum($3), 1) == 0) {t = G[l];G[++l] = i;}
    else t = G[--l];
    A[++j] = $1"\t"$2"\t"t"\t"$3"\t"$4"\t"$5;
    b = i;
 } END{
    if (j > 1) {
       for (k = 1; k <= j; k++) {
          split(A[k], B, "\t");
          print B[1], B[2], B[3], or(lshift(b,1), B[4]), B[5], B[6];
       }
    } else if (j == 1) print A[1];
 }' > "$SHOME/../macEthlbl_HLP.txt"

"$SHOME/mbm" "$SHOME/../macEthlbl_HLP.txt" "$SHOME/../macEthlbl_HLP.bin"

else
echo "./uconv mactypefile"
fi
