#!/bin/sh
#set -x

PLATFORM=`uname`
VERBOSE=0
OPROG=fundisp
NPROG=../fundisp
STDIN=/dev/null
DIR=../txt/

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)
    shift
    STDIN=$1
    if [ x`$ECHO $STDIN | awk -F. '{print $NF}'` = x"gz" ]; then
      NFILE=stdin.gz
    else
      NFILE=stdin
    fi
    shift
    continue;;
  -P)
    DO1=YES
    NPROG=../fundisp.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; do
  if [ x"$ARG1" = x ]; then
    continue
  fi
  if [ x`$ECHO $ARG1 | sed 's/^#.*/YES/g'` = x"YES" ]; then
    continue
  fi
  CMD=$ARG1
  ARG2=${DIR}${ARG2}
  ARG3=${DIR}${ARG3}

  case $CMD in
  OPROG)
    OPROG=$ARG2
    $ECHO OPROG="${OPROG}"
    ;;
  NPROG)
    NPROG=$ARG2
    $ECHO NPROG="${NPROG}"
    ;;
  EXIT)
    exit
    ;;
  EXACT|SUM|REV)
    if [ x"$DO1" = xYES ]; then
      $ECHO "${M}DO1: $ARG3"
      cat $STDIN | $NPROG $ARG3 > fundisp1.out
    else
      $ECHO "${M}EXACT: $ARG2 ... $ARG3"
      CMP=yes
      XPROG=""
      $OPROG $ARG2 > tfundisp1.out
      if test $? -ne 0; then
        CMP=no
        XPROG="$OPROG"
      fi
      cat $STDIN | $NPROG $ARG3 > tfundisp2.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" < tfundisp1.out > fundisp1.out
          sed "/^ *$X      $Y.*$/d" < tfundisp2.out > fundisp2.out
        else
          mv tfundisp1.out fundisp1.out
          mv tfundisp2.out fundisp2.out
        fi
        diff fundisp1.out fundisp2.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 "***** fundisp1.out *****"
    cat fundisp1.out
  fi
  if [ $VERBOSE = 2 ]; then
    $ECHO "***** fundisp1.out *****"
    cat fundisp1.out
    $ECHO "***** fundisp2.out *****"
    cat fundisp2.out
  fi
  rm -f err.log
done

rm -f fundisp1.out fundisp2.out tfundisp1.out tfundisp2.out
