#!/bin/sh
#
# chkconfig:	345 98 03
# description:	Starts and stops the Crossfire server 
#

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Demon specified configuration.
. /etc/sysconfig/crossfire

# Check that networking is up.
if is_no "${NETWORKING}"; then
        msg_Network_Down crossfire
        exit 1
fi

TMPDIR="/tmp"; export TMPDIR

case "$1" in
    start)
        if [ ! -f /var/lock/subsys/crossfire ]; then
		cd /
		FLAGS="-log /var/log/crossfire"
		if [ -n "$PORT" ] ; then
			FLAGS="$FLAGS -csport $PORT"
		fi
		if is_yes "$DEBUG" ; then
			FLAGS="$FLAGS -d"
		else
			FLAGS="$FLAGS +d"
		fi
		if is_yes "$STAT_LOSS_ON_DEATH" ; then
			FLAGS="$FLAGS -stat_loss_on_death"
		else
			FLAGS="$FLAGS +stat_loss_on_death"
		fi
		if is_yes "$BALANCED_STAT_LOSS" ; then
			FLAGS="$FLAGS -balanced_stat_loss"
		else
			FLAGS="$FLAGS +balanced_stat_loss"
		fi
		if is_yes "$USE_PERMANENT_EXPERIENCE" ; then
			FLAGS="$FLAGS -use_permanent_experience"
		else
			FLAGS="$FLAGS +use_permanent_experience"
		fi
	
		msg_starting crossfire
		daemon su games -c "\"exec crossfire -detach $FLAGS >>/var/log/crossfire 2>&1\"" 
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/crossfire
        else
                msg_Already_Running crossfire
                exit 1
        fi
	;;
    stop)
        if [ -f /var/lock/subsys/crossfire ]; then
                msg_stopping crossfire
		killproc crossfire
		rm -f /var/lock/subsys/crossfire >/dev/null 2>&1
        else
                msg_Not_Running crossfire
                exit 1
        fi
	;;
   status)
    	status crossfire
	su games -c "crossfire +d -s"
	exit $?
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload)
	if [ -f /var/lock/subsys/crossfire ]; then
	        msg_reloading crossfire
	        killproc crossfire
	else
	        msg_Not_Running crossfire
	        exit 1
        fi
	;;
  *)
	msg_Usage "$0 {start|stop|restart|reload|status}"
	exit 1
esac
