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

# Copyright 2014 Puppet Labs

#set default privileges to -rw-r-----
umask 027

# You should only need to edit the default/pe-workflow-service file and not
#   this init script directly
if [ -r "/etc/default/pe-workflow-service" ] ; then
    . /etc/default/pe-workflow-service
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=pe-workflow-service
REALNAME=workflow-service
USER=pe-workflow-service
GROUP=pe-workflow-service
DESC="pe-workflow-service Puppet Labs version-checking backend"
JARFILE="pe-workflow-service-release.jar"
PIDFILE=/run/puppetlabs/${REALNAME}/${REALNAME}.pid
SCRIPTNAME=/etc/init.d/$NAME
START_TIMEOUT=${START_TIMEOUT:-300}

# 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()
{
    
    
    /usr/bin/install --directory --owner=${USER} --group=${GROUP} --mode=755 "/run/puppetlabs/${REALNAME}"
    start-stop-daemon --start --quiet --chuid $USER --oknodo --pidfile $PIDFILE --chdir $INSTALL_DIR \
      --startas "${INSTALL_DIR}/bin/${REALNAME}" -- start >> /var/log/puppetlabs/${REALNAME}/${REALNAME}-daemon.log 2>&1
    retval=$?

    
    return $retval
}

#
# Function that stops the daemon/service
#
do_stop()
{
    "${INSTALL_DIR}/bin/${REALNAME}" stop
}

#
# 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
    if [ "$?" -eq 0 ]; then
      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
    else
      # Failed to stop
      log_end_msg 1
    fi
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    "${INSTALL_DIR}/bin/${REALNAME}" reload
    return $?
}

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        RETVAL="$?"
        case "$RETVAL" in
            0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            1|2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        exit "$RETVAL"
        ;;
    stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        RETVAL="$?"
        if [ "$RETVAL" -eq 0 ]; then
            [ "$VERBOSE" != no ] && log_end_msg 0
        else
            [ "$VERBOSE" != no ] && log_end_msg 1
        fi
        exit "$RETVAL"
        ;;
    status)
        get_status
        exit $?
        ;;
    condrestart|try-restart)
        get_status_q || exit 0
        do_restart
        ;;
    restart)
        [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
        do_restart
        ;;
    force-reload|reload)
        [ "$VERBOSE" != no ] && log_daemon_msg "Reloading $DESC" "$NAME"
        do_reload
        RETVAL="$?"
        [ "$VERBOSE" != no ] && log_end_msg $RETVAL
        exit "$RETVAL"
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|condrestart|try-restart|restart|force-reload|reload}" >&2
        exit 3
        ;;
esac

exit $?
