#!/bin/bash
### BEGIN INIT INFO
# Provides:          opscenterd
# Required-Start:    $network $local_fs $remote_fs $named $syslog $time
# Required-Stop:     $network $local_fs $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Cassandra cluster manager
### END INIT INFO

# Author: paul cannon <paul@datastax.com>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Cassandra cluster manager"
NAME=opscenterd
SCRIPTNAME="$0"

OPSCENTER_SCRIPT="/usr/share/opscenter/bin/opscenter"
PIDFILE="/var/run/opscenter/opscenter.pid"

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

# We like to let the user override these also
USER="root"
if getent passwd opscenter >/dev/null; then
    USER="opscenter"
fi
MYUID=$(id -u "$USER")
MYGID=$(id -g "$USER")
export OPSC_HOME=$(getent passwd $MYUID | cut -d ':' -f 6)

# Load the VERBOSE setting and other rcS variables
[ -r /lib/init/vars.sh ] && . /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
[ -r /lib/lsb/init-functions ] && . /lib/lsb/init-functions

[ -d /var/run/opscenter ] || mkdir /var/run/opscenter
[ -d /usr/share/opscenter/tmp ] || mkdir /usr/share/opscenter/tmp

export TWISTD_EXTRA="-u $MYUID -g $MYGID --pidfile $PIDFILE"

is_running()
{
    if [ -f "$PIDFILE" ]; then
        PID=$(cat "$PIDFILE" 2>/dev/null)
        grep -q "$TAC" "/proc/$PID/cmdline" 2>/dev/null && return 0
        return 1
    fi
    return 3
}

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started

    is_running && return 1

    setsid $OPSCENTER_SCRIPT || return 2

    return 0
}

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

    is_running || return 1

    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
        --pidfile $PIDFILE
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    rm -f "$PIDFILE"
    return "$RETVAL"
}

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)
    is_running
    stat=$?
    case "$stat" in
      0) log_daemon_msg "$DESC $NAME is running" ;;
      1) log_daemon_msg "could not access pidfile for $DESC $NAME" ;;
      *) log_daemon_msg "$DESC $NAME is not running" ;;
    esac
    exit "$stat"
    ;;
  #reload|force-reload)
    #
    # If do_reload() is not implemented then leave this commented out
    # and leave 'force-reload' as an alias for 'restart'.
    #
    #log_daemon_msg "Reloading $DESC" "$NAME"
    #do_reload
    #log_end_msg $?
    #;;
  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

:
