#!/bin/sh

set -e
pvdir=$PWD

if [ ! -x ./analyze ]
then
    echo ERROR analyze not found
    exit 2
fi

if [ -d /local/bblanche/tmp ]
then
    TMPPARENT=/local/bblanche/tmp
else
    TMPPARENT=.
fi

TMPDIR="$(mktemp -d "$TMPPARENT/tmp.XXXXXXXXXX")"
TMP=$TMPDIR/tmp
timeoutopt=""

# usage message, in case of bad arguments

usage()
{
    cat <<'EOF'
Usage: test [-timeout <n>] <mode> <test_set>
where
-timeout <n>: sets the time out to <n> seconds
<mode> can be:
- test: test the mentioned scripts
- test_add: test the mentioned scripts and add the
  expected result in the script when it is missing
- add: add the expected result in the script when it is missing,
  do not test scripts that already have an expected result
- update: test the mentioned scripts and update the expected
  result in the script
and <test_set> can be: typed, untyped, or
dir <prefix> <list_of_directories>
- typed runs ProVerif on examples for the typed front-ends
- typedopt runs ProVerif on examples for the typed front-ends,
  with various additional options
- untyped runs ProVerif on examples for the untyped front-ends
- arinc runs ProVerif on the ARINC823 protocol
- dir <prefix> <list_of_directories> analyzes the mentioned directories
  using ProVerif, using <prefix> as prefix for the output files.
EOF
    exit 2
}

# analyzedirlist <prefix> <list of directories>
# analyzes the ProVerif scripts in the given list of directories,
# and outputs the results in tests/<prefix>`date`
# with summary in tests/sum-<prefix>`date`
# and comparison with expected results on the standard output and in tests/res-<prefix>`date`

analyzedirlist()
{
    options=$1
    shift
    prefix=$1
    shift
    for dir in "$@"
    do
	if [ -d "$dir" ]
	then
	    if [ -x "$dir/prepare" ]
	    then
		cd "$dir"
		./prepare
		cd "$pvdir"
	    fi
	fi
    done
    ./analyze $timeoutopt $options PV "$mode" "$TMPDIR" "$prefix" dirs "$@"
}

mkdir -p tests

case X$1 in
    X-timeout) timeoutopt="-timeout $2";
	       shift
	       shift;;
    *)  ;;
esac


case X$1 in
    X)  mode=test;;
    Xtest|Xtest_add|Xadd|Xupdate)
	mode=$1
	shift;;
    *)  echo "Error: unknown mode $1."
	usage;;
esac

case X$1 in
    X|Xtyped)
	analyzedirlist "" typed examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync ;;
    Xtypedopt)
	analyzedirlist "-progopt -set inductionQueries true -endprogopt" typed_inductionQueries examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set preciseActions trueWithoutArgsInNames -endprogopt" typed_precise_noarg examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
#	analyzedirlist "-progopt -set preciseActions true -set removeEventsForLemma true -endprogopt" typed_precise_remove_event examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
#	analyzedirlist "-progopt -set preciseActions trueWithoutArgsInNames -set removeEventsForLemma true -endprogopt" typed_precise_noarg_remove_event examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set movenew true -endprogopt" typed_movenew examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set movelet false -endprogopt" typed_movelet_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set preciseLetExpand false -endprogopt" typed_preciseLetExpand_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set expandSimplifyIfCst false -endprogopt" typed_expandSimplifyIfCst_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set nounifIgnoreAFewTimes auto -endprogopt" typed_nounifIgnoreAFewTimes_auto examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set simplifyDerivation false -endprogopt" typed_simplifyDerivation_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set abbreviateDerivation false -endprogopt" typed_abbreviateDerivation_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set explainDerivation false -endprogopt" typed_explainDerivation_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set unifyDerivation false -endprogopt" typed_unifyDerivation_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set displayDerivation false -endprogopt" typed_displayDerivation_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set traceBacktracking false -endprogopt" typed_traceBacktracking_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set traceDisplay none -endprogopt" typed_traceDisplay_none examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set abbreviateClauses false -endprogopt" typed_abbreviateClauses_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync
	analyzedirlist "-progopt -set removeUselessClausesBeforeDisplay false -endprogopt" typed_removeUselessClausesBeforeDisplay_false examples/pitype/secr-auth examples/pitype/noninterf examples/pitype/weaksecr examples/pitype/choice examples/pitype/lemma examples/cryptoverif examples/pitype/jfk examples/pitype/certified-mail-AbadiGlewHornePinkas/onefile examples/pitype/certified-mail-AbadiGlewHornePinkas/journalsas examples/pitype/ffgg examplesnd/lemmas examplesnd/pitype examplesnd/fromcv examplesnd/Ukano unclearIP/examples/pitype/sync ;;
    Xuntyped)
	analyzedirlist "" untyped examples/horn/secr examples/pi/secr-auth examples/pi/noninterf examples/pi/weaksecr examples/pi/choice examples/pi/jfk examples/pi/mailprotAbadi/onefile2 examples/pi/mailprotAbadi/journalsas2 examples/pi/ffgg examplesnd/attackspeed examplesnd/pibugs examplesnd/bugs examplesnd/peeter_examples unclearIP/examples/pi/sync ;;
    Xarinc)
	analyzedirlist "" arinc examples/pitype/arinc823 ;;
    Xdir)
	# test dir <prefix> <list of directories>
	# analyzes the CryptoVerif scripts in the given list of directories,
	# and outputs the results in tests/<prefix>`date`
	shift
	analyzedirlist "" "$@";;
    *)  echo "Unknown analysis set $1"
	usage
esac

rm -r "$TMPDIR"
