#!/bin/bash

if [ -z "$LILLYMOL_HOME" ] || [ -z "$BUILD_DIR" ]; then 
    echo "System variables LILLYMOL_HOME and BUILD_DIR are required for running the test"
    echo "Please export LILLYMOL_HOME (local path to LillyMol code)"
    echo "Please export BUILD_DIR (the folder name under the bin folder after build)"
    echo "Example: export LILLYMOL_HOME=/home/user/LillyMol"
    echo "Example: export BUILD_DIR=Linux-gcc-7.2.1" 
    exit 1
else
    BIN_DIR="$LILLYMOL_HOME/bin/$BUILD_DIR"
fi

test_command=dicer
case=case_1
case_id="Case 1"

test_top="$LILLYMOL_HOME/test"
test_cmd_top="$test_top/$test_command"

diff_tool=../../fileDiff.sh
same_lines=../../same_lines.py

command="$BIN_DIR/$test_command"

if [ ! -x "$command" ]; then
    echo "$command is not executable or does not exist"
    exit 1
fi

in="$test_cmd_top/$case/in/test.smi"
out=test.smi
cmp_out="$test_cmd_top/$case/out/test.smi"

# Support linux and mac 
if [[ "${UNAME}" == "Linux" ]]; then
    cmp_out=out/${UNAME}/test.smi
elif [[ "${UNAME}" == "darwin"* ]]; then
    cmp_out=out/osx/test.smi
else
    echo "${UNAME} is not supported"
fi

echo "Testing: $command"

# sort the output to avoid dependencies on hash ordering.

stderr='stderr'
$command -k 2 -X 5000 -m 5 -M 20 -A D "$in" 2> ${stderr} > ${out}

$diff_tool "${out}" "${cmp_out}"

if [[ $? -eq 1 ]] ; then
  echo "${case_id} : TEST PASS"
  rm -f "${out}" "${stderr}"
  exit 0
fi

${same_lines} "${out}" "${cmp_out}"
if [[ $? -eq 0 ]] ; then
  echo "${case_id} : TEST PASS"
  rm -f "${out}" "${stderr}"
  exit 0
fi

echo "$case_id : TEST FAIL"

rm -f "${out}"
rm -f "${stderr}"
