#!/bin/sh
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2001 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id: slash,v 1.5.2.7 2001/10/31 17:02:57 pudge Exp $

# /etc/init.d/slash
#
# written by Yazz Atlas for the Slashteam... 
# 	with assists from:
#		Jamie McCarthy
#		Cliff Wood
#

# This prolly ain't 100% accurate, but it should be good enough.
OS=`uname | awk '{print $1}'`

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:\
				/usr/local/sbin:/usr/bin/X11:/usr/X11R6/bin"

# Slash home
DATADIR=/usr/local/slash
SLASHD="$DATADIR/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
RUNNING_PID=$DATADIR

# To figure out where things are...
TZ=GMT
export TZ
WHICH_CAT=`which cat`
WHICH_CUT=`which cut`

if [ ! -f $SLASHSITE ] ; then
	echo "[0;31mNOT[1;m Starting slashd:[0;31m No[1;m $SLASHSITE";
	exit 0;
fi

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

case "$1" in
  start)
    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
				su - $USERNAME -c "TZ=GMT $SLASHD $SERVER_NAME" &
			fi
# this is handled by slashd
#			echo $! > $RUNNING_PID/site/$SITENAME/logs/slashd.pid
			echo "ok PID = $! "
		done
    ;;
  stop)
    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/site/$SITENAME/logs/slashd.pid ] ;then
				echo -n "Stopping slashd $SERVER_NAME: "
				if ! kill `cat $RUNNING_PID/site/$SITENAME/logs/slashd.pid` ;then
					echo -n "...using kill -9..."
					sleep 3
					kill -9 `cat $RUNNING_PID/site/$SITENAME/logs/slashd.pid`
					rm -f $RUNNING_PID/site/$SITENAME/logs/slashd.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

      ;;
  restart)
    cd $DATADIR
		$0 stop
		sleep 2
		$0 start
    ;;
  *)
    echo "Debian Usage: /etc/init.d/$0 {start|stop|restart}"
    echo "Redhat Usage: /etc/rc.d/init.d/$0 {start|stop|restart}"
    echo "Other Usage: /usr/local/sbin/$0 {start|stop|restart}"
    exit 1
    ;;
esac

exit 0 ;
