#!/bin/bash
#
# A wrapper script to emulate the interface and functionality of the
# old mts tool, mapping it to the interface of the new MakeTestScript
# program.
#
# This script is intended to be a facade to provide backward compatibility
# for existing scripts expecting the old mts tool interface. Error handling
# is not comprehensive and may not be robust to abnormal inputs. Where
# possible, scripts should be updated to use the new MakeTestScript tool
# interface directly. This script is intended in part to serve as an example
# of how to update such existing scripts. For this reason, more verbose
# parameters are used in some cases to make this script more pedagogically
# useful.
#
# Do not use this script to invoke MakeTestScript from an interactive shell;
# learn the new interface.
#
# Alex Kinneer
# University of Nebraska - Lincoln
# Feb. 8, 2006
#
# modified by
# Wayne Motycka
# University of Nebraska -Lincoln
# August 18, 2010

# Before I made the MTS_PATH hunting code (see next block) this
# was how we set the MTS_PATH in a hard coded fashion.  If the
# hunting code doesn't work for you try uncommenting this to
# and set your MTS_PATH path to MakeTestScript here
# 
#if [ `uname -s` -eq "Linux" ]
#then
#    MTS_PATH="/home/wmotycka/mts"
#elif [ `uname -s` -eq "SunOS" ]
#then
#    MTS_PATH="/home/ugrad/wmotycka/mts"
else
#    MTS_PATH="${HOME}/mts"
#fi
#
# SET THE MTS_PATH VARIABLE to the path where you put MakeTestScript on your system
# Originally this was hard coded, this next block tries to find it automatically
# Starting at the base path of the command executed ($0) and parse up to the root (/)
# only works if you executed the mts/javamts cmd from the mts/bin/[bc]sh directory
#

MTS_PATH=${0%/*}
if [ ! -f ${MTS_PATH}/MakeTestScript.jpx ]
then
    # go hunting for the mts base directory
    while ! [ -f ${MTS_PATH}/MakeTestScript.jpx ]
    do
       MTS_PATH=`echo ${MTS_PATH%/*}`
       if [ $MTS_PATH == "/" ]
       then
           echo "exhausted search for mts root dir, can't find dir where mts is"
           echo "installed on your system.  Try using the full path to the mts script"
           exit 1
       fi
    done
fi


# Set this variable if you want to override the location of antlr
ANTLR_PATH="${MTS_PATH}/lib/antlr-2.7.6.jar"


# Do not change the script beyond this point unless you really know what you
# are doing

CLASSPATH=${CLASSPATH}:${MTS_PATH}/bin/mts.jar:${ANTLR_PATH}
export CLASSPATH

# Usage message copied verbatim
printUsage() {
echo ''
echo 'USAGE: mts <subject-dir> <subject-exe> <universe-file> R|D|d|T <script-name> <program-suffix> <dir_old>'
echo '  <subject-dir>      : name of test database dir'
echo '  <subject-exe>      : name of exe'
echo '  <universe-file>    : directory/name of universe file'
echo '   R|D|d|T|d_t|D_t   : script type = R: Runall,'
echo '                                     D: Runall-Diff using cmp,'
echo '                                     d: Runall-Diff using diff,'
echo '                                     T: Runall-Trace,'
echo '                                     D_t: Runall-Diff Time using cmp,'
echo '                                     d_t: Runall-Diff Time using diff,'
echo '  <script-name>      : name of output script'
echo '  <program-suffix>   : suffix of program name(will append '.c.tr'),'
echo '                       or NULL (case insensitive) -- only for script type T.'
echo '  <dir_old>          : name of directory containing outputs to compare'
echo '                       test outputs to, or NULL (case insensitive)'
echo '                       --only for script type D'
echo ''
echo ''
echo ''
exit 1
}

if [ $# -lt 5 ]; then
  printUsage
fi

# Turn off all shell tokenizing of variables, so we can reliably pass certain
# parameters verbatim to MakeTestScript.
IFS=""

STIMPLE_FILE="${3}"
SCRIPT_NAME="${5}"
EXP_DIR="${1}"
SUBJECT_EXE="${2}"

# Construct the arguments to create 'diff'-scripts. Because variable tokenizing
# is disabled, we use arrays to pair parameter data with the appropriate
# MakeTestScript parameter flags, otherwise it would all be passed as one giant
# parameter to MakeTestScript. If the array is unset, it will be expand to
# nothing, so it is safe for the non 'diff'-type scripts.
setDiffArgs() {
  DIFF_ARGS[0]="--compare-outputs"
  if [ "${1}" != "" ] && [ `echo ${1} | tr A-Z a-z` != "null" ]; then
    DIFF_ARGS[1]="${1}"
    DIFF_ARGS[2]="${2}"
  else
    echo 'Argument 7 (dir_old) must be present and not null'
    exit 1
  fi
}

# Construct the arguments to create trace scripts. The same considerations
# apply here as for the setDiffArgs() function.
setTraceArgs() {
  TRACE_ARGS[0]="--trace"
  TRACE_ARGS[1]="--trace-source-dir"
  TRACE_ARGS[2]='${ARISTOTLE_DB_DIR}'
  #TRACE_ARGS[2]='$ARISTOTLE_DB_DIR' # Exact diff compatibility with old mts
  if [ "${1}" != "" ] && [ `echo ${1} | tr A-Z a-z` != "null" ]; then
    TRACE_ARGS[3]="--trace-name"
    TRACE_ARGS[4]="${1}.c.tr"
  else
    echo 'Argument 6 (program_suffix) must be present and not null'
    exit 1
  fi
}

# Convert the old 'script-type' parameter to the appropriate parameters
# to MakeTestScript
case "${4}" in
  R)
    ;;
  D)
    setDiffArgs "${7}" "cmp -s"
    ;;
  d)
    setDiffArgs "${7}" "diff -r"
    ;;
  T)
    setTraceArgs "${6}"
    ;;
  D_T)
    setDiffArgs "${7}" "cmp -s"
    setTraceArgs "${6}"
    ;;
  d_T)
    setDiffArgs "${7}" "diff -r"
    setTraceArgs "${6}"
    ;;
  *)
    printUsage
    ;;
esac

# Invoke MakeTestScript. As noted previously, the DIFF_ARGS and TRACE_ARGS
# array expansions disappear if the arrays are unset. All other parameters
# (and input data used in DIFF_ARGS and TRACE_ARGS where appropriate) are
# passed verbatim, including spaces and any other special characters.
java sir.mts.MakeTestScript --no-escapes --stimple-file ${STIMPLE_FILE} --script-name ${SCRIPT_NAME} --experiment-dir ${EXP_DIR} --exe-name ${SUBJECT_EXE} ${DIFF_ARGS[@]} ${TRACE_ARGS[@]} --target csh --print-config
