#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          spunkybot
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Initscript for Spunky Bot
# Description:       Start/Stop/Restart the Spunky Bot service daemon
### END INIT INFO

set -e

# Get LSB functions
. /lib/lsb/init-functions

# Installation:
# Move this file to /etc/init.d/spunkybot
# Make it executable: sudo chmod +x /etc/init.d/spunkybot
# Start at boot: sudo update-rc.d spunkybot defaults 80 10
# Manual start: sudo /etc/init.d/spunkybot start

# Change the next 3 lines to suit where you installed Spunky Bot, and which user is running the Urban Terror server
DIR=/opt/spunkybot
USER=q3ut4
GROUP=q3ut4

# Change the name, if you want to run multiple instances of Spunky Bot
NAME=spunkybot

# Don't modify the code below
DAEMON=$DIR/spunky.py
RUNDIR=/var/run/$NAME
PIDFILE=$RUNDIR/$NAME.pid

# See if the daemon is present
if [ -e "$DAEMON" ]; then
    chmod +x "$DAEMON"
    chown -R "$USER:$GROUP" "$DIR"
fi

# Make sure /var/run/spunkybot exists
test -d "$RUNDIR" || {
    mkdir -p "$RUNDIR"
    chown -R "$USER:$GROUP" "$RUNDIR"
}

case "$1" in
    start)
        if [ -e "$PIDFILE" ]; then
            echo "$0:"
            echo "  Another instance of \`${DAEMON##*/}' seems to be running."
            echo "  If this is not the case, please remove \"$PIDFILE\"."
            exit 1
        fi

        log_begin_msg "Starting service $NAME..."
        umask 002
        if start-stop-daemon --start --quiet --background --chdir $DIR --chuid $USER:$GROUP --pidfile "$PIDFILE" --make-pidfile --exec $DAEMON; then
            log_end_msg 0
        else
            log_end_msg $?
        fi
        ;;

    stop)
        log_begin_msg "Stopping service $NAME..."
        if start-stop-daemon --stop --quiet --pidfile "$PIDFILE" && rm -f "$PIDFILE"; then
            log_end_msg 0
        else
            log_end_msg $?
        fi
        ;;

    restart|force-reload)
        $0 stop
        sleep 3
        $0 start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|force-reload}"
        exit 1
        ;;
esac
exit 0
