#!/usr/bin/sh
#
# Do a full reset on pScheduler
#
#BEGIN-HELP
#
# Usage: reset [ --force ]
#
#END-HELP

self_help()
{
    sed -n -e '/^#BEGIN-HELP/,/^#END-HELP/{//!p}' $0 \
	| sed -e 's/^#\s*//'
}


if [ "$(id -u)" != "0" ]
then
    echo "This must be done as root." 1>&2
    exit 1
fi

#
# Gargle the arguments
#

[ -t 0 -a -t 1 -a -t 2 ] \
    && INTERACTIVE=true \
    || INTERACTIVE=false

FORCE=false

while echo "$1" | grep -q -E '^--'
do
    case "$1" in
	--force)
	    FORCE=true
	    INTERACTIVE=false
	    shift
	    ;;
	--help)
	    self_help
	    exit 0
	    ;;
	*)
	    self_help 1>&2
	    exit 1
	    ;;
    esac
done

if [ $# -gt 0 ]
then
    self_help 1>&2
    exit 1
fi


if $INTERACTIVE
then
    cat <<EOF

WARNING

You are about to obliterate all stored data in pScheduler on this host.

Please confirm this by typing today's date in YYYY-MM-DD format at the
prompt below.

EOF
    printf "Confirm (YYYY-MM-DD): "
    read DATE
    if [ "${DATE}" != "$(date +%F)" ]
    then
	printf "\nDate does not match today's date.  Doing nothing.\n\n"
	exit 1
    fi

fi


${INTERACTIVE} && printf "\n"
pscheduler internal service stop

${INTERACTIVE} && printf "\nClearing all pScheduler data..."
pscheduler internal db-reset < /dev/null
${INTERACTIVE} && printf "  Done.\n\n"

pscheduler internal service start

${INTERACTIVE} && printf "\n\nDone.  Hope you meant to do that.\n\n"

exit 0
