#! /bin/sh
#
# init.d/bnetd
#
### BEGIN INIT INFO
# Provides:       bnetd
# Required-Start: $network
# Required-Stop:  $null
# Default-Start:  3 5
# Default-Stop:   
# Description:    Starts the bnetd server
### END INIT INFO

BNETD_BIN=/usr/sbin/bnetd
BNETD_CONFIG=/etc/pvpgn/bnetd.conf

test -x $BNETD_BIN || { echo "$BNETD_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

test -r $BNETD_CONFIG || {
    echo "$BNETD_CONFIG does not exist";
    if [ "$1" = "stop" ]; then exit 0; else exit 6; fi;
}

FILES="IX86ver1.mpq 
	PMACver1.mpq 
	XMACver1.mpq 
	bnserver-D2DV.ini 
	bnserver-D2XP.ini 
	bnserver-WAR3.ini 
	bnserver.ini 
	icons-WAR3.bni 
	icons.bni 
	icons_STAR.bni"
	
	START=1
	for i in $FILES; do
		if [ ! -f "/var/lib/pvpgn/files/$i" ]; then
			START=0;
		fi
	done

	if [ "$START" = "0" ]; then
		echo -e "\nYou are missing the pvpgn support files."
		echo "Please install the package pvpgn-support"
		exit 0
	fi

. /etc/rc.status
rc_reset

case "$1" in
    start)
	echo -n "Starting bnetd server "
	startproc $BNETD_BIN
	sleep 1s #sleep because the server may crash with incorrect settings
	rc_status -v
	;;
    stop)
	echo -n "Shutting down bnetd server "
	killproc -TERM $BNETD_BIN
	rc_status -v
	;;
    try-restart | condrestart | force-reload)
	## Note: try-restart is now part of LSB (as of 1.9).
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    reload)
	echo -n "Reloading bnetd server unsupported "
	killproc -HUP $BNETD_BIN
	# rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for bnetd server "
	checkproc $BNETD_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
