#! /bin/sh -e

local NAME DESCRIPTION DAEMON

NAME=triggerd
DESCRIPTION="triggerd"
DAEMON=/bin/$NAME

. /etc/init.d/functions.sh
. /etc/conf.d/$NAME || error "/etc/conf.d/$NAME missing"

utask_signal () {
	local utask_pids

	# Notice: this sends the signal to all processes called 'utask' (not
	#	  only the daemon)
	utask_pids=$(pidof utask) &&
		[ "$utask_pids" ] &&
		killall -USR2 $utask_pids >/dev/null 2>&1 || :

	return 0
}

start_triggerd() {
	if [ "$TRIGGERD_ENABLED" = yes ]; then
		start_daemon $DAEMON $TRIGGERD_OPTIONS || return 1
		utask_signal
	else
		information "disabled"
	fi

	return 0
}

stop_triggerd() {
	stop_daemon $DAEMON
}

case "$1" in
	start)
		begin "Starting $DESCRIPTION"
		start_triggerd
		end $?
		;;
	stop)
		begin "Stopping $DESCRIPTION"
		stop_triggerd
		end $?
		;;
	restart)
		begin "Restarting $DESCRIPTION"
		stop_triggerd && start_triggerd
		end $?
		;;
	*)
		error "Usage: $0 start|stop|restart"
		;;
esac

exit 0
