#!/bin/bash
#
# minecraft-server.sh { start | stop | status }
#
# $Revision:$
 
PWD=`pwd`
SCRIPT_DIR=$(cd $(dirname $0) && pwd)
PARENT_DIR=`dirname $SCRIPT_DIR`
PROG=$SCRIPT_DIR/minecraft_server.jar
SCRIPT_PID="$SCRIPT_DIR/run_script.pid"
SCRIPT_NAME=`basename "$0"`
 
# kill named processes
killproc() {
    pid=`/bin/ps ax | /bin/grep "$1" | awk '{print $1}'`
    [ "$pid" != "" ] && kill $pid
}
 
case "$1" in
'start')
    if [ -f $SCRIPT_PID ]; then
        PID=`cat $SCRIPT_PID`
        if (ps ax | awk '{print $1}' | /bin/grep $PID >/dev/null); then
            exit
        fi
    fi
 
    echo $$ > $SCRIPT_PID
 
    java -Xmx1024M -Xms1024M -jar $PROG nogui
 
    /bin/rm -f $SCRIPT_PID
    ;;
'stop')
    killproc $PROG
    ;;
'status')
    if [ -f $SCRIPT_PID ]; then
        PID=`cat $SCRIPT_PID`
        if (ps ax | awk '{print $1}' | /bin/grep $PID >/dev/null); then
            echo "$PROG (PID $PID) is running ..."
        else
            echo "$PROG is stopped"
        fi
    else
        echo "$PROG is stopped"
    fi
    ;;
*)
    echo "Usage: $SCRIPT_NAME { start | stop | status }"
    ;;
esac
EOS
