#!/bin/sh
# This is a wrapper around the FSL Evaluation and Example Data Suite.
# Written by Michael Hanke <michael.hanke@gmail.com>
#
# Copyright (C) 2005-2007 by
# Michael Hanke        michael.hanke@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

# Default values for option(s)
CLEAN_RESULTS=0

# Parse commandline options (taken from the getopt examples from the Debian util-linux package)

# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need CLOPTS as the `eval set --' would nuke the return value of getopt.
CLOPTS=`getopt -o h,c --long help,clean-results -n 'fsl-selftest' -- "$@"`

if [ $? != 0 ] ; then
	echo "Terminating..." >&2
	exit 1
fi

# Note the quotes around `$CLOPTS': they are essential!
eval set -- "$CLOPTS"

while true ; do
	case "$1" in
		-c|--clean-results) CLEAN_RESULTS=1; shift;;
		-h|--help)
			printf "FSL-SELFTEST - runs all or selected tests of the FSL Evaluation\n"
			printf "               and Example Data Suite.\n\n"
			printf "Usage: fsl-selftest [options] [test]\n\n"
			printf "Options:\n"
			printf "  -h, --help          - Print this help.\n"
			printf "  -c, --clean-results - Delete the temporary output directory with\n"
			printf "                        all data computed during the test run. Default: False.\n"
			printf "\nPlease see the manpage for more information.\n"
			exit 0 # nothing left to do
			;;
		--) shift ; break ;;
		*) echo "Internal error!"; exit 1;;
	esac
done


# the root of the feeds data on Debian systems
SRC_DIR=/usr/share/fsl-feeds

echo "Create temporary working directory"
WDIR=`mktemp -d /tmp/fsl-selftest.XXXXXX || exit 1`
echo "Using $WDIR as working directory"


# enter working dir
cd $WDIR

# make links to example data
ln -s $SRC_DIR/data
ln -s $SRC_DIR/RUN


# load FSL config
[ "x$FSLDIR" = "x" ] && . /etc/fsl/fsl.sh || echo "Using FSL installation at $FSLDIR"


# test all FSL modules if none specified
if [ -z "$1" ]; then
	MODE="all"
else
	MODE="$1"
fi

# run the tests
./RUN $MODE

if [ "$CLEAN_RESULTS" = "1" ]; then
	printf "Remove temporary directory '$WDIR'\n"
	rm -rf $WDIR
	printf "Done.\n"
else
	echo "Done. Leaving results in $WDIR"
fi

