#!/bin/sh

EXE=awk
N=1
FILE=-
DOHEAD=0
DORDB=0
XHEAD=""
SEP=" "

# process args
while [ x"$1" != x ]; do
    case $1 in
	-c) shift
	    COLS=$1
            shift
            continue;;
        -help|--help) 
	    echo "usage:"
	    echo " "
	    echo "  funtbl [switches] input_file"
	    echo "  funtbl [switches] < input_file"
	    echo " "
	    echo "switches (optional):"
	    echo " "
	    echo "  -c \"col1 ...\"    # output columns numbers (def: all)"
	    echo "  -h               # prepend column names as a header"
	    echo "  -H prefix        # prepend prefix and column names"
	    echo "  -n num           # extract the nth data table (def: 1)"
	    echo "  -p prog          # awk program (def: host-specific)"
	    echo "  -s sep           # output separator (def: space)"
	    echo "  -T               # output rdb table format"
	    echo " "
	    echo "  -help            # print this message"
	    echo " "
	    echo "examples:"
	    echo " "
	    echo "  funcnts -rs ... | funtbl -c \"1 3\" -n 4 -s \"\t\""
	    echo "  funtbl -c \"1 3\" -n 4 -h -s \"\t\" foo.out"
	    echo "  funtbl -c \"1 3\" -n 4 -T foo.out"
	    echo " "
	    echo "All 3 will output the first and third columns of the fourth"
	    echo "table, with the columns separated by a tab. The second form"
	    echo "will output a single line header of column names. The third"
	    echo "form will output 2-line rdb header."
	    echo " "
            exit 0;;
        -h) DOHEAD=1
	    XHEAD="# "
            shift
            continue;;
        -H) DOHEAD=1
	    shift
	    XHEAD=$1
            shift
            continue;;
        -n) shift
	    N=$1
            shift
            continue;;
        -p) shift
	    EXE=$1
            shift
            continue;;
        -s) shift
	    SEP=$1
            shift
            continue;;
        -T) DORDB=1
	    DOHEAD=1
	    SEP="\t"
            shift
            continue;;
	*)  FILE=$1
            shift
            continue;;
    esac
done

cat $FILE |	\
$EXE '

BEGIN{
    I=0
    STATE=0
    N='"$N"'
    COLS="'"$COLS"'"
    DOHEAD='"$DOHEAD"'
    DORDB='"$DORDB"'
    SEP="'"$SEP"'"
    XHEAD="'"$XHEAD"'"
    if( COLS == "" ){
        NCOL=0
    }
    else{
	NCOL=split(COLS,COLNAMES);
    }
}

# state 0: looking for nth header
STATE==0{
    last = cur
    cur = $0
    dashes=0
    for(j=1; j<=NF; j++){
	if( $j ~ /--*/ ){
	    dashes++;
	}
        else{
            break;
	}
    }
    if( (dashes >= 1) && (dashes == NF) ){
        I=I+1
        if( I==N ){
            STATE=1    
	    LINES=0
	    if( DOHEAD == 1 ){
	        split(last, HEADER)
	        split($0, DASHES)
	    }
        }
    }
}

# look for blank line to end row output
$0 ~ /^$/{
    if( STATE == 1 ){
        STATE=2
    }
}

# state 1: output this line of data
STATE==1{
    # output header, if necessary
    if( LINES == 0 ){
	if( DOHEAD == 1 ){
 	    # output as comment if not rdb
            if( DORDB != 1 ){
	        printf("%s", XHEAD)
	    }
	    # specific columns
	    if( COLS == "" ){
	        for(i=1; i<=NF; i++){
		    printf("%s", HEADER[i])
		    if( i != NF )
		        printf("%s", SEP)
	        }    
                printf("\n")
                if( DORDB == 1 ){
	            for(i=1; i<=NF; i++){
		        printf("%s", DASHES[i])
		        if( i != NF )
		            printf("%s", SEP)
		    }
                    printf("\n")
	        }    
	    }
 	    # all columns
	    else{
	        for(i=1; i<=NCOL; i++){
		    printf("%s", HEADER[COLNAMES[i]])
		    if( i != NCOL ){
		        printf("%s", SEP)
		    }
	        }    
                printf("\n")
                if( DORDB == 1 ){
	            for(i=1; i<=NCOL; i++){
		        printf("%s", DASHES[COLNAMES[i]])
  		        if( i != NCOL ){
		            printf("%s", SEP)
		        }
		    }
                    printf("\n")
                }
	    }    
	}
    }
    else{
	if( COLS == "" ){
	    for(i=1; i<=NF; i++){
		printf("%s", $i)
		if( i != NF )
		    printf("%s", SEP)
	    }    
            printf("\n")
	}
	else{
	    split($0, ARGS) 		
	    for(i=1; i<=NCOL; i++){
		printf("%s", ARGS[COLNAMES[i]])
		if( i != NCOL )
		    printf("%s", SEP)
	    }    
            printf("\n")
	}
    }
    LINES=LINES+1
}
'

