#! /bin/sh
### BEGIN INIT INFO
# Provides:          puppetserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: puppetserver
# Description:       Start puppetserver 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/puppetserver file and not
#   this init script directly
if [ -r "/etc/default/puppetserver" ] ; then
    . /etc/default/puppetserver
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=puppetserver
REALNAME=puppetserver
USER=puppet
GROUP=puppet
DESC="puppetserver Puppet Labs version-checking backend"
JARFILE="puppet-server-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 $?
