#!/bin/sh

USER=minecraft
MINECRAFT_SERVER_PATH=/home/minecraft/minager/
MINECRAFT_SHELL=minager.sh

case "$1" in
    'start')
        sudo -H -u $USER bash -c "$MINECRAFT_SERVER_PATH/$MINECRAFT_SHELL start"
        ;;
    'stop')
        sudo -H -u $USER bash -c "$MINECRAFT_SERVER_PATH/$MINECRAFT_SHELL stop"
        ;;
    'status')
        sudo -H -u $USER bash -c "$MINECRAFT_SERVER_PATH/$MINECRAFT_SHELL status"
        ;;
    'restart')
        sudo -H -u $USER bash -c "$MINECRAFT_SERVER_PATH/$MINECRAFT_SHELL restart"
        ;;
    *)
        # If no argument, we launch the app in case of server startup
        sudo -H -u $USER bash -c "$MINECRAFT_SERVER_PATH/$MINECRAFT_SHELL start &>/dev/null"
        # And show the script usage
        echo "Usage: /etc/init.d/minecraft {start|stop|status|restart}\n" >&2
        exit 3
        ;;
esac
exit 0
