#!/bin/sh
### BEGIN INIT INFO
# Provides:          communism
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: management of virtual resources
# Description:       Networking management system of virtual resources.
### END INIT INFO

# Author: Ilyin Alexei <geolog@yandex-team.ru>

NAME="communism"
SCRIPTNAME=/etc/init.d/$NAME

[ -r /etc/default/$NAME ] && . /etc/default/$NAME || true

: ${ENABLED:="no"}
: ${DAEMON:="/Berkanavt/communism/bin/solver"}
: ${USERNAME:="lenin"}
: ${PORT:="3366"}
: ${SOCKET:="/Berkanavt/communism/socket"}
: ${LOGFILE:="/Berkanavt/communism/solver.out"}
: ${PIDFILE:="/Berkanavt/communism/solver.pid"}
: ${LIMITS:="/Berkanavt/communism/limits/limits.txt"}
: ${EXTRA_ARGS:=""}
: ${DAEMON_ARGS:="-r -p $PORT -u $SOCKET -l $LOGFILE -P $PIDFILE -k $LIMITS $EXTRA_ARGS"}

[ "$ENABLED" = "yes" ] || exit 0
[ -x "$DAEMON" ] || exit 0

do_status() {
    # Return:
    #   0   Program is running.
    #   1   Program is not running and the pid file exists.
    #   3   Program is not running.
    #   4   Unable to determine program status.
    start-stop-daemon --quiet --status --name $(basename $DAEMON) --pidfile $PIDFILE
}

do_start() {
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    do_status
    case "$?" in
        0) return 1 ;;
        1) rm -f $PIDFILE ;;
        4) return 2 ;;
    esac
    start-stop-daemon --quiet --start --chuid $USERNAME \
        --exec $DAEMON --pidfile $PIDFILE -- $DAEMON_ARGS || return 2
}

do_stop() {
    # Return
    #   0 if daemon has been stopped or was already stopped
    #   2 if daemon could not be stopped
    if start-stop-daemon --quiet --oknodo --stop --retry 5 --exec $DAEMON --pidfile $PIDFILE; then
        rm -f $PIDFILE
        return 0
    fi
    return 2
}

case "$1" in
    status)
        echo -n "Determining status of $NAME..."
        do_status
        case "$?" in
            0) echo "OK";                                           exit 0 ;;
            1) echo "FAILED (process is dead but pid file exists)"; exit 1 ;;
            3) echo "FAILED";                                       exit 1 ;;
            4) echo "FAILED (unable to determine program status)";  exit 1 ;;
        esac
        ;;
    start)
        echo -n "Starting $NAME..."
        do_start
        case "$?" in
            0|1) echo "OK";     exit 0 ;;
            2)   echo "FAILED"; exit 1 ;;
        esac
        ;;
    stop)
        echo -n "Stopping $NAME..."
        do_stop
        case "$?" in
            0) echo "OK";     exit 0 ;;
            2) echo "FAILED"; exit 1 ;;
        esac
        ;;
    restart|force-reload)
        echo -n "Restarting $NAME..."
        do_stop
        case "$?" in
            0)
                do_start
                case "$?" in
                    0) echo "OK";                   exit 0 ;;
                    1) echo "FAILED (can't stop)" ; exit 1 ;;
                    *) echo "FAILED (can't start)"; exit 1 ;;
                esac
                ;;
            *) echo "FAILED"; exit 1 ;;
        esac
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac

