#! /bin/sh

set -e

SELFNAME=${0##*/}

BASEPATH=/var/lib/quake3
WRAPPER=/usr/games/ioquake3-server
DAEMON=/usr/lib/games/ioquake3/ioq3ded.bin
OPTIONS="+set com_hunkmegs 24 +exec server.cfg +exec maps.cfg"
RUNAS=quake3:games
PIDFILE=/var/run/ioquake3-server.pid

test -x $WRAPPER || exit 0

export HOME=$BASEPATH

case "$1" in
  start)
    echo -n "Starting ioquake3-server"
    start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDFILE \
            --nicelevel -10 --make-pidfile --background --chdir $HOME \
            --exec $DAEMON --startas $WRAPPER -- $OPTIONS
    echo "."
    ;;
  stop)
    echo -n "Stopping ioquake3-server"
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
            --exec $DAEMON
    echo "."
    ;;
  restart|force-reload)
    $0 stop
    sleep 3
    $0 start
    ;;
  *)
    echo "Usage: $SELFNAME {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
