#!/bin/sh
#set -x

PLATFORM=`uname`
VERBOSE=0
OPROG=funmerge
NPROG=../funmerge
STDIN=/dev/null
DIR=../txt/
FILES="mif1.fits mif2.fits mif3.fits mif4.fits"
LIST="@mif.lst"

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;;
  -n)
    shift
    NPROG=$1
    shift
    continue;;
  -o)
    shift
    OPROG=$1
    shift
    continue;;
  -P)
    DO1=YES
    NPROG=../funmerge.pure
    shift
    continue;;
  -v)
    VERBOSE=1
    shift
    continue;;
  -V)
    VERBOSE=2
    shift
    continue;;
  *)
    shift
    continue;;
  esac
done

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

IFS="	"
while read ARG1 ARG2 ARG3 ARG4; do
  if [ x"$ARG1" = x ]; then
    continue
  fi
  if [ x`$ECHO $ARG1 | sed 's/^#.*/YES/g'` = x"YES" ]; then
    continue
  fi
  CMD=$ARG1

  eval "ARG3=\"$ARG3\""
  eval "ARG4=\"$ARG4\""

  case $CMD in
  EXIT)
    exit
    ;;
  EXACT)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG2"
      eval "$NPROG $ARG2 $ARG3 stdout" | fundisp stdin > funmerge2.out
    else
      $ECHO "${M}EXACT: $ARG2 $ARG3 ... $ARG2 $ARG4"
      CMP=yes
      XPROG=""
      eval "$OPROG $ARG2 $ARG3 stdout" | fundisp stdin > tfunmerge1.out
      if test $? -ne 0; then
        CMP=no
        XPROG="$OPROG"
      fi

      eval "$NPROG $ARG2 $ARG4 stdout" | fundisp stdin > tfunmerge2.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" < tfunmerge1.out > funmerge1.out
          sed "/^ *$X      $Y.*$/d" < tfunmerge2.out > funmerge2.out
        else
          mv tfunmerge1.out funmerge1.out
          mv tfunmerge2.out funmerge2.out
        fi
        diff funmerge1.out funmerge2.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 "***** funmerge1.out *****"
    cat funmerge1.out
  fi
  if [ $VERBOSE = 2 ]; then
    $ECHO "***** funmerge1.out *****"
    cat funmerge1.out
    $ECHO "***** funmerge2.out *****"
    cat funmerge2.out
  fi
  rm -f err.log
done

rm -f funmerge1.out funmerge2.out tfunmerge1.out tfunmerge2.out
