#!/bin/bash
#
# xpilot-server	: start or stop the xpilot server
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#		Modified for Debian GNU/Linux xpilot-server package
#		by Matt Kern <matt.kern@pobox.com>
#		and by Ben Armstrong <synrg@sanctuary.nslug.ns.ca>

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

test -x /usr/games/start-xpilots || exit 0

USER=xpilots

set -e

source /etc/xpilots.conf

case "$1" in
  start)
	echo "$AUTOSTART" |grep -iq yes || exit 0
	test -f "$DEFAULTMAP" || exit 0

	echo -n "Starting xpilot server: "
	start-stop-daemon --start --quiet --user $USER --name xpilots \
		--oknodo --exec /usr/games/start-xpilots
	echo "xpilots."
	;;
  stop)
	echo -n "Stopping xpilot server: "
	if start-stop-daemon --stop --quiet --user $USER --name xpilots \
		--oknodo --retry 30 >/dev/null 2>&1; then
	   echo "xpilots."
        else
           echo "system-wide xpilots not running."
	fi
	;;
  restart|force-reload)
	echo -n "Restarting xpilot server: "
	start-stop-daemon --stop --quiet --user $USER --name xpilots \
		--oknodo --retry 30 >/dev/null 2>&1 || true

	echo "$AUTOSTART" |grep -iq yes || exit 0
	test -f "$DEFAULTMAP" || exit 0

	start-stop-daemon --start --quiet --user $USER --name xpilots \
		--oknodo --exec /usr/games/start-xpilots
	echo "xpilots."
	;;
  *)
	N=/etc/init.d/xpilot-server
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
