#!/bin/bash
#
# Puppet Labs puppetserver
#
# chkconfig: - 70 10
# description: Puppet Labs puppetserver

### 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

# Source function library.
. /etc/rc.d/init.d/functions

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

prog="puppetserver"
realname="puppetserver"

##########################################
#  You should not have to edit this init script.
#  Please attempt to make changes in /etc/sysconfig/puppetserver
##########################################

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

config=$CONFIG

if [ -d "/run" ]; then
  rundir=/run/puppetlabs
else
  rundir=/var/run/puppetlabs
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin
JARFILE="puppet-server-release.jar"
lockfile=/var/lock/subsys/$prog
PIDFILE="${rundir}/${realname}/${realname}.pid"
START_TIMEOUT=${START_TIMEOUT:-300}

find_my_pid() {
    pid=`pgrep -f "${JARFILE}"`
    /usr/bin/install --directory --owner=${USER} --group=${GROUP} --mode=755 "${rundir}/${realname}"
    [ -n "$pid" ] && echo $pid > $PIDFILE
}

start() {
    # call status here and figure out current state
    rh_status_q
    [ -x $JAVA_BIN ] || exit 5
    [ -e $config ] || exit 6
    # Move any heap dumps aside
    echo -n $"Starting $prog: "

    
    
    pushd "${INSTALL_DIR}" &> /dev/null
    daemon --user $USER --pidfile $PIDFILE "${INSTALL_DIR}/bin/${realname} start >> /var/log/puppetlabs/${realname}/${realname}-daemon.log 2>&1"
    retval=$?
    popd &> /dev/null
    [ -s $PIDFILE ] && success $"$base startup" || failure $"$base startup"
    echo
    [ -s $PIDFILE ] && touch $lockfile

    
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    "${INSTALL_DIR}/bin/${realname}" stop
    retval=$?

    [ $retval -eq 0 ] && success $"$base stopped" || failure $"$base stopped"
    echo
    [ $retval -eq 0 ] && rm -f $lockfile $PIDFILE
    return $retval
}

restart() {
    stop && start
}

rh_status() {
    find_my_pid
    status -p $PIDFILE $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

reload() {
    echo -n $"Reloading $prog: "
    "${INSTALL_DIR}/bin/${realname}" reload
    RETVAL=$?

    [ $RETVAL -eq 0 ] && success $"$base reloaded" || failure $"$base reloaded"
    echo
    return $RETVAL
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    reload)
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|try-restart|reload|status}"
        exit 2
esac
exit $?
