#! @SHELL@
echo 1..16 # Number of tests to be executed.
#
test -n "$CDO"      || CDO=cdo
test -n "$DATAPATH" || DATAPATH=./data
#
CDOOUT=cout$$
CDOERR=cerr$$
FORMATS="srv ext ieg grb grb2 nc nc2 nc4"
#
NTEST=1
#
function fileformat()
{
  FILEFORMAT=undefined
  ENABLE_TEST=yes

  if [ "${FORMAT}" = srv  ] ; then  FILEFORMAT=SERVICE;  fi
  if [ "${FORMAT}" = ext  ] ; then  FILEFORMAT=EXTRA;    fi
  if [ "${FORMAT}" = ieg  ] ; then  FILEFORMAT=IEG;      fi
  if [ "${FORMAT}" = grb  ] ; then  FILEFORMAT=GRIB;     fi
  if [ "${FORMAT}" = grb2 ] ; then  FILEFORMAT=GRIB_API; fi
  if [ "${FORMAT}" = nc   ] ; then  FILEFORMAT=netCDF;   fi
  if [ "${FORMAT}" = nc2  ] ; then  FILEFORMAT=netCDF2;  fi
  if [ "${FORMAT}" = nc4  ] ; then  FILEFORMAT=netCDF4;  fi

  if [ "@ENABLE_SERVICE@" = no -a "${FORMAT}" = srv  ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_EXTRA@" = no   -a "${FORMAT}" = ext  ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_IEG@" = no     -a "${FORMAT}" = ieg  ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_GRIB@" = no    -a "${FORMAT}" = grb  ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_GRIBAPI@" = no -a "${FORMAT}" = grb2 ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_NETCDF@" = no  -a "${FORMAT}" = nc   ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_NETCDF@" = no  -a "${FORMAT}" = nc2  ] ; then  ENABLE_TEST=no; fi
  if [ "@ENABLE_NC4@" = no     -a "${FORMAT}" = nc4  ] ; then  ENABLE_TEST=no; fi
}
#
for OPERATOR in write copy; do
  for FORMAT in $FORMATS; do
    RSTAT=0

    CDOTEST="$OPERATOR $FORMAT"
    echo "Running test: $NTEST"

    FILEFORMAT=undefined
    ENABLE_TEST=yes

    fileformat

    if [ "${ENABLE_TEST}" = yes ] ; then
      for DATATYPE in F32 F64; do
        FILE=file_${DATATYPE}_${FORMAT}
        RFILE=$DATAPATH/file_F32_srv_ref

	if [ $OPERATOR = write ] ; then
          CDOCOMMAND="$CDO -f $FORMAT -b $DATATYPE cdi${OPERATOR},1,global_10,3,3,3 $FILE"
          echo "$CDOCOMMAND"

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

          $CDO diff,0.0001 $FILE $RFILE > $CDOOUT 2> $CDOERR
          test $? -eq 0 || let RSTAT+=1
          test -s $CDOOUT && let RSTAT+=1
          cat $CDOOUT $CDOERR
        else
          OFILE=file_${DATATYPE}_${FORMAT}_res
          CDOCOMMAND="$CDO ${OPERATOR} $FILE $OFILE"
          echo "$CDOCOMMAND"

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

          $CDO diff,0.0001 $FILE $OFILE > $CDOOUT 2> $CDOERR
          test $? -eq 0 || let RSTAT+=1
          test -s $CDOOUT && let RSTAT+=1
          cat $CDOOUT $CDOERR

	  rm -f $FILE $OFILE
        fi
      done

      test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
      test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
    else
      test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST # SKIP $FILEFORMAT not enabled"
    fi

    let NTEST+=1
  done
done
#
rm -f $CDOOUT $CDOERR
#
exit 0
