#! /bin/sh

# Dec 10, 2012

# Portions of the script which must be customized are marked with "!!",
# below.

# The complete list of tests must be placed in "alltests", !!4, below.
# The test[s] for the SHORTTEST must be placed in "shortlist", !!5 below.

# pycaldb4.t (this file)
# driver script (unix sh) for myglue package testing

# tests/test_*.py (external file)
# Python testing script to be activated by the driver above
#

# syntax:
# pycaldb4.t [<testid> ... ]

# interface between python and unix-sh (this file) is done through
# "export INFILE" and  "export OUTFILE", see !!6, !!7, !!8

# Test suite activation may be made through  setting and exporting
# INFILE, see !!7, !!8

# Developer's comments:
# 

######################################################################

######################################################################
# subroutine
# exportenv <ket> <val>
# portably export environ-varible
exportenv()
{
 key=`echo $1`
 val=`echo $2`
 if test "x`uname`" = "xSunOS"; then
     if test $key = "CALDB"; then
         CALDB=$val; export CALDB
     elif test $key = "CALDBCONFIG";then
         CALDBCONFIG=$val; export CALDBCONFIG
     elif test $key = "CALDBALIAS";then
         CALDBALIAS=$val; export CALDBALIAS
     elif test $key = "PATH";then
         PATH=$val;export PATH
     fi
 else
     export $key=$val
 fi

 if test $key = "CALDB"; then
     echo "export $key =" $CALDB;
 elif test $key = "CALDBCONFIG";then
     echo "export $key =" $CALDBCONFIG
 elif test $key = "CALDBALIAS";then
     echo "export $key =" $CALDBALIAS
 elif test $key = "PATH";then
     echo "export $key=" $PATH
 fi


}
#####################################################################
# subroutine
# texec_dir <message>
# Fatal error exit

set_texec_dir()
{
    curpath=`dirname $0`
    thetool=$1
    if test -d "${curpath}/${thetool}"; then
        TEXEC_DIR="${curpath}/${thetool}";
    elif test -d "$ASCDS_INSTALL/test/bin/${thetool}"; then
        TEXEC_DIR="$ASCDS_INSTALL/test/bin/${thetool}";
    else
        echo "ERROR: Can't find ${thetool} test scripts.";
        exit 1;
    fi
    #exportenv PATH $TEXEC_DIR:$PATH
 
 }

######################################################################

# Initialization

# !!3
toolname="pycaldb4"

# set up list of tests
# !!4
alltests="test0 test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11 test12 test13"

# "short" test to run
# !!5
shortlist="test1 test2"




# compute date string for log file
DT=`date +'%d%b%Y_%T'`


# convenience definitions
OUTDIR=$TESTOUT/$toolname
SAVDIR=$TESTSAV/$toolname
INDIR=$TESTIN/$toolname
LOGDIR=$TESTLOG/$toolname



# set up log file name
LOGFILE=$LOGDIR/${toolname}_log.$DT

#get rid of old logs
rm -f $LOGDIR/${toolname}_log.*

# Any tests specified on command line?
if test $# -gt 0; then
  # yes, do those tests
  testlist=$*
else
  # No, see if we are to do "short" test
  if test "x$SHORTTEST" = "x" ; then
    # No, do everything
    testlist=$alltests
  else
    # yes, do short test
    testlist=$shortlist
  fi
fi

# Make sure we have a log directory
if test -d $LOGDIR ; then
 :
else
  mkdir -p $LOGDIR
  if test $? -ne 0 ; then
    error_exit ""
  fi
fi


# Make sure we have an output directory
if test -d $OUTDIR ; then
 :
else
  mkdir -p $OUTDIR >> $LOGFILE 2>&1
  if test $? -ne 0 ; then
    error_exit "can't create output directory $OUTDIR"
  fi
fi

