#! /bin/sh

SAOTRACE_TEST_ROOT=${SAOTRACE_TEST_ROOT:-$(dirname $(dirname $0))}
export SAOTRACE_CONFIG=orbit-200809-01f-a

test -n "$SAOTRACE_DB" || { echo >&2 "Please set the SAOTRACE_DB environment variable to the root directory of the SAOTrace Configuration Database"; exit 1; }

test -d "$SAOTRACE_DB" || { echo >&2 "$SAOTRACE_DB: The SAOTrace Configuration Database directory specified via SAOTRACE_DB does not exist"; exit 1; }

test -d "$SAOTRACE_TEST_ROOT/tests" || { echo >&2 "Please set the environment variable SAOTRACE_TEST_ROOT to the root directory of the SAOTrace installation"; exit 1; }

test -f "${SAOTRACE_TEST_ROOT}/bin/trace-nest" || { echo >&2 "unable to find trace-nest executable:  ${SAOTRACE_TEST_ROOT}/bin/trace-nest"; exit 1; }

PFILES_RW=$(mktemp -d)
test $? -eq 0 || {  echo >2 "Unable to create temporary PFILES directory"; exit 1;}
export PFILES="$PFILES_RW"
cp "${SAOTRACE_TEST_ROOT}/tests/trace-nest.par" "$PFILES_RW"
${SAOTRACE_TEST_ROOT}/bin/plist trace-nest
${SAOTRACE_TEST_ROOT}/bin/trace-nest output=foo.fits
error=$?
test -d "$PFILES_RW" && rm -rf "$PFILES_RW"
test $error -eq 0 && { echo "Success!"; exit 0; } || { echo "Failure"; exit 1; }
