#! @SHELL@

# Checks whether cdo handles the inputargument right,
# most of the operators accept only one input argument.
# If you want to feed several inputfiles you can escape the files
# to make them one argument.

if [ "$USER" = "m214003" ]; then
echo 1..3 # Number of tests to be executed.
else
echo 1..1 # Number of tests to be executed.
fi
#
test -n "$CDO"      || CDO='cdo'
test -n "$DATAPATH" || DATAPATH=./data
#
IFILE1="testfile01"
IFILE2="testfile0?"
IFILE3="test*0?"
if [ "$USER" = "m214003" ]; then
  IFILES="$IFILE1 $IFILE2 $IFILE3"
else
  IFILES="$IFILE1"
fi
#
CDOOUT=cout$$
CDOERR=cerr$$
#
NTEST=1
#
for IFILE in $IFILES; do
  RSTAT=0
  CDOTEST="'$IFILE'"
  echo "Running test: $NTEST"
  INFILE=`ls $DATAPATH/grib_${IFILE}.grb`
  echo $INFILE

  for OPERATOR in info sinfo; do
    CDOCOMMAND="$CDO ${OPERATOR} $INFILE"
    echo "$CDOCOMMAND"

    $CDOCOMMAND  > $CDOOUT 2> $CDOERR
    test $? -eq 0 || let RSTAT+=1
    cat $CDOOUT $CDOERR
    mv $CDOOUT ref1

    CDOCOMMAND="$CDO ${OPERATOR} $DATAPATH/grib_${IFILE}.grb"
    echo "$CDOCOMMAND"

    $CDOCOMMAND  > $CDOOUT 2> $CDOERR
    test $? -eq 0 || let RSTAT+=1
    cat $CDOOUT $CDOERR
    mv $CDOOUT ref2

    diff ref1 ref2 > $CDOOUT 2> $CDOERR
    test $? -eq 0 || let RSTAT+=1

    rm -f ref1 ref2
  done
 
  test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
  test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
 
  let NTEST+=1
done
#
rm -f $CDOOUT $CDOERR
#
exit 0
