#!/bin/sh
#
# Ricardo Goncalo - diff two columns of numbers
# 12/2/2009
#
# get options
print=0
getopts hp option
case $option in
    h)     echo " "
           echo "Diff 2 numerical columns"
           echo " "
           echo "usage : collDiff.sh [filename] [col label] [col1] [col2]"
           echo "or    : collDiff.sh -p [filename]"
           echo " "
           exit 0
    ;;
    p)     echo " "
           echo "Printing 10 first lines in input file"
	   shift
	   print=1
    ;;

esac

# if p option set, print out 10 1st lines and exit
# error protection
if [ $print == 1 ]
then 
    if [ $# -ne 1 ]
    then 
	echo "Error: wrong number of arguments; use castorcopy.sh -h"
	exit 1
    fi
    if [ ! -e $1 ]
    then 
	echo "Error: file $1 doesn't exist"
	exit 1
    fi
    # get parameters
    file=$1
    # print file header
    index=0
    while read line
    do
	if [ $index -le 10 ]
	then 
	    echo $line
	    (( index += 1 ))
	fi
    done < $file
    exit 0
fi

# print differences between columns, including a label column
# error protection
if [ $# -ne 5 ]
then 
    echo "Error: not enough arguments; use castorcopy.sh -h"
    exit 1
fi
if [ ! -e $1 ]
then 
    echo "Error: file $1 doesn't exist"
    exit 1
fi

# get parameters
file=$1
label=$2
col1=$3
col2=$4
delta=$5

# print out differences if >10%
echo "(c1-c2)/<c1,c2>   c1             c2             label"
while read line
do
    if [[ $line != \#* ]]
	then
	echo $line | awk '{ if ( ($'$col1'+$'$col2') > 0 &&  (sqrt( ($'$col1' - $'$col2')^2 )*2 / ($'$col1'+$'$col2') ) > '$delta' && $'$col1' > 0 && $'$col2' > 0) printf "%i%s \t\t %i \t\t %i \t\t %s \n", ( 200 * ($'$col1'-$'$col2') / ( $'$col1'+$'$col2') ), "%", $'$col1', $'$col2', $'$label' }'
	echo $line | awk '{ if ( ($'$col1'+$'$col2') > 0 &&  (sqrt( ($'$col1' - $'$col2')^2 )*2 / ($'$col1'+$'$col2') ) > '$delta' && ($'$col1' <= 0 || $'$col2' <= 0)) printf "%s \t\t %i \t\t %i \t\t %s \n", "---", $'$col1', $'$col2', $'$label' }'
    fi
done < $file
echo $diff