#! /bin/sh

DAEMON=/usr/sbin/snui_server
NAME=snui-server
DESC="SNUI Server"

test -x $DAEMON || exit 0

if [ ! -f /etc/snui/snui.conf ]; then
	echo "/etc/snui/snui.conf does not exist; aborting startup"
	exit 0
fi

if [ ! -f /etc/snui/apache.conf ]; then
	echo "/etc/snui/apache.conf does not exist; no Web admin will be available"
fi

set -e

case "$1" in
  start)
	echo -n "Starting SNUI Server:"
	start-stop-daemon --start --background --quiet --make-pidfile \
		--pidfile /var/run/snui_server.pid -c snui-admin:snui-admin \
		--exec /usr/sbin/snui_server
	echo -n " snui_server"
	if [ -f /etc/snui/apache.conf ]; then
		start-stop-daemon --start --quiet --pidfile /var/run/snui_apache.pid \
			--exec /usr/sbin/apache -- -f /etc/snui/apache.conf
		echo -n " snui_apache"
	fi
	echo "."
	;;
  stop)
	echo -n "Stopping SNUI Server:"
	start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/snui_server.pid
	echo -n " snui_server"
	start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/snui_apache.pid
	echo -n " snui_apache"
	echo "."
	;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo -n "Reloading $DESC configuration..."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
	# echo "done."
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	start-stop-daemon --stop --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON
	echo "."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
