#! /bin/sh
#
# This file was automatically customized by dh-make on Thu, 29 Nov 2001 19:33:30 +0100

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/slashd
NAME=slash
DESC=slashd

# Slash home
DATADIR=/etc/slash
SLASHD=/usr/sbin/slashd
SLASHSITE=$DATADIR/slash.sites

# This is where you store the running PID of the slashd's you start
# Under Redhat people like to place this in /var/run
RUNNING_PID=/var/run

# To figure out where things are...
export TZ=GMT
WHICH_CAT=`which cat`
WHICH_CUT=`which cut`
test -f $DAEMON || exit 0

GRAB_CONFIG=`$WHICH_CAT $SLASHSITE | $WHICH_CUT -d"#" -f1` 
if [ -z "`echo $GRAB_CONFIG | grep :`" ]; then
	echo "NOT Starting/stopping slashd: No sites in $SLASHSITE";
	exit 0;
fi

case "$1" in
  start)
	echo -n "Starting $DESC: "
	cd $DATADIR
	for server_name in $GRAB_CONFIG
		do
			SERVER_NAME=`echo $server_name |$WHICH_CUT -d":" -f1`
			USERNAME=`echo $server_name |$WHICH_CUT -d":" -f2`
			echo -n "Starting slashd $SERVER_NAME: "

			# There are differing syntaxes of 'su' between OSes
			# and even between different distributions of the same
			# OS. If you have an OS that isn't listed here (or is
			# a different case of one listed here [ie Red Hat Linux,
			# Debian Linux]) please add in the necessary logic and
			# send in a patch. Thanks!
			#
			# - Slashteam (slashteam@osdn.com)

			# if you aren't using GMT for internal dates, please change
			# the appropriate lines, below.
			if [ "$OS" = "FreeBSD" ] ; then
				su - $USERNAME "-c 'TZ=GMT $SLASHD $SERVER_NAME'" &
			elif [ "$OS" = "Linux" ] ; then 
				su - $USERNAME -c "TZ=GMT $SLASHD $SERVER_NAME" &
			else
				TZ=GMT $SLASHD $SERVER_NAME &
			fi
# this is handled by slashd
#			echo $! > $RUNNING_PID/site/$SITENAME/logs/slashd.pid
		done
	echo "$NAME."
    ;;
  stop)
	echo -n "Stopping $DESC: "
	cd $DATADIR
	for server_name in $GRAB_CONFIG
		do
			SERVER_NAME=`echo $server_name |$WHICH_CUT -d":" -f1`
			USERNAME=`echo $server_name |$WHICH_CUT -d":" -f2`
			SITENAME=`echo $server_name |$WHICH_CUT -d":" -f3`
			if [ -f $RUNNING_PID/slashd.$SITENAME.pid ] ;then
				echo -n "Stopping slashd $SERVER_NAME: "
				if ! kill `cat $RUNNING_PID/slashd.$SITENAME.pid` ;then
					echo -n "...using kill -9..."
					sleep 3
					kill -9 `cat $RUNNING_PID/slashd.$SITENAME.pid`
					rm -f $RUNNING_PID/slashd.$SITENAME.pid
				fi
# this is handled by slashd
#				rm -f $RUNNING_PID/site/$SITENAME/logs/slashd.pid
				echo "ok."
			else
				echo "Slashd $SERVER_NAME has no PID file"
			fi
		done

	echo "$NAME."
      ;;
  #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 "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
  #;;
  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: "
	for server_name in $GRAB_CONFIG
		do
		SITENAME=`echo $server_name |$WHICH_CUT -d":" -f3`
		start-stop-daemon --stop --quiet --pidfile \
		/var/run/$NAME.$SITENAME.pid --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.$SITENAME.pid --exec $DAEMON
		done
	echo "$NAME."
	;;
  *)
	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
