#!/bin/sh

### BEGIN INIT INFO
# Provides:          kafka
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Kafka
# Description:       Publish-subscribe messaging rethought as a distributed commit log.
#
### END INIT INFO

# Author: Aleksey Morarash <aleksey.morarash@gmail.com>

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Publish-subscribe messaging rethought as a distributed commit log."
SCRIPTNAME=/etc/init.d/$NAME

NAME=kafka

DAEMON=/usr/lib/kafka/bin/kafka-server-start.sh
DAEMON_ARGS="/etc/kafka/server.properties"
LOGFILE=/var/log/kafka/$NAME.log
PIDFILE=/var/lib/kafka/$NAME.pid

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

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

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

do_start(){
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    check_caller_user_id
    do_status && return 1
    su -s /bin/bash \
        -c "nohup $DAEMON $DAEMON_ARGS > $LOGFILE 2>&1 & echo \$! > $PIDFILE" \
        $NAME > /dev/null 2>&1 || return 2
}

do_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
    check_caller_user_id
    start-stop-daemon --quiet --pidfile=$PIDFILE --user=$NAME \
        --retry=TERM/5/KILL/2 \
        --stop > /dev/null
    case "$?" in
        0)
            do_status
            case "$?" in
                0) return 2 ;;
                4) return 4 ;;
            esac
            ;;
        1) return 1 ;;
        2) return 2 ;;
    esac
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return 0
}

do_status(){
    # Return
    #   0 if program is running.
    #   1 if program is not running and the pid file exists.
    #   3 if program is not running.
    #   4 unable to determine program status.
    start-stop-daemon --quiet --pidfile=$PIDFILE --user=$NAME \
        --status > /dev/null
}

check_caller_user_id(){
    MYID=`id -u`
    if [ "$MYID" != 0 ]; then
        echo "Error: you must be root to do this" >&2
        exit 1
    fi
}

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    status)
        do_status
        RETVAL=$?
        case "$RETVAL" in
            0) log_success_msg "$NAME is running" ;;
            4) log_failure_msg "unable to get status for $NAME" ;;
            *) log_failure_msg "$NAME is not running" ;;
        esac
        return $RETVAL
        ;;
    restart|force-reload)
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                do_start
                case "$?" in
                    0) log_end_msg 0 ;;
                    1) log_end_msg 1 ;; # Old process is still running
                    *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
            *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac

:
