#!/bin/bash
#
# openshift-console Startup script for the OpenShift Enterprise Management Console
#
# chkconfig: - 85 15
# description: The OpenShift Enterprise Management Console
# processname: httpd
# config: /var/www/openshift/console/httpd/console.conf
# config: /var/www/openshift/console/httpd
# pidfile: /var/www/openshift/console/httpd/run/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop openshift console
# Description: The openshift console
### END INIT INFO

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

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=openshift-console
pidfile=${PIDFILE-/var/www/openshift/console/httpd/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/openshift-console}
RETVAL=0

OPTIONS="-C 'Include /var/www/openshift/console/httpd/console.conf' -f /var/www/openshift/console/httpd/httpd.conf"

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        pushd /var/www/openshift/console >/dev/null 2>&1
        rm -rf Gemfile.lock
        scl enable ruby193 "bundle install --local" > /dev/null
        chown apache:apache Gemfile.lock
        popd >/dev/null 2>&1
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
	echo -n $"Stopping $prog: "
        if [ -e ${pidfile} ]; then
          killproc -p ${pidfile} -d 10 $httpd
        else
          # BZ876937
          echo -n "(already stopped)"
          success
        fi
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    eval "set -- $OPTIONS"
    if ! LANG=$HTTPD_LANG $httpd "$@" -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
	else
	    success
	fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart|try-restart)
	if status -p ${pidfile} $httpd >&/dev/null; then
		stop
		start
	fi
	;;
  force-reload|reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
	RETVAL=2
esac

exit $RETVAL
