#!/bin/bash

### BEGIN INIT INFO
# Provides:          ibmihs
# Required-Start:    $syslog $local_fs $network
# Should-Start:      $time
# Required-Stop:     $syslog $local_fs $network
# Should-Stop:       $time
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: IBM HTTP Server
# Description:       IBM HTTP Server
### END INIT INFO

# chkconfig: 35 98 02
# description: IBM HTTP Server

# Place this file at /etc/init.d/ibmihs (or
# /etc/rc.d/init.d/ibmihs) and make symlinks to
#   /etc/rc.d/rc0.d/K02ibmihs
#   /etc/rc.d/rc1.d/K02ibmihs
#   /etc/rc.d/rc2.d/K02ibmihs
#   /etc/rc.d/rc3.d/S98ibmihs
#   /etc/rc.d/rc4.d/S98ibmihs
#   /etc/rc.d/rc5.d/S98ibmihs
# Or check out the chkconfig program, if you have it.
#
## EDIT FROM HERE, if you absolutely must. Rather use /etc/sysconfig/ibmihs, though.

# Start up node agent as well?
IHS_INSTDIR=/opt/IBM/WebSphere/IHS

# Renice it, if necessary
IHS_NICE=0

## STOP EDITING HERE

# The name of this script
MYNAME="ibmihs"

# Source the sysconfig file, if present
if [ -f /etc/sysconfig/ibmihs ]; then
    . /etc/sysconfig/ibmihs
else
    echo "${MYNAME}: Please create /etc/sysconfig/ibmihs"
fi

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# TODO

# Check for echo -n vs echo \c
if echo '\c' | grep -s c >/dev/null 2>&1 ; then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi

# ctl_ihs
#
# Will start, stop or check status of the IHS server process
#
# Arguments: the action to perform (start, stop, status) as $1
# Returns:
# 0 - success
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
ctl_ihs() {
    ACT=$1

    # IHS control program, apachectl
    IHS_CTL=${IHS_INSTDIR}/bin/apachectl
    if [ ! -x "${IHS_CTL}" ]; then
	echo "cannot execute ${IHS_CTL}!"
	return 5
    fi

    # TODO:
    #  - check if IHS is already running prior to starting it up
    #  - after starting IHS up, check if it indeed is running
    #  - implement renicing of the process tree leader
    #  - fix return values
    case $ACT in
	start)
	    ${IHS_CTL} start
	    ;;
	stop)
	    ${IHS_CTL} stop
	    ;;
	graceful)
	    ${IHS_CTL} graceful
	    ;;
	restart)
	    ${IHS_CTL} restart
	    ;;
	status)
	    ${IHS_CTL} status
	    ;;
    esac

    return 0
}

# Parse command line parameters.
case $1 in
  start|stop|status|graceful|restart)
	if [ "$1" = "start" ]; then
	    ACTION="Starting"
	elif [ "$1" = "stop" ]; then
	    ACTION="Stopping"
	elif [ "$1" = "graceful" ]; then
	    ACTION="Gracefully restarting"
	elif [ "$1" = "restart" ]; then
	    ACTION="Restarting"
	else
	    ACTION="Checking status of"
	fi
	ACT=$1

	${ECHO_N} "${MYNAME}: ${ACTION} IBM HTTP Server: "${ECHO_C}
	ctl_ihs $ACT
	;;
  *)
	# Print help
	echo "Usage: $0 {start|stop|restart|status}" 1>&2
	exit 1
	;;
esac

exit 0
