#!/bin/bash

PROG=`basename $0`
STEALTH=/usr/bin/stealth

testAbsolute()
{
    echo $1 | grep "^/" > /dev/null 2>&1 && return

    echo "\`$1' must be absolute path"
    exit 1
}

case $# in
    (2|3)
        testAbsolute $1
        testAbsolute $2
        if [ "$3" != "" ]
        then
            testAbsolute $3
            SKIP="-s $3"
        fi

        if [ -x ${STEALTH} ] ; then
            ${STEALTH} --ping $1
            if [ $? -ne 0 ] ; then
		/usr/bin/logger -t STEALTHCRON -p daemon.warning -- \
			restarting stealth --daemon $1 ...
		rm -f $1
		${STEALTH} --daemon $1 ${SKIP}  $2
	    fi
        fi
    ;;
            
    (*)
    echo "
$PROG by Frank B. Brokken (f.b.brokken@rug.nl)
Usage: $PROG uds policyfile [skipfile]
where:
    uds:        absolute path to the Unix Domain Socket to be used
    policyfile: absolute path to the policyfile to be used
    skipfile:   absolute path to the skipfile to be used (optional)

    calls $STEALTH} --ping uds. 
    If that fails, 
        ${STEALTH} --daemon uds policyfile
    or (if skipfile was specified)
        ${STEALTH} --daemon uds policyfile -s skipfile policyfile
    is started.
"
    exit 1
    ;;
esac