# check for directory environment variables
if test "x${TESTIN}" = "x" -o "x${TESTOUT}" = "x" -o "x${TESTSAV}" = "x" \
   -o "x${TESTLOG}" = "x" ; then
  error_exit "one or more of TESTIN/TESTOUT/TESTSAV/TESTLOG not defined"
fi

# announce ourselves
echo ""
echo "${toolname} regression" | tee $LOGFILE
echo ""

script_succeeded=0

######################################################################
# Begin per-test loop
TEXEC_DIR=""
set_texec_dir ${toolname}   # assigned PATH there to SCRIPTDIR
echo "cd  ${TEXEC_DIR}"
cd ${TEXEC_DIR}
 
for testid in $testlist
do

  # delete old outputs
    rm -f $OUTDIR/${testid}*


  # Set up file names
    outfile=$OUTDIR/${testid}.txt
    savfile=$SAVDIR/${testid}.txt


# !!6
# export OUTFILE, INFILEs etc. as environmental variables
# which will be grabbed by python package called below
#
#
    #OUTFILE=$outfile;
    #export OUTFILE;   #???
    echo "running $testid" >> $LOGFILE
    
  ####################################################################
  # run the tool
    case ${testid} in

	test1) command_str="python test_caldb4.py -t1 -q > ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
	    ;;

	test2) command_str="python test_caldb4.py -t2 >  ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;

	test3) command_str="python test_caldb4.py -t3 >  ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test4) command_str="python test_caldb4.py -t4 > ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test5) command_str="python test_caldb4.py -t5 >  ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test6) command_str="python test_caldb4.py -t6 >  ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test7) command_str="python test_caldb4.py -t7 >  ${outfile}"
	    #TESTID=${testid};
	    #export TESTID;
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test8) command_str="python test_caldb4.py -t8 >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test9) command_str="python test_caldb4.py -t9 >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test10) command_str="python test_caldb4.py -t10 >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test11) command_str="python test_caldb4.py -t11 >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
	test12) command_str="python test_caldb4.py -t12 >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
            eval $command_str  | tee -a $LOGFILE  2>&1
            ;;

	test13) command_str="python test_caldb4.py -t13  >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
 
	test0) command_str="python test_imports.py >  ${outfile}"
	    rm -f ${outfile}
	    echo $command_str  | tee -a $LOGFILE
	    eval $command_str  | tee -a $LOGFILE  2>&1
            ;;
    esac


  ####################################################################
  # check the outputs

  # Init per-test error flag
  mismatch=1

  # if different tests need different kinds of comparisons, use a
  #  case ${testid} in...  here

  ######################################################################
  # ascii files
  #
  if test "x${testid}" = "xtest13"; then    # SM-36
      grep NOT-OK ${outfile} >/dev/null 2>> $LOGFILE
      if  test $? -eq 0 ; then  # yes, found 'NOT-OK' string
	  echo "ERROR: 'NOT-OK' in $OUTDIR/${testid}.txt" >> $LOGFILE
	  echo "grep NOT-OK ${outfile} " >> $LOGFILE
	  mismatch=0
      fi
  else
      diff $OUTDIR/${testid}.txt $SAVDIR/${testid}.txt >/dev/null 2>>$LOGFILE
      if  test $? -ne 0 ; then
	  echo "ERROR: TEXT MISMATCH in $OUTDIR/${testid}.txt" >> $LOGFILE
	  mismatch=0
      fi
  fi
  

  ####################################################################
  # Did we get an error?
  if test $mismatch -eq 0 ; then
    # Yes
    echo "${testid} NOT-OK"
    script_succeeded=1
  else
    # No
    echo "${testid} OK"
  fi

done
# end pre-test loop
######################################################################


######################################################################
# report results

# blank line
echo ""

if test $script_succeeded -eq 0; then
    echo "${toolname} : PASS" | tee -a $LOGFILE
else
    echo "${toolname} : FAIL" | tee -a $LOGFILE
fi

echo "log file in ${LOGFILE}"

exit $script_succeeded
