#!/bin/sh
# Barry Alcock <alcockba@educk.globalnet.co.uk>
# 18/Jul/2000

# ================================================================================
# This software is distributed under the GNU Public License V2 and comes with
# no warranty what so ever.
# ================================================================================

# make /etc/rc.d/rc run us without dumping all of
# the output into /var/log/messages
# /etc/rc.d/rc looks for 'daemon ' or 'action '

# Get some nice RedHat functions
. /etc/rc.d/init.d/functions

HLDS_HOME=/home/halflife
HLDS_CONF=/etc/hlds.conf

# Set LOGS to "1" for generate logs, "" for no logs
LOGS=1

case "$1" in
	start)
		# Start daemon
		echo -n "Starting halflife servers: "

		ulimit -cHS 0
		cd $HLDS_HOME
		./hldsfw -c $HLDS_CONF
		echo -n "hldsfw "

		TS=`date +%Y%m%d-%H%M`
		for game in `cat $HLDS_CONF | awk -F: '{ print $1 }'` ; do
			max=`grep "^$game:" $HLDS_CONF | cut -d: -f2`
			addr=`grep "^$game:" $HLDS_CONF | cut -d: -f3`
			port=`grep "^$game:" $HLDS_CONF | cut -d: -f4`
			addr=`nslookup $addr | tail -2l | awk '{ print $2 }'`
			if [ "$LOGS" = "1" ] ; then
				/usr/bin/nohup 2>&1 >/var/log/hlds/$game-$TS ./hlds_l -nomaster -game $game -port $port +sv_lan 1 +ip $addr +maxplayers $max &
			else
				bin/nohup 2>&1 >/dev/null ./hlds_l -nomaster -game $game -port $port +sv_lan 1 +ip $addr +maxplayers $max &
			fi
			echo -n "$game "
		done
		success "hlds_l startup"
		echo
		;;

	stop)
		# Stop daemon
		echo -n "Shutting down halflife servers: "

		killproc hlds_l
		killproc hldsfw
		echo
		;;

	restart)
		$0 stop
		$0 start
		;;

	status)
		status hlds_l
		status hldsfw
		;;

	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit 0
