#!/bin/bash

### BEGIN INIT INFO
# Provides:          scalaris
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     3 5
# Default-Stop:
# Short-Description: Scalaris node
# Description:       Default Scalaris node. http://code.google.com/p/scalaris/
### END INIT INFO

# Source function library.
if [ -e /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    . /etc/init.d/functions
fi

prefix=/usr
exec_prefix=${prefix}
datarootdir=${prefix}/share
SCREEN=/usr/bin/screen
SUDO=/usr/bin/sudo
RUNUSER=

# Source config.
if [ -e /etc/scalaris/initd.conf ]; then
    . /etc/scalaris/initd.conf
fi

SCALARIS_NODE=${SCALARIS_NODE-"node"}

SCALARIS_USER=scalaris

PID_DS="/var/run/scalaris_${SCALARIS_NODE}.pid"

LOG_DS="/var/log/scalaris/initd_${SCALARIS_NODE}.log"

SCALARISCTL="${exec_prefix}/bin/scalarisctl -n ${SCALARIS_NODE} -d --screen -l /var/log/scalaris ${SCALARIS_ADDITIONAL_PARAMETERS}"

# For SELinux we need to use 'runuser' not 'sudo'
if [ -n "$RUNUSER" -a -x "$RUNUSER" ]; then
    SU="$RUNUSER"
    SU_CMD="$RUNUSER $SCALARIS_USER -s /bin/bash -c"
else
    SU="$SUDO"
    SU_CMD="$SUDO -u $SCALARIS_USER -H /bin/bash -c"
fi

start() {
    if [ -f "$PID_DS" ]; then
        PROCPID=`cat "$PID_DS"`
        if [ -e "/proc/$PROCPID" ];then
            echo "Scalaris node \"${SCALARIS_NODE}\" already started"
            return 0
        fi
    fi

    echo >> "$LOG_DS"
    date >> "$LOG_DS"
    echo -e "Starting Scalaris node \"${SCALARIS_NODE}\"...\n\n" >> "$LOG_DS"
    echo -n "Starting Scalaris node \"${SCALARIS_NODE}\"... "
    $SU_CMD "$SCALARISCTL start" >> "$LOG_DS" 2>&1
    if [ $? -ne 0 ]; then
        echo "failed"
        return 1 # generic or unspecified error
    fi
    sleep 1s
    $SU_CMD "$SCREEN -ls | grep \"scalaris_${SCALARIS_NODE}\" | cut -d'.' -f1 | head -n1 | xargs echo" > "$PID_DS"

    if [ -e "/proc/$PROCPID" ]; then
        echo "success"
    else
        echo "failed"
        return 1
    fi

    return 0
}

gstop() {
    result=0
    if [ -f "$PID_DS" ]; then
        echo -n "Shutting down Scalaris node \"${SCALARIS_NODE}\"... "
        PROCPID=`cat "$PID_DS"`
        if [ -e "/proc/$PROCPID" ];then
            $SU_CMD "$SCALARISCTL gstop"
            result=$?
        else
            # not running anymore
            result=0
        fi
        if [ $result -eq 0 ]; then
            rm -f "$PID_DS"
            echo "success"
        else
            echo "failed"
        fi
    else
        echo "Scalaris node \"${SCALARIS_NODE}\" has not been running"
    fi

    return $result
}

stop() {
    result=0
    if [ -f "$PID_DS" ]; then
        echo -n "Killing Scalaris node \"${SCALARIS_NODE}\"... "
        killproc -p "$PID_DS" "$SCREEN"
        result=$?
        if [ $result -eq 0 ]; then
            rm -f "$PID_DS"
            echo "success"
        else
            echo "failed"
        fi
    else
        echo "Scalaris node \"${SCALARIS_NODE}\" has not been running"
    fi

    return $result
}

status() {
    if [ -f "$PID_DS" ]; then
        PROCPID=`cat "$PID_DS"`
        if [ ! -e "/proc/$PROCPID" ];then
            echo "Scalaris node \"${SCALARIS_NODE}\" has crashed"
            return 1 # program is dead and /var/run pid file exists
        else
            echo -n "Checking Scalaris node \"${SCALARIS_NODE}\"... "
            $SU_CMD "$SCALARISCTL status"
            if [ $? -ne 0 ]; then
                echo "probably crashed"
                return 4 # program or service status is unknown
            fi
            echo "running"
            return 0
        fi
    else
        echo "Scalaris node \"${SCALARIS_NODE}\" is not running"
        return 3 # program is not running
    fi
}

restart() {
    gstop && sleep 1 && start
    return $?
}

# See how we were called.
case "$1" in
    start)
        start
        result=$?
        ;;
    stop)
        gstop
        result=$?
        ;;
    kill)
        stop
        result=$?
        ;;
    restart)
        restart
        result=$?
        ;;
    try-restart)
        ## Stop the service and if this succeeds (i.e. the
        ## service was running before), start it again.
        if [ `$0 status >/dev/null` ]; then
          $0 restart
          result=$?
        else
          result=0
        fi
        ;;
    reload)
        result=3
        ;;
    force-reload)
        restart
        result=$?
        ;;
    status)
        status
        result=$?
        ;;
    *)
        echo -e "Usage: $0 {start|stop|kill|restart|try-restart|reload|force-reload|status}\n"
        result=1
        ;;
esac

exit $result
