#!/bin/bash

# Determine the name of this service
service_name() {
    DIR=`dirname $0`
    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
}

SERVICE_NAME=`service_name`

# shutdown the system-wide apache instance for good measure
/etc/init.d/apache2 stop 2> /dev/null 1> /dev/null

# check for symlink in init system
test -L /etc/rc2.d/S90apache2-${SERVICE_NAME} || {
   echo Symlink missing from rc2.d
   ln -s /srv/${SERVICE_NAME}/etc/init.d/apache2 /etc/rc2.d/S90apache2-${SERVICE_NAME}
}

# Check status
/srv/${SERVICE_NAME}/etc/init.d/apache2 status 2> /dev/null 1> /dev/null

if [ "${?}" != 0 ]; then
    echo APACHE SERVICE FAILED!!!!!!!!
    echo -n "uptime: "
    uptime
    echo Attempting to restart
    /srv/${SERVICE_NAME}/etc/init.d/apache2 stop
    /srv/${SERVICE_NAME}/etc/init.d/apache2 start
fi


