#!/bin/sh
# set -x

PLATFORM=`uname`
VERBOSE=0
SW="-r"
X=504
Y=512
STDIN=/dev/null
OPROG=funcnts
NPROG=../funcnts
OFILE=${HOME}/data/snr.ev
NFILE=${HOME}/data/snr.ev.gz

if [ -r /usr/ucb/echo ]; then
  ECHO=/usr/ucb/echo
else
  ECHO=echo
fi

EAWK='start==1&&NF>0{tpix += $2; tarea +=$3; print $0};/^---- ------------ ---------$/{start=1}END{print tpix,tarea}'

SAWK='start==1&&NF>0{tpix += $2; tarea +=$3};/^---- ------------ ---------$/{start=1}END{print tpix,tarea}'

FAWK='{tpix = $1};END{print tpix}'

while [ x"$1" != x ]; do
  case $1 in
  -1)
    DO1=YES
    shift
    continue;;
  -b)
    shift
    B="$1"
    shift
    continue;;
  -c)
    shift
    C="$1;"
    shift
    continue;;
  -E)
    DOEXACT=YES
    shift
    continue;;
  -e)
    FIELD=`${OPROG} ${OFILE} "field" | awk "$SAWK" | awk "$FAWK"`
    $ECHO "FIELD: $FIELD"
    $ECHO " "
    M="-"
    shift
    continue;;
  -f1)
    shift
    OFILE=$1
    NFILE=$1
    shift
    continue;;
  -f2)
    shift
    OFILE=$1
    shift
    NFILE=$1
    shift
    continue;;
  -n)
    shift
    NPROG=$1
    shift
    continue;;
  -o)
    shift
    OPROG=$1
    shift
    continue;;
  -p)
    shift
    STDIN=$1
    if [ x`$ECHO $STDIN | awk -F. '{print $NF}'` = x"gz" ]; then
      NFILE=stdin.gz
    else
      NFILE=stdin
    fi
    continue;;
  -P)
    PURE=YES
    NPROG=../funcnts.pure
    shift
    continue;;
  -V)
    DO1=YES
    NPROG="/usr/bin/valgrind --tool=memcheck  --leak-check=yes --show-reachable=yes --trace-children=yes ../funcnts"
    shift
    continue;;
  -s)
    shift
    SW=$1
    shift
    continue;;
  -v)
    VERBOSE=1
    shift
    continue;;
  -v2)
    VERBOSE=2
    shift
    continue;;
  -x)
    shift
    X=$1
    shift
    continue;;
  -y)
    shift
    Y=$1
    shift
    continue;;
  *)
    shift
    continue;;
  esac
done

if [ x"$B" != x ]; then
  OFILE=${OFILE}"[$B]"
  NFILE=${NFILE}"[$B]"
fi

X2=`$ECHO "$X + 10" | bc`
Y2=`$ECHO "$Y + 10" | bc`
X3=`$ECHO "$X + 20" | bc`
Y3=$Y
X4=`$ECHO "$X + 10" | bc`
Y4=`$ECHO "$Y - 10" | bc`

$ECHO "OPROG="${OPROG} "OFILE="${OFILE}
$ECHO "NPROG="${NPROG} "NFILE="${NFILE}

