#! /bin/sh

failed=0
test_tmp=""

tests="test_setup_args_missing_r require_root 1 \
       test_setup_args_h false 0                \
       test_setup_args_c_bad require_root 1     \
       test_setup_args_c_good require_root 0"

set -- $tests

echo "1..$(( $# / 3 ))"

require_root()
{
    [ $(id -u) -ne 0 ]
}

cleanup_tmp()
{
    if [ -n "$test_tmp" ]; then
        rm -rf "$test_tmp"
    fi
    return $failed
}

trap cleanup_tmp 0

test_tmp="$(mktemp -d -t "$(basename $0)XXXXXXXXXX")"
if [ ! -d "$test_tmp" ]; then
    echo "Bail out! Can't create temp file"
    exit 99
fi

test_setup_args_missing_r()
{
    globus-gridftp-server-setup-chroot
}

test_setup_args_h()
{
    globus-gridftp-server-setup-chroot -h
}

test_setup_args_c_bad()
{
    globus-gridftp-server-setup-chroot -c ${X509_CERT_DIR}.bad -r $test_tmp/test_setup_args_c_bad
}

test_setup_args_c_good()
{
    globus-gridftp-server-setup-chroot -c ${X509_CERT_DIR} -r $test_tmp/test_setup_args_c_good
}

i=0
while test "$#" -gt 0; do
    i=$(($i + 1))
    test_name="$1"
    shift
    test_skip="$1"
    shift
    test_expect="$1"
    shift

    if $test_skip; then
        res="$test_expect"
        skip_reason=" #SKIP $test_skip"
    else
        testout="${test_tmp}/${test_name}.out" 
        testerr="${test_tmp}/${test_name}.err" 
        $test_name > "$testout" 2> "${testerr}"
        res=$?
        sed -e 's/^/#    /' < ${testout}
        sed -e 's/^/#    /' < ${testerr}
        skip_reason=""
    fi
    if [ $res -eq $test_expect ]; then
        echo "ok $i - $test_name$skip_reason"
    else
        echo "not ok $i - $test_name"
        failed=$(($failed + 1))
    fi
done
exit $failed
