#! /bin/bash
# Copyright (c) 1995-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Mads Martin Jrgensen <feedback@suse.de>
#
# /etc/init.d/bzflagserver
#
#   and its symbolic link
#
# /usr/sbin/rcbzflagserver
#
# LSB compliant service control script; see http://www.linuxbase.org/spec/
# 
# System startup script for the BZFlag gameserver
#
### BEGIN INIT INFO
# Provides: bzflagserver
# Required-Start: $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: bzflag server daemon
# Description:    Start bzflagserver to provide a server for the popular
#	tank game BZFlag, http://www.bzflag.org
### END INIT INFO
# 
# Note on Required-Start: It does specify the init script ordering,
# not real dependencies. Depencies have to be handled by admin
# resp. the configuration tools (s)he uses.
. /etc/rc.status

# First reset status of this service
rc_reset

if [ -f /etc/sysconfig/bzflagserver ]; then
	. /etc/sysconfig/bzflagserver
else
	echo "Could not find /etc/sysconfig/bzflagserver"
	echo "Please start the bzflagserver manually"
	rc_failed 1
	rc_status -v
fi

# Check for missing binaries (stale symlinks should not happen)
BZFLAGSERVER_BIN=/usr/bin/bzfs
test -x $BZFLAGSERVER_BIN || exit 5

# See /etc/sysconfig/bzflagserver for options to the bzfs
if [ "$BZFLAGSERVER_PUBLIC" = "yes" ] ; then
	PUBLIC="-public"
fi

if [ "$BZFLAGSERVER_NEWBIE" = "yes" ] ; then
	HANDICAP="-handicap"
fi

case "$1" in
    start)
	echo -n "Starting bzflagserver"

	if [ "$BZFLAGSERVER_SRVMSG" ] ; then
		startproc -u nobody -g nobody $BZFLAGSERVER_BIN \
		$BZFLAGSERVER_OPTS $PUBLIC -p "$BZFLAGSERVER_PORT" \
		-world "/usr/share/games/bzflag/maps/$BZFLAGSERVER_WORLD" \
		-srvmsg "$BZFLAGSERVER_SRVMSG" $HANDICAP
	else
		startproc -u nobody -g nobody $BZFLAGSERVER_BIN \
		$BZFLAGSERVER_OPTS $PUBLIC -p "$BZFLAGSERVER_PORT" \
		-world "/usr/share/games/bzflag/maps/$BZFLAGSERVER_WORLD" \
		$HANDICAP
	fi
	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down bzflagserver"
	## Stop daemon with killproc(8) and if this fails
	## set echo the echo return value.

	killproc -TERM $BZFLAGSERVER_BIN

	# Remember status and be verbose
	rc_status -v
	;;
    try-restart)
	## Stop the service and if this succeeds (i.e. the 
	## service was running before), start it again.
	## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
	$0 status >/dev/null &&  $0 restart

	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	$0 stop  &&  $0 start
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service bzflagserver: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $BZFLAGSERVER_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
