#!/bin/sh
# set -x

PLATFORM=`uname`
VERBOSE=0
TEXT=0
OPROG=funhead
NPROG=../funhead
STDIN=/dev/null
OFILE=${HOME}/data/snr.ev
NFILE=${HOME}/data/snr.ev.gz
X=504
Y=512

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

while [ x"$1" != x ]; do
  case $1 in
  -1)
    DO1=YES
    shift
    continue;;
  -b)
    shift
    B="$1("
    B2=")"
    shift
    continue;;
  -c)
    shift
    C="$1\;"
    shift
    continue;;
  -e)
    OM="field;-"
    NM="-"
    shift
    continue;;
  -f2)
    shift
    OFILE=$1
    shift
    NFILE=$1
    shift
    continue;;
  -i)
    DOINC=YES
    shift
    continue;;
  -n)
    shift
    NPROG=$1
    shift
    continue;;
  -o)
    shift
    OPROG=$1
    shift
    continue;;
  -p)
    shift
    STDIN=$NFILE
    if [ x`$ECHO $STDIN | awk -F. '{print $NF}'` = x"gz" ]; then
      NFILE=stdin.gz
    else
      NFILE=stdin
    fi
    shift
    continue;;
  -P)
    DO1=YES
    NPROG=../funhead.pure
    shift
    continue;;
  -V)
    DO1=YES
    NPROG="/usr/bin/valgrind --tool=memcheck  --leak-check=yes --show-reachable=yes --trace-children=yes ../funhead"
    shift
    continue;;
  -t)
    TEXT=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;;
  -z)
    ZBUG=true
    shift
    continue;;
  *)
    shift
    continue;;
  esac
done

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} "FILE="${OFILE}
$ECHO "NPROG="${NPROG} "FILE="${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
    $ECHO OPROG="${OPROG}"
    ;;
  NPROG)
    NPROG=$ARG2
    $ECHO NPROG="${NPROG}"
    ;;
  OFILE)
    OFILE=$ARG2
    $ECHO OFILE="${OFILE}"
    ;;
  NFILE)
    NFILE=$ARG2
    $ECHO NFILE="${NFILE}"
    ;;
  EXCLUDE)
    OM="field;-"
    NM="-"
    ;;
  EXIT)
    exit
    ;;
  EXACT|SUM|REV)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG3"
      $NPROG ${NFILE}"[${NM}$ARG3]"  < $STDIN | egrep -v OBS_ID | \
      sed 's/  */ /g' | sort | \
      awk -F/ '{print $1}' | awk -F= '$2 ~ /'\''/{print $1,$2; next}{printf("%s %f\n", $1, $2)}' >  funhead2.out
    else
      if [ x"$DOINC" = xYES ]; then
        $ECHO "$ARG3" > arg3.inc
        $ECHO "${M}EXACT: ${B}$ARG2${B2} ... ${B}@include($ARG3)${B2}"
        ARG3="@arg3.inc"
      else
        $ECHO "${M}EXACT: ${B}$ARG2 ... ${B}$ARG3"
      fi
      CMP=yes
      XPROG=""
      $OPROG ${OFILE}"[${B}${OM}$ARG2${B2}]" | egrep -v OBS_ID | \
      sed 's/  */ /g' | sort | \
      awk -F/ '{print $1}' | awk -F= '$2 ~ /'\''/{print $1,$2; next}{printf("%s %f\n", $1, $2)}'  > tfunhead1.out
      if test $? -ne 0; then
        CMP=no
        XPROG="$OPROG"
      fi
      if [ x$TEXT = x1 ]; then
	  ARG3=`$ECHO "$ARG3-1" | bc`
      fi
      $NPROG ${NFILE}"[${B}${NM}$ARG3${B2}]" < $STDIN | egrep -v OBS_ID | \
      sed 's/  */ /g' | sort | \
      awk -F/ '{print $1}' | awk -F= '$2 ~ /'\''/{print $1,$2; next}{printf("%s %f\n", $1, $2)}' > tfunhead2.out
      if test $? -ne 0; then
        CMP=no
        XPROG="${XPROG:-}${XPROG:+' '}$NPROG"
      fi
      if [ x"$CMP" = xyes ]; then
        if [ x"$ZBUG" = xtrue ]; then
          sed "/^ *$X      $Y.*$/d" < tfunhead1.out > funhead1.out
          sed "/^ *$X      $Y.*$/d" < tfunhead2.out > funhead2.out
        else
          mv tfunhead1.out funhead1.out
          mv tfunhead2.out funhead2.out
        fi
        diff funhead1.out funhead2.out
      else
        $ECHO "skipping compare due to errors in $XPROG"
      fi
    fi
    $ECHO " "
    ;;
  *)
    $ECHO "ERROR: unknown command: $CMD"
    exit 1
    ;;
  esac
  if [ $VERBOSE = 1 ]; then
    $ECHO "***** funhead1.out *****"
    cat funhead1.out
  fi
  if [ $VERBOSE = 2 ]; then
    $ECHO "***** funhead1.out *****"
    cat funhead1.out
    $ECHO "***** funhead2.out *****"
    cat funhead2.out
  fi
  rm -f err.log
done

rm -f funhead1.out funhead2.out tfunhead1.out tfunhead2.out