#!/bin/bash
#
# Script that runs a quick check that the results are sensible
# 
# Run it from the top-level directory as scripts/check
#
# Created by GPS on 2009-09-05 and subsequently adapted by AK.
#----------------------------------------------------------------------

# list of tests to run:
# - for a test labelled $test, there should be a reference output file
#   $test.default_output

# Usage:
usage="Usage: scripts/check [-s sourcedir] [test1 [test2 [...] ] ]"
#  
#
# if sourcedir is not given, then reference results are taken
# from the current directory.
#
# if not tests are supplied, then it uses the default set of tests

default_tests=(\
         example_f90/tabulation_example \
         example_f90/tabulation_example_qed \
         example_f90/tabulation_example_qed_streamlined \
         example_f90/tabulation_example_n3lo \
         example_f90/structure_functions_example \
         example_f90/structure_functions_example_flavour \
     )

# parse the command line according to the usage, using a simple loop
# that just shifts and checks each argument
# Default sourcedir is one leel up from this script
sourcedir="$(dirname "$(realpath "$0")")/.."
tests=()

# Parse arguments
while [[ $# -gt 0 ]]; do
  case "$1" in
    -s)
      shift
      if [[ $# -eq 0 ]]; then
        echo "Error: -s requires an argument"
        exit 1
      fi
      sourcedir="$1"
      ;;
    -*)
      echo "Unknown option: $1"
      echo "$usage"
      exit 1
      ;;
    *)
      tests+=("$1")
      ;;
  esac
  shift
done
# if tests is empty, replace it with the default tests
if [ ${#tests[@]} -eq 0 ]; then
  echo "No tests specified, using default tests."
  tests=("${default_tests[@]}")
fi

echo "Source directory: $sourcedir"
echo "Tests: ${tests[@]}"

for test in ${tests[@]}; do
  echo
  echo '*****************************************************************'
  echo '    ' Doing check with $test
  echo '*****************************************************************'
  echo 
  echo "Running from $(pwd)"
  refname=$(echo "$test" | sed -e 's/:/_/g')
  checkname=$(echo "$test" | sed -e 's|/|_|g' -e 's/:/_/g')
  testcmd=$(echo "$test" | sed -e 's/:/ /g')
  extraargs=""  
  if [[ "$testcmd" == benchmarking/prec_and_timing* ]]; then
    extraargs="-o /dev/null -output-benchmark"
    refname=$(echo "$refname" | sed -e 's|prec_and_timing|prec_and_timing.default_output/prec_and_timing|g')
  fi
  echo "Running: ($testcmd $extraargs)"
  ($testcmd $extraargs | tee ${checkname}.tmp.new-pregrep) || exit 1;
  
  grep -vi -e '^ *[a-z]' -e '^\*' -e '^ *$' ${checkname}.tmp.new-pregrep > ${checkname}.tmp.new
  grep -vi -e '^ *[a-z]' -e '^\*' -e '^ *$' $sourcedir/$refname.default_output > ${checkname}.tmp.orig  

  diff ${checkname}.tmp.new ${checkname}.tmp.orig > ${checkname}.tmp.diff
  checkwc=`cat ${checkname}.tmp.diff| wc -l `
  
  echo
  if [ $checkwc == "0" ] 
  then
    echo check PASSED
    rm -f ${checkname}.tmp.new ${checkname}.tmp.orig ${checkname}.tmp.diff ${checkname}.tmp.new-pregrep
  else
    echo check FAILED: look at ${checkname}'.tmp.*' to see differences
    exit 1;
  fi
  
done

echo "All tests passed!"
