#! /bin/sh
### BEGIN INIT INFO
# Provides:          cgaserver
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: CGA Server 
# Description:       Classic Gaming Arena Dedicated Server
### END INIT INFO

# Author: Joe Petrie <joe.petrie@cdiware.com>

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="CGA Server"
NAME=cgaserver
DAEMON=/usr/sbin/cga/server/$NAME
export PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
export CONFIG_PATH=/etc/cgaserver

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

check_status()
{
   if [ ! -r "$PIDFILE" ]; then
      test "$1" != -v || echo "$NAME is not running."
      return 3
   fi
   if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
      test "$1" != -v || echo "$NAME is running."
      return 0
   else
      test "$1" != -v || echo "$NAME is running but $PIDFILE exists."
      return 1
   fi
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "($NAME)"
        start-stop-daemon --start --quiet --exec $DAEMON

        sleep 2

        if check_status -q; then
           log_end_msg 0
        else
           log_failure_msg "check syslog for diagnostics."
           log_end_msg 1
           exit 1
        fi
        ;;
  stop)
	log_daemon_msg "Stopping $DESC" "($NAME)"
	start-stop-daemon --stop --quiet --pidfile $PIDFILE
        log_end_msg $?
	;;
  status)
       echo -n "Status of $DESC: "
       check_status -v
       exit "$?"
       ;;
  restart|force-reload)
        $0 stop
        sleep 2
        $0 start
        if [ "$?" != "0" ]; then
           exit 1
        fi
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop}" >&2
	exit 3
	;;
esac

exit 0
