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

### BEGIN INIT INFO
# Provides:          pe-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: pe-puppetdb
# Description:       Start pe-puppetdb daemon placed in /etc/init.d.
### END INIT INFO

# Copyright 2014 Puppet Labs

# Source function library.
[ -e /lib/lsb/init-functions ] && . /lib/lsb/init-functions

prog="pe-puppetdb"
realname="puppetdb"

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

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

config=$CONFIG

PATH=/sbin:/usr/sbin:/bin:/usr/bin
JARFILE="puppetdb.jar"
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"
lockfile="/var/lock/subsys/${prog}"
PIDFILE="/var/run/puppetlabs/${realname}/${realname}.pid"
LOGFILE="/var/log/puppetlabs/${realname}/${realname}.log"
START_TIMEOUT=${START_TIMEOUT:-300}

# First reset status of this service
rc_reset

print_service_pid() {
    local pid
    if [ ! -d  "/var/run/puppetlabs/${realname}" ] ; then
        install --owner "${USER}" --group "${USER}" --directory "/var/run/puppetlabs/${realname}"
    fi
    pid="$(pgrep -f "${JAVA_BIN}.*${JARFILE}")"
    echo -n "${pid}"
}

start() {
    local service_pid
    [ -x "${JAVA_BIN}" ] || exit 5
    [ -e "${config}" ] || exit 6
    echo -n $"Starting ${prog}: "

    
    # startproc creates logfiles but doesn't set ownership correctly for new
    # files. Let's always do this in case the file ownership is wrong.
    touch "${LOGFILE}"
    chown $USER:$USER "${LOGFILE}"

    export HOME="$(getent passwd ${USER} | cut -d':' -f6)"
    # startproc will change users, so make sure that user has permission
    # to access the present working directory.
    cd "${INSTALL_DIR}"
    startproc -u "${USER}" -l "${LOGFILE}" -p "${PIDFILE}" -- "${JAVA_BIN}" "-XX:OnOutOfMemoryError=\"kill -9 %p\"" -Djava.security.egd=/dev/urandom ${JAVA_ARGS}
    rc_status -v
    retval=$?

    # NOTE: retval holds the return value of `startproc`, not the process
    # started by `startproc`.

    # If rc_status didn't succeed, bail out early without bothering to poll
    # waiting for the application or doing work that assumes a launching state
    if [ "$retval" != 0 ]; then
        log_failure_msg $"${prog} startup"
        echo
        return $retval
    fi

    service_pid="$(print_service_pid)"
    echo -n "${service_pid}" > "${PIDFILE}"

    
    # Wait for the application to become available
    if ! wait_for_app "${service_pid}" "$START_TIMEOUT"; then
        log_failure_msg $"${prog} startup"
        echo
        return 1
    fi

    # if the pid file exists, with the PID in it (non-zero size)
    if [ -s "${PIDFILE}" ]; then
        log_success_msg $"${prog} startup"
        echo
        touch "${lockfile}"
        return 0
    else
        log_failure_msg $"${prog} startup"
        echo
        return 1
    fi
}

stop() {
    echo -n $"Stopping ${prog}: "
    if [ ! -s "${PIDFILE}" ] ; then
        print_service_pid > "${PIDFILE}"
    fi

    killproc -p "${PIDFILE}" -t"${SERVICE_STOP_RETRIES}"s -- "${JAVA_BIN}" "-XX:OnOutOfMemoryError=\"kill -9 %p\"" "${JAVA_ARGS}"
    rc_status -v
    retval=$?

    if [ "$retval" = 0 ]; then
        rm -f "${lockfile}" "${PIDFILE}"
        log_success_msg $"${prog} stopped"
        echo
        return 0
    else
        log_failure_msg $"${prog} could not be stopped, check ${LOGFILE}"
        echo
        return 1
    fi
}

restart() {
    stop
    start
}

sl_status_q() {
  sl_status > /dev/null 2>&1
}

sl_status() {
    checkproc -p "${PIDFILE}" -- "${JAVA_BIN}" "-XX:OnOutOfMemoryError=\"kill -9 %p\"" "${JAVA_ARGS}"
    rc_status -v
}


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