#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

DESC="The Minecraft server"
NAME='minecraft-server'
MINECRAFT_HOME='/home/minecraft'
DAEMON_HOME="$MINECRAFT_HOME/server"
DAEMON="$DAEMON_HOME/$NAME.jar"
PIDFILE="$DAEMON_HOME/$NAME.pid"
SCRIPTNAME="$DAEMON_HOME/$0"
JAVA_OPT='-Xms=512M -Xmx=2048M'

# ***********************************************
# Normal functions for bash commands.
# ***********************************************
start()
{
    # If PIDFILE exists and PID is in running processes
    if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
    then
        echo 'Service already running\n'
    else    
        echo "Starting service $NAME"
	cd $DAEMON_HOME
        nohup 2>/dev/null java $JAVA_OPT -jar $DAEMON &>/dev/null &
        expr $! - 1 > $PIDFILE
        echo "Service started [`cat $PIDFILE`]\n"
    fi
}

stop()
{
    # If PIDFILE doesn't exists or PID isn't in running processes
    if [ ! -f "$PIDFILE" ] || ! kill -0 `cat "$PIDFILE"`
    then
        echo 'Service not running\n'
    else
        echo 'Stopping service...'
        # Send signal to end to the process
        kill -15 `cat "$PIDFILE"` && rm -f "$PIDFILE"
        echo 'Service stopped\n'
    fi
}

status()
{
    if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
    then
        echo "Service is running (${GREEN}● active${NC})\n"
    else
        echo "Service not running (${RED}● inactive${NC})\n"
    fi
}

# ***********************************************
# Commands used by Minager to drive the server.
# ***********************************************
api_check_error()
{
    if [ $1 != 0 ]
    then
        exit 1
    fi
}

api_status()
{
    if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
    then
        echo 1 # Running
    else
        echo 0 # Stopped
    fi
}

api_start() {
    # If PIDFILE exists and PID is in running processes
    if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
    then
        exit 2 # STATE_UNCHANGED
    else    
	    cd $DAEMON_HOME
        api_check_error $?

        nohup 2>/dev/null java $JAVA_OPT -jar $DAEMON &>/dev/null &
        api_check_error $?

        expr $! - 1 > $PIDFILE
        api_check_error $?
    fi
}

api_stop()
{
    # If PIDFILE doesn't exists or PID isn't in running processes
    if [ ! -f "$PIDFILE" ] || ! kill -0 `cat "$PIDFILE"`
    then
        exit 2 # STATE_UNCHANGED
    else
        # Send signal to end to the process
        kill -15 `cat "$PIDFILE"` && rm -f "$PIDFILE"
        api_check_error $?
    fi
}

api_restart()
{
    # Stop if running
    if [ $(api_status) = 1 ]
    then
        kill -15 `cat "$PIDFILE"` && rm -f "$PIDFILE"
        api_check_error $?
    fi

    sleep 5
    api_start
}

case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'status')
        status
        ;;
    'restart')
        stop
        sleep 5
        start
        ;;
    'api_status')
        api_status
        ;;
    'api_start')
        api_start
        ;;
    'api_stop')
        api_stop
        ;;
    'api_restart')
        api_restart
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}\n" >&2
        exit 3
        ;;
esac
exit 0
