#!/bin/bash
#
# openshift-broker        Startup script for the OpenShift Origin broker
#
# chkconfig: - 85 15
# description: The OpenShift Origin broker
# processname: httpd
# config: /var/www/openshift/broker/httpd/broker.conf
# config: /var/www/openshift/broker/httpd
# pidfile: /var/www/openshift/broker/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 Origin broker
# Description: The OpenShift Origin broker
### END INIT INFO

. /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-broker
osdir=/var/www/openshift/broker/
pidfile=${PIDFILE-$osdir/httpd/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/openshift-broker}
RETVAL=0

OPTIONS="-C 'Include $osdir/httpd/broker.conf' -f $osdir/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() {
        echo -n $"Starting $prog: "
        pushd $osdir > /dev/null
          rm -rf Gemfile.lock
          scl enable ruby193 "bundle install --local > /dev/null"
          chown apache:apache Gemfile.lock
          RETVAL=$?
        popd > /dev/null
        if [ $RETVAL != 0 ]; then
          echo $"Bundle failed"
          echo "Run 'bundle install --local' in $osdir to see the problem."
	  return $RETVAL
	fi
        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
