#! /bin/bash
### BEGIN INIT INFO
# Provides:          riak
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Riak is a distributed data store
# Description:       Riak is a distributed data store
### END INIT INFO

NAME=riak
DAEMON=/usr/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
RUNDIR=/var/run/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
. /lib/lsb/init-functions

# `service` strips all environmental VARS so
# if no HOME was set in /etc/default/$NAME then set one here
# to the data directory for erlexec's sake
if [ -z "$HOME" ]; then
    export HOME=/var/lib/riak
fi

#
# Function that starts the daemon/service
#
do_start()
{
    if [ ! -d $RUNDIR ]; then
        mkdir $RUNDIR
        chown riak:riak $RUNDIR
    fi
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started

    # Startup with the appropriate user
    start-stop-daemon --start \
        --name riak \
        --user riak \
        --exec $DAEMON -- start \
        || return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Identify the erts directory
    ERTS_PATH=`$DAEMON ertspath`

    # Attempt a clean shutdown.
    $DAEMON stop

    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    # Make sure it's down by using a more direct approach
    start-stop-daemon --stop \
                      --quiet \
                      --retry=TERM/30/KILL/5 \
                      --user riak \
                      --exec $ERTS_PATH/run_erl
    return $?
}

#
# Function that graceful reload the daemon/service
#
do_reload() {
    # Restart the VM without exiting the process
    $DAEMON restart && return $? || return 2
}

# Checks the status of a node
do_status() {
    $DAEMON ping && echo $"$NAME is running" && return 0
    echo $"$NAME is stopped" && return 2
}

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $NAME"
        $DAEMON ping >/dev/null 2>&1 && echo $"$NAME is already running" && exit 0
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1
                exit 1
                ;;
        esac
        ;;
    stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $NAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1
                exit 1
                ;;
        esac
        ;;
    ping)
        # See if the VM is alive
        $DAEMON ping || exit $?
        ;;
    reload|force-reload)
        log_daemon_msg "Reloading $NAME"
        do_reload
        ES=$?
        log_end_msg $ES
        exit $ES
        ;;
    restart)
        log_daemon_msg "Restarting $NAME"
        do_stop
        case "$?" in
            0|1)
                do_start
                case "$?" in
                    0) log_end_msg 0 ;;
                    1) log_end_msg 1 && exit 1 ;; # Old process is still running
                    *) log_end_msg 1 && exit 1 ;; # Failed to start
                esac
                ;;
            *)
                # Failed to stop
                log_end_msg 1 && exit 1
                ;;
        esac
        ;;
    status)
        do_status && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|ping|restart|force-reload|status}" >&2
        exit 3
        ;;
esac

:
