#! /bin/sh

case "$1" in

/*)
	prog="$1"
	;;
*)
	prog="`which $1`"
	;;
esac

set -e

case "$2" in
start)
	if test "$3" = ""
	then
		echo "Usage: $prog start [configfile]" >&2
		exit 1
	fi

	/usr/sbin/env -i /bin/sh -c "set -a; . $3; $prog check || exit 1; \
		/usr/bin/courierlogger -pid="'"${PIDFILE}"'" $LOGGEROPTS -start $prog"
	exit $?
	;;
stop)
	if test "$3" = ""
	then
		echo "Usage: $prog stop [configfile]" >&2
		exit 1
	fi

	/usr/sbin/env -i /bin/sh -c "set -a; . $3;\
		/usr/bin/courierlogger -pid="'"${PIDFILE}"'" $LOGGEROPTS -stop $prog"
	exit $?
	;;
restart)
	if test "$3" = ""
	then
		echo "Usage: $prog restart [configfile]" >&2
		exit 1
	fi

	/usr/sbin/env -i /bin/sh -c "set -a; . $3; $prog check || exit 1; \
		/usr/sbin/mkdir -p `dirname $PIDFILE` ; \
		/usr/bin/courierlogger -pid="'"${PIDFILE}"'" $LOGGEROPTS -restart $prog"
	exit $?
	;;
*)
	echo "Invalid command" >&2
	exit 1
esac
