#! /bin/sh
### BEGIN INIT INFO
# Provides:          puppetdb
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      
# Should-Stop:       
# Short-Description: puppetdb
# Description:       Start puppetdb daemon placed in /etc/init.d.
### END INIT INFO

# Copyright 2014 Puppet Labs


# You should only need to edit the default/puppetdb file and not
#   this init script directly
if [ -r "/etc/default/puppetdb" ] ; then
    . /etc/default/puppetdb
else
    exit 1
fi

[ -e "$INSTALL_DIR/ezbake-functions.sh" ] && . "$INSTALL_DIR/ezbake-functions.sh"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=puppetdb
REALNAME=puppetdb
DESC="puppetdb Puppet Labs version-checking backend"
JARFILE="puppetdb.jar"
PIDFILE=/var/run/puppetlabs/${REALNAME}/${REALNAME}.pid
SCRIPTNAME=/etc/init.d/$NAME
START_TIMEOUT=${START_TIMEOUT:-300}

JAVA_ARGS="${JAVA_ARGS} -cp '${INSTALL_DIR}/${JARFILE}' clojure.main -m puppetlabs.puppetdb.main --config ${CONFIG} -b '${BOOTSTRAP_CONFIG}'"
EXTRA_ARGS="--chuid $USER --background --make-pidfile"
EXEC="$JAVA_BIN -XX:OnOutOfMemoryError=\"kill -9 %p\" -Djava.security.egd=/dev/urandom $JAVA_ARGS"

# Exit if the package is not installed
[ -x "$JAVA_BIN" ] || 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

#
# Function that starts the daemon/service
#
do_start()
{
    
    if [ ! -d  "/var/run/puppetlabs/$NAME" ] ; then
      mkdir -p /var/run/puppetlabs/$NAME
      chown -R $USER:$GROUP /var/run/puppetlabs/$NAME
    fi

    start-stop-daemon --start $EXTRA_ARGS --quiet --oknodo --pidfile $PIDFILE --chdir $INSTALL_DIR --exec $JAVA_BIN \
      --startas /bin/bash -- -c "exec $EXEC >> /var/log/puppetlabs/${REALNAME}/${REALNAME}-daemon.log 2>&1"
    retval=$?

    if [ "$retval" -ne 0 ] \
        || ! wait_for_pidfile $PIDFILE 5 \
        || ! wait_for_app $(cat $PIDFILE) $START_TIMEOUT ;then
        retval=2
    fi

    
    return $retval
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped or was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --oknodo --retry=TERM/${SERVICE_STOP_RETRIES}/KILL/5 --pidfile $PIDFILE --exec $JAVA_BIN
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

#
# Function that gets the status of the daemon/service
#
get_status()
{
    status_of_proc -p $PIDFILE "$JAVA_BIN" "$NAME"
    return $?
}

#
# Function that gets the status of the daemon/service
# and makes no output
#
get_status_q()
{
    get_status >/dev/null 2>&1
}


#
# Function that restarts the daemon/service
#
do_restart()
{
    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
}

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)
        get_status
        exit $?
        ;;
    condrestart|try-restart)
        get_status_q || exit 0
        do_restart
        ;;
    restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
        do_restart
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|condrestart|try-restart|restart|force-reload}" >&2
        exit 3
        ;;
esac

:
