#!/usr/bin/env bash
DOC="runtest - Run n3proc Test Suite
Author: Sean B. Palmer, inamidst.com
License: GPL 2; share and enjoy!

Usage: 
   ./runtest [options] ../n3proc.py input
     -h        Display help
     -n <str>  Only run tests starting with str

The "input" variable will be substituted with a test file name.
"

if [[ ($# < 1) ]]; then
   echo -n "$DOC"
   exit
fi

OPT=$(getopt +hn: $* 2>&1)
if [ $? -ne 0 ]; then
   echo -e $(basename $0) "option error: $OPT" >&2; echo
   exit 1
fi
set -- $OPT

TESTS=''
while : 
do
   arg="$1"
   if [ -z "$arg" ]; then break; fi

   case $arg in
   -h)	 echo -n "$DOC"; exit;; 
   -n)   shift; TESTS="$1"; shift;; 
   --)   shift; break;;
   -*)   echo "Unknown arg: $arg (try -h)" >&2; exit 1;; 
   esac
done

PASSES=0
FAILURES=0

for fn in $(ls -1 $TESTS*.n3); do
   if [ -f ${fn%.n3}.nt ]; then
      eval $(echo "$@" | sed "s/input/$fn/") > $$.input
      RESULT=$(python cf.py $$.input ${fn%.n3}.nt)

      if echo $RESULT | egrep ' pass$' &> /dev/null; (( $? ))
      then FAILURES=$(($FAILURES + 1))
      else PASSES=$(($PASSES + 1))
      fi

      echo $RESULT
      rm $$.input
   fi
done

echo "Passes: $PASSES; Failures: $FAILURES"