IFS="	"
while read ARG1 ARG2 ARG3; do
  if [ x"$ARG1" = x ]; then
    continue
  fi
  if [ x`$ECHO $ARG1 | sed 's/^#.*/YES/g'` = x"YES" ]; then
    continue
  fi
  CMD=$ARG1
  eval "ARG2=\"${C}$ARG2\""
  eval "ARG3=\"${C}$ARG3\""
  if [ x$CMD = xZIEXACT ]; then
    if [ x$FILTER_PAINT = xtrue ]; then
      continue
    else
      CMD=EXACT
    fi
  fi
  if [ x$CMD = xZEXACT ]; then
    if [ x$FILTER_PAINT = xtrue ]; then
      CMD=SUM
    else
      CMD=EXACT
    fi
  fi
  case $CMD in
  OPROG)
    OPROG=$ARG2
    ;;
  NPROG)
    NPROG=$ARG2
    ;;
  OFILE)
    OFILE=$ARG2
    ;;
  NFILE)
    NFILE=$ARG2
    ;;
  EXCLUDE)
    FIELD=`$OPROG ${OFILE} "field" | awk "$SAWK" | awk "$FAWK"`
    $ECHO "FIELD: $FIELD"
    $ECHO " "
    M="-"
    ;;
  EXIT)
    exit
    ;;
  EXACT)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG3"
      cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" > funcnts2.out
    else
      $ECHO "${M}EXACT: $ARG2 ... $ARG3"
      if [ x"$DOEXACT" = xYES ]; then
        CMP=yes
        XPROG=""
        $OPROG ${SW} ${OFILE} "$ARG2" > funcnts1.out 2>err.log
        if test $? -ne 0; then
          CMP=no
          XPROG="$OPROG"
	  cat err.log
        fi
        cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" > funcnts2.out
        if test $? -ne 0; then
          CMP=no
          XPROG="${XPROG:-''}${XPROG:+' '}$NPROG"
        fi
      else
        CMP=yes
        XPROG=""
        $OPROG ${SW} ${OFILE} "$ARG2" | awk $EAWK > funcnts1.out 2>err.log
        if test $? -ne 0; then
          CMP=no
          XPROG="$OPROG"
	  cat err.log
        fi
        cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" |awk $EAWK >funcnts2.out
        if test $? -ne 0; then
          CMP=no
          XPROG="${XPROG:-''}${XPROG:+' '}$NPROG"
        fi
      fi
     if [ x"$CMP" = xyes ]; then
        if [ x$M = x ]; then
          diff funcnts1.out funcnts2.out
        else
          V1=`cat funcnts1.out | awk $FAWK`
          V2=`cat funcnts2.out | awk $FAWK`
          V2=`$ECHO "$FIELD-$V2" | bc`
          if [ $V1 != $V2 ]; then
              $ECHO "V1=$V1 V2=$V2"
          fi
        fi
      fi
    fi
    $ECHO " "
    ;;
  REV)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG3"
      cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" > funcnts2.out
    else
      $ECHO "${M}REV: $ARG2 ... $ARG3"
      $OPROG ${SW} ${OFILE} "$ARG2" | awk $EAWK > tfuncnts1.out
      if test $? -ne 0; then
        $ECHO "skipping $NPROG due to error"
      else
        cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" | awk $EAWK > tfuncnts2.out
        if [ x$M = x ]; then
          awk 'NF==3{print $0}' < tfuncnts1.out | sort -r | awk '{print $2,$3}' > funcnts1.out
          awk 'NF==3{print $2,$3}' < tfuncnts2.out > funcnts2.out
          diff funcnts1.out funcnts2.out
        else
          V1=`cat funcnts1.out | awk $FAWK`
          V2=`cat funcnts2.out | awk $FAWK`
          V2=`$ECHO "$FIELD-$V2" | bc`
          if [ $V1 != $V2 ]; then
              $ECHO "V1=$V1 V2=$V2"
          fi
        fi
      fi 
   fi
    $ECHO " "
    ;;
  SUM)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG3"
      cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" > funcnts2.out
    else
      $ECHO "${M}SUM:   $ARG2 ... $ARG3"
      $OPROG ${SW} ${OFILE} "$ARG2" | awk $SAWK > funcnts1.out
      if test $? -ne 0; then
        $ECHO "skipping $NPROG due to error"
      else
        cat $STDIN | $NPROG ${SW} ${NFILE} "${M}$ARG3" | awk $SAWK > funcnts2.out
        if [ x$M = x ]; then
          diff funcnts1.out funcnts2.out
        else
          V1=`cat funcnts1.out | awk $FAWK`
          V2=`cat funcnts2.out | awk $FAWK`
          V2=`$ECHO "$FIELD-$V2" | bc`
          if [ $V1 != $V2 ]; then
              $ECHO "V1=$V1 V2=$V2"
          fi
        fi
      fi
    fi
    $ECHO " "
    ;;
  *)
    $ECHO "ERROR: unknown command: $CMD"
    exit 1
    ;;
  esac
  if [ $VERBOSE = 1 ]; then
    $ECHO "***** funcnts1.out *****"
    cat funcnts1.out
  fi
  if [ $VERBOSE = 2 ]; then
    $ECHO "***** funcnts1.out *****"
    cat funcnts1.out
    $ECHO "***** funcnts2.out *****"
    cat funcnts2.out
  fi
  rm -f err.log
done

rm -f funcnts1.out funcnts2.out tfuncnts1.out tfuncnts2.out
