#! /bin/sh
#
# /etc/init.d/db2
#
#
# System startup script for the DB2 instances.
# Will start/stop whatever instances are enabled to
# start at boot time in the DB2 global registry.
# See the DB2 Information Center for the db2greg 
# utility for more details.
#
# This is specific for RHEL, may not work on other
# distributions.
#
# Edit the DB2_INSTALL_PATH variable. Drop this script
# in /etc/init.d. Call `chkconfig --add db2`.
#

# Copyright 2013 Nick Ivanov
# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.


### BEGIN INIT INFO
# Provides:       db2
# Required-Start: $local_fs
# Required-Stop: $local_fs
# X-Start-Before: $network
# X-Stop-After: $network
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Start/stop DB2 instance processes
### END INIT INFO

# DB2 installation path; only one if multiple versions installed
# !!! Change this according to your environment
DB2_INSTALL_PATH=/opt/ibm/db2/V10.1


# Source common functions
. /etc/init.d/functions


list_instances() {
  $DB2_INSTALL_PATH/bin/db2greg -dump | grep -E '^I,DB2' | \
        cut -d, --output-delimiter=" " -s -f4,5,7
}

checkproc() {
# sort of replacement for the SuSE checkproc
	ps -C db2sysc --no-heading -o uname | grep -c $1>/dev/null
}

RET=0

case "$1" in
    	start)
		echo "Checking DB2 instances..."    
		# read DB2 instances from the global registry
		list_instances | while read INST INSTDIR AUTOSTART 
		do 
			if [ $AUTOSTART -eq 1 ]
			then
				echo -n "	$INST"
				su - $INST -c $INSTDIR/adm/db2start>/dev/null& # send to background
				RC=$?
				[ $RC -eq 0 ] && success || failure
				echo
				RET+=$RC
			fi
		done
		;;

	status)
		echo "Checking for DB2 instances..."

		list_instances | while read INST INSTDIR AUTOSTART 
		do 
			echo -n "	$INST"
			checkproc $INST
			RC=$?
			[ $RC -eq 0 ] && success  # just report those that are running
			[ $RC -gt 0 -a $AUTOSTART -eq 1 ] && failure # autostart instances should be running
			[ $RC -gt 0 -a $AUTOSTART -ne 1 ] && passed # ignore non-autostart instances 
			echo
		done
		;;

	stop)
		echo "Stopping all DB2 instances..."

		list_instances | while read INST INSTDIR AUTOSTART 
		do 
			checkproc $INST
			RC=$?
			if [ $RC -eq 0 ]
			then
				echo -n "	$INST"
				su - $INST -c "$INSTDIR/adm/db2stop force">/dev/null # wait for it to stop
				RC=$?
				[ $RC -eq 0 ] && success || failure
				echo
				RET+=$RC
			fi		
		done
		;;

    	*)
		echo "Usage: $0 {start|stop|status}"
		RET=1
		;;
esac

exit $RET
