#!/bin/sh
#
# This script is intended to run under Cygwin on a Window's machine
# with a clean export of CERT version 3.2 or later.
# It will run the release build of CERTDemo on Subject 32 and
# calculate the area under the ROC.


RESULTS=GKR_Results_nomodel.txt
RESULTS2=GKR_Results_withmodel.txt
AROC=NA
AROC_file=AreaUnderROC.txt

if [ $# -ne 2 ]; then
    echo "Usage: GKR_Test.sh <relative path to subject neutral images> <relative path to subject test images>"
    exit
fi

NEUT=$1
SUBJ=$2

STARTDIR=../../WinBuildAndTest/Demos

if [ ! -d "${STARTDIR}/${NEUT}" ]; then
	echo "The neutral images directory ${NEUT} doesn't exist."
	exit
fi

if [ ! -d "${STARTDIR}/${SUBJ}" ]; then
	echo "The test images directory ${SUBJ} doesn't exist."
	exit
fi

cd ../..
TOPDIR=`pwd`

RELEASE_DIR=CERT2_VS2005/release 
if [ ! -d ${RELEASE_DIR} ]; then
	echo "The directory ${RELEASE_DIR} doesn't exist."
    echo "Build the release version of CERT before running this script."
	exit
fi

if [ ! -x ${RELEASE_DIR}/CERTDemo.exe ]; then
	echo "${RELEASE_DIR}/CERTDemo.exe doesn't exist."
    echo "Build the release version of CERT before running this script."
	exit
fi

if [ ! -f ${RELEASE_DIR}/libfftw3-3.dll ]; then
	cp ThirdPartyLibraries/libfftw3-3.dll ${RELEASE_DIR}
fi

rm -f "${TOPDIR}/${RESULTS}"
rm -f "${TOPDIR}/${RESULTS2}"

echo "Running GKR Demo in model building mode: `date`"
cd ${RELEASE_DIR}
./CERTDemo.exe -t -c "${STARTDIR}/GKR_Demo.cfg" -gt "GKR_Test_Model.xml" "${STARTDIR}/${NEUT}" > /dev/null  # "${TOPDIR}/${RESULTS}"

echo "Finished building model ${TOPDIR}/GKR_Test_Model.xml: `date`"

echo "Running GKR Demo in model using mode: `date`"

./CERTDemo.exe -t -c "${STARTDIR}/GKR_Demo.cfg" -gc "GKR_Test_Model.xml" "${STARTDIR}/${SUBJ}" > "${TOPDIR}/${RESULTS2}"

mv GKR_Test_Model.xml "${TOPDIR}"

echo "Outputs are in (includes both raw AU and GKR/CKF outputs): ${TOPDIR}/${RESULTS2}"
echo "The model itself is in: ${TOPDIR}/GKR_Test_Model.xml"
