#!/bin/sh
#set -x

VERBOSE=0
OPROG=funcalc
NPROG=../funcalc
R="cir(511,512,1)"
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

while [ x"$1" != x ]; do
  case $1 in
  -1)
    DO1=YES
    shift
    continue;;
  -f2)
    shift
    OFILE=$1
    shift
    NFILE=$1
    shift
    continue;;
  -n)
    shift
    NPROG=$1
    shift
    continue;;
  -o)
    shift
    OPROG=$1
    shift
    continue;;
  -P)
    DO1=YES
    NPROG=../funcalc.pure
    shift
    continue;;
  -V)
    DO1=YES
    NPROG="/usr/bin/valgrind --tool=memcheck  --leak-check=yes --show-reachable=yes --trace-children=yes ../funcalc"
    shift
    continue;;
  -r)
    shift
    R=$1
    shift
    continue;;
  -v)
    VERBOSE=1
    shift
    continue;;
  -v2)
    VERBOSE=2
    shift
    continue;;
  *)
    shift
    continue;;
  esac
done

OFILE="${OFILE}[${R}]"
NFILE="${NFILE}[${R}]"
$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}
  if [ x"$ARG2" = x1 ]; then
    OUT=FALSE
  else
    OUT=TRUE
  fi	
  EXPR=${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
    ;;
  REGION)
    R=$ARG2
    ;;
  EXIT)
    exit
    ;;
  EXACT|SUM|REV)
    if [ x"$DO1" = xYES ]; then
      $ECHO "DO1: $EXPR"
      if [ x"$OUT" = xFALSE ]; then
        $NPROG -l "-L.." -e "${EXPR}" ${NFILE}  > funcalc1.out
      else
        $NPROG -l "-L.." -e "${EXPR}" ${NFILE} stdout | fundisp stdin  > funcalc1.out
      fi
    else
      $ECHO "EXACT: $EXPR"
      CMP=yes
      XPROG=""
      if [ x"$OUT" = xFALSE ]; then
        $OPROG -e ${EXPR} ${OFILE} > tfuncalc1.out 2>err.log
      else
        $OPROG -e ${EXPR} ${OFILE} stdout | fundisp stdin > tfuncalc1.out 2>err.log
      fi
      if test $? -ne 0; then
        CMP=no
        XPROG="$OPROG"
      fi
      if [ x"$OUT" = xFALSE ]; then
        $NPROG -l "-L.." -e ${EXPR} ${NFILE} > tfuncalc2.out
      else
        $NPROG -l "-L.." -e ${EXPR} ${NFILE} stdout | fundisp stdin > tfuncalc2.out
      fi
      if test $? -ne 0; then
        CMP=no
        XPROG="${XPROG:-""}${XPROG:+" "}$NPROG"
      fi
      if [ x"$CMP" = xyes ]; then
        mv tfuncalc1.out funcalc1.out
        mv tfuncalc2.out funcalc2.out
        diff funcalc1.out funcalc2.out
      fi
    fi
    $ECHO " "
    ;;
  *)
    $ECHO "ERROR: unknown command: $CMD"
    exit 1
    ;;
  esac
  if [ $VERBOSE = 1 ]; then
    $ECHO "***** funcalc1.out *****"
    cat funcalc1.out
  fi
  if [ $VERBOSE = 2 ]; then
    $ECHO "***** funcalc1.out *****"
    cat funcalc1.out
    $ECHO "***** funcalc2.out *****"
    cat funcalc2.out
  fi
  rm -f err.log
done

rm -f funcalc1.out funcalc2.out tfuncalc1.out tfuncalc2.out
