#!/bin/bash

### BEGIN INIT INFO
# Provides:          wasdmgr
# 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: WebSphere Application Server ND - Deployment Manager
# Description:       WebSphere Application Server ND - Deployment Manager
### END INIT INFO

# chkconfig: 35 98 02
# description: WebSphere Application Server ND - Deployment Manager

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

# WAS DMGR owner username
WAS_USER=wasdmgr1

# Profile name
WAS_PROFILE=dmgr

# Server name
WAS_SERVER=dmgr

# Renice it, if necessary
WAS_NICE=0

## STOP EDITING HERE

# The name of this script
MYNAME="wasdmgr"

# Source the sysconfig file, if present
if [ -f /etc/sysconfig/wasdmgr ]; then
    . /etc/sysconfig/wasdmgr
else
    echo "${MYNAME}: Please create /etc/sysconfig/wasdmgr"
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

# get_variables
#
# Initializes environment variables (WAS_START, WAS_STOP, WAS_SERV_STAT, WAS_LOG)
#
# Arguments: none
# Returns:
# 0 - success
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
get_variables() {
    # Obtain WAS user's homedir
    local WAS_HOME=`eval echo ~${WAS_USER}`

    # Missing user?
    if [ "${WAS_HOME}" = "~${WAS_USER}" ]; then
	return 6
    fi

    WAS_START=${WAS_HOME}/${WAS_PROFILE}/bin/startManager.sh
    WAS_STOP=${WAS_HOME}/${WAS_PROFILE}/bin/stopManager.sh
    WAS_SERV_STAT=${WAS_HOME}/${WAS_PROFILE}/bin/serverStatus.sh
    WAS_LOG=${WAS_HOME}/${WAS_PROFILE}/logs/startup.log

    # Check if the log exists and create & chown it if not yet so
    if [ ! -e "${WAS_LOG}" ]; then
	if [ ! -d "`dirname ${WAS_LOG}`" ]; then
	    return 5
	elif [ ! -w "`dirname ${WAS_LOG}`" ]; then
	    return 4
	fi
	touch ${WAS_LOG}

	# This is not a fatal error, actually. It may prevent us from
	# actually being able to write to the log, but we check that
	# elsewhere.
	chown ${WAS_USER} ${WAS_LOG} >/dev/null 2>&1
    fi

    return 0
}

# ctl_was
#
# Will start, stop or check status of the WAS 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_was() {
    ACT=$1

    get_variables
    case $? in
	4)
	    echo "insufficient privileges to manipulate ${WAS_LOG}!"
	    return 4
	;;
	5)
	    echo "log directory `dirname ${WAS_LOG}` does not exist!"
	    return 5
	;;
	6)
	    echo "user ${WAS_USER} does not exist!"
	    return 6
	;;
    esac

    case $ACT in
	start)
	    if [ ! -x "${WAS_START}" ]; then
		echo "cannot execute ${WAS_START}!"
		return 5
	    fi
	    ;;
	stop)
	    if [ ! -x "${WAS_STOP}" ]; then
		echo "cannot execute ${WAS_STOP}!"
		return 5
	    fi
	    ;;
	status)
	    if [ ! -x "${WAS_SERV_STAT}" ]; then
		echo "cannot execute ${WAS_SERV_STAT}!"
		return 5
	    fi
	    ;;
    esac

    if [ ! -w "${WAS_LOG}" ]; then
	echo "log file not writable: ${WAS_LOG}!"
	return 4
    fi

    # TODO:
    #  - check if WAS is already running prior to starting it up
    #  - after starting WAS up, check if it indeed is running
    #  - redirect all output to ${WAS_LOG} and only output command status
    #  - implement renicing of the process tree leader
    #  - fix return values
    case $ACT in
	start)
	    /bin/su - ${WAS_USER} -c "${WAS_START}"
	    ;;
	stop)
	    /bin/su - ${WAS_USER} -c "${WAS_STOP}"
	    ;;
	status)
	    /bin/su - ${WAS_USER} -c "${WAS_SERV_STAT} ${WAS_SERVER}"
	    ;;
    esac

    return 0
}

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

	${ECHO_N} "${MYNAME}: ${ACTION} WAS ND Deployment Manager: "${ECHO_C}
	ctl_was $ACT
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
	# Print help
	echo "Usage: $0 {start|stop|restart|status}" 1>&2
	exit 1
	;;
esac

exit 0
