#! /bin/sh
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="glasnost"
NAME="glasnost"
DIR=/usr/lib/glasnost/
GLASNOST_USER=glasnost-daemon

#set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	for SERV in Dispatcher VirtualHostsServer Gateway
	do
		start-stop-daemon --chuid $GLASNOST_USER \
				--start --exec $DIR/servers/$SERV/$SERV.py
		echo -n '.'
		sleep 5
	done
	for SERV in `ls $DIR/servers/ | egrep -v 'Dispatcher|Gateway|VirtualHosts'`
	do
		start-stop-daemon --chuid $GLASNOST_USER \
				--start --exec $DIR/servers/$SERV/$SERV.py
		echo -n '.'
	done

	echo " done."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --user $GLASNOST_USER || true
	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 (this takes time): "
	start-stop-daemon --stop --user $GLASNOST_USER || true
	
	sleep 20

	for SERV in Dispatcher VirtualHostsServer Gateway
	do
		start-stop-daemon --chuid $GLASNOST_USER \
				--start --exec $DIR/servers/$SERV/$SERV.py
		echo -n '.'
		sleep 5
	done
	for SERV in `ls $DIR/servers/ | egrep -v 'Dispatcher|Gateway|VirtualHosts'`
	do
		start-stop-daemon --chuid $GLASNOST_USER \
				--start --exec $DIR/servers/$SERV/$SERV.py
		echo -n '.'
	done

	echo " done."
	;;
  reload)
  	echo "not implemented"
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
