#!/bin/bash
#
# apache2    A portable, host-agnostic environment for running Apache 2.x
#            Safely allows multiple instances of apache to be running side by side on the same host.
# 
# Notes:
#            Disable your system-wide Apache2 instance:
#                sudo update-rc.d apache2 remove
# 
#            Link this script into /etc/rc2.d to start this instance at boot:
#                sudo ln -s /srv/SERVICENAME/etc/init.d/apache2 /etc/rc2.d/S91apache2-SERVICENAME
#

# Source function library.
. /lib/lsb/init-functions

# Determine the name of this service
service_name() {
    ZERO=`readlink -f $0`
    DIR=`dirname $ZERO`
    pushd "${DIR}" > /dev/null
    TOP=`pwd | cut -d / -f 2`
    SERVICE=`pwd | cut -d / -f 3`
    popd > /dev/null

    # verify this is under the /srv mountpoint
    if [ "${TOP-error}" = "srv" ]
    then
        echo $SERVICE
    else
        return 1;
    fi
}


# Local Variables
INSTANCE=apache2
SERVICE=`service_name`
SERVERROOT=/srv/${SERVICE}/etc/${INSTANCE-broken}
CONFIG=${SERVERROOT}/conf/httpd.conf
pidfile=${SERVERROOT}/run/${INSTANCE-broken}.pid
lockfile=${SERVERROOT}/run/${INSTANCE-broken}.lock
prog=apache2
httpd=/usr/sbin/apache2
OPTIONS="-d ${SERVERROOT} -f ${CONFIG}"

# Apache Variables
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=${pidfile}
export APACHE_SERVICE_NAME=${SERVICE}

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

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

RETVAL=0

# 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 ${SERVICE} ${INSTANCE}: "
        $httpd $OPTIONS

        RETVAL=$?
        if [ $RETVAL = 0 ] 
        then
            echo "[OK]"
            touch ${lockfile}
        elif [ $RETVAL = 1 ]
        then
            rm -f ${lockfile} ${pidfile}
        fi

        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 ${SERVICE} ${INSTANCE}: "
    $httpd $OPTIONS -k stop

    RETVAL=$?
    if [ $RETVAL = 0 ] 
    then
        echo "[OK]"
        touch ${lockfile}
    elif [ $RETVAL = 1 ]
    then
        rm -f ${lockfile} ${pidfile}
    fi

    return $RETVAL
}


reload() {
    echo -n $"Reloading ${SERVICE} ${INSTANCE}: "
    if ! ${httpd} $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        return 1
    else
        $httpd $OPTIONS -k graceful

        RETVAL=$?
        if [ $RETVAL = 0 ] 
        then
            echo "[OK]"
            touch ${lockfile}
        elif [ $RETVAL = 1 ]
        then
            rm -f ${lockfile} ${pidfile}
        fi

        return $RETVAL
    fi
}

restart() {
    echo -n $"Restarting ${SERVICE} ${INSTANCE}: "
    if ! ${httpd} $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not restarting due to configuration syntax error"
        return 1
    else
        $httpd $OPTIONS -k restart

        RETVAL=$?
        if [ $RETVAL = 0 ] 
        then
            echo "[OK]"
            touch ${lockfile}
        elif [ $RETVAL = 1 ]
        then
            rm -f ${lockfile} ${pidfile}
        fi

        return $RETVAL
    fi
}

status() {
    status_of_proc -p ${pidfile} ${prog} "Service ${SERVICE} ${INSTANCE}"
    return $?
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
    status
	;;
  restart)
	restart
	;;
  reload)
    reload
	;;
  graceful)
    reload
	;;
  configtest)
    ${httpd} $OPTIONS -t
    ;;
  *)
	echo $"Usage: $prog {start|stop|restart|reload|status|graceful|configtest}"
	exit 1
esac

exit $RETVAL
