#!/bin/bash

### BEGIN INIT INFO
# Provides:          db2das
# Required-Start:    $syslog $local_fs $network
# Should-Start:      $time
# Required-Stop:     $syslog $local_fs $network $db2inst
# Should-Stop:       $time
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: The DB2 Database Administration Server
# Description:       The DB2 Database Administration Server
### END INIT INFO

# chkconfig: 35 98 02
# description: DB2 Database Administration Server

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

# Installation prefix
DB2DIR=/opt/IBM/db2/V8.1

# DB2 Database Administration Server username
DAS_USER=dasusr1

# Renice it, if necessary
DAS_NICE=0

## STOP EDITING HERE

# The name of this script
MYNAME="db2das"

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

# 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 (DAS_BIN, DAS_LOG)
#
# Arguments: none
# Returns:
# 0 - success
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
get_variables() {
    # Obtain DAS user's homedir
    local DAS_HOME=`eval echo ~${DAS_USER}`

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

    DAS_BIN=${DAS_HOME}/das/bin/db2admin
    DAS_LOG=${DAS_HOME}/das/dump/startup.log

    # Check if the log exists and create & chown it if not yet so
    if [ ! -e "${DAS_LOG}" ]; then
	if [ ! -d "`dirname ${DAS_LOG}`" ]; then
	    return 5
	elif [ ! -w "`dirname ${DAS_LOG}`" ]; then
	    return 4
	fi
	touch ${DAS_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 ${DAS_USER} ${DAS_LOG} >/dev/null 2>&1
    fi

    return 0
}

# ctl_das
#
# Will start or stop DB2 DAS
#
# Arguments: the action to perform (start or stop)
# Returns:
# 0 - success
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
ctl_das() {
    ACT=$1

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

    if [ ! -x "${DAS_BIN}" ]; then
	echo "cannot execute ${DAS_BIN}!"
	return 5
    fi

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

    # TODO:
    #  - check if DAS is already running prior to starting it up
    #  - after starting DAS up, check if it indeed is running
    #  - redirect all output to ${DAS_LOG} and only output startup status
    #  - implement renicing of the process tree leader
    #  - fix return values
    /bin/su - ${DAS_USER} -c "${DAS_BIN} ${ACT}"

    return 0
}

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

	${ECHO_N} "${MYNAME}: ${ACTION} DB2 Database Administration Server: "${ECHO_C}
	ctl_das $ACT
	;;
  restart)
	$0 stop
	$0 start
	;;
  status)
	${ECHO_N} "${MYNAME}: Checking status of DB2 DAS: "${ECHO_C}
	if /sbin/pidof db2dasrrm >/dev/null 2>&1; then
	    DAS_PID="`ps xau | grep db2dasrrm | \
				grep -v grep | \
				grep ${DAS_USER} | \
				head -1 | tr -s ' ' | cut -f 2 -d ' '`"
	    if [ -n "${DAS_PID}" ]; then
		echo "running (PID ${DAS_PID})."
	    else
		echo "missing."
	    fi
	else
	    echo "missing."
	fi
	exit 0
	;;
  *)
	# Print help
	echo "Usage: $0 {start|stop|restart|status}" 1>&2
	exit 1
	;;
esac

exit 0
