#!/bin/sh
#set -x

PLATFORM=`uname`
VERBOSE=0
OPROG=../fundisp
NPROG=../funimage
STDIN=/dev/null
OFILE=${HOME}/data/snr.fits
NFILE=${HOME}/data/bzero.fits
X=504
Y=512

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;;
  -f)
    shift
    OFILE=$1
    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=../funimage.pure
    shift
    continue;;
  -v)
    VERBOSE=1
    shift
    continue;;
  -V)
    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\""
  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"
      cat $STDIN | $NPROG ${NFILE}"[${NM}$ARG3]" | sed -e '1,2d;s/[0-9]*://g' > funimage2.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}]" | sed -e '1,2d;s/[0-9]*://g;s/ +/ /g' | awk '{for(i=1;i<=NF;i++){printf("%.2f ",$i)}printf("\n");}'> tfunimage1.out
      if test $? -ne 0; then
        CMP=no
        XPROG="$OPROG"
      fi
      cat $STDIN | $NPROG ${NFILE}"[${B}${NM}$ARG3${B2}]" stdout | $OPROG stdin| sed -e '1,2d;s/[0-9]*://g;s/ +/ /g' | awk '{for(i=1;i<=NF;i++){printf("%.2f ",$i)}printf("\n");}' > tfunimage2.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" < tfunimage1.out > funimage1.out
          sed "/^ *$X      $Y.*$/d" < tfunimage2.out > funimage2.out
        else
          mv tfunimage1.out funimage1.out
          mv tfunimage2.out funimage2.out
        fi
        diff funimage1.out funimage2.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 "***** funimage1.out *****"
    cat funimage1.out
  fi
  if [ $VERBOSE = 2 ]; then
    echo "***** funimage1.out *****"
    cat funimage1.out
    echo "***** funimage2.out *****"
    cat funimage2.out
  fi
  rm -f err.log
done

rm -f funimage1.out funimage2.out tfunimage1.out tfunimage2.out
