#!/bin/sh
#
# Startup script for the CLUSTERPRO information base
#
# chkconfig: 35 82 08
# description: CLUSTERPRO information base Script
#
### LSB #############################################################
# /etc/init.d/clusterpro_ib
#
### BEGIN INIT INFO
# Provides: clusterpro_ib
# Required-Start: clusterpro_trn
# Required-Stop: clusterpro_trn
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the clusterpro information base
### END INIT INFO

#####################################################################
#
# main()
#

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]
then
	. /etc/rc.d/init.d/functions
elif [ -f /etc/rc.status ]
then
	. /etc/rc.status
fi

. /opt/nec/clusterpro/bin/clpfunctions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/nec/clusterpro/bin
LD_LIBRARY_PATH=/opt/nec/clusterpro/lib
MALLOC_CHECK_=0
LANG=C

export PATH
export LD_LIBRARY_PATH
export MALLOC_CHECK_
export LANG

# See how we were called.
case "$1" in
  start)
	targetos=`/bin/uname`
	clp_filedel "$1" init_ib $targetos
	clp_logwrite "$1" "clusterpro_ib start process start." init_ib
	    
    	# don't remove next line
    	#daemon clpibsv
    
    	#
	cd /opt/nec/clusterpro/bin

	#
	if [ "$targetos" = "SunOS" ]
	then
		echo "Starting clusterpro information base: \c"
	else
		echo -n "Starting clusterpro information base: "
	fi

	#
	clp_logwrite "$1" "clppidof start." init_ib
	pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --ibsv 2>&1`
	clp_logwrite "$1" "clppidof end.(pids=${pids})" init_ib

	if [ "$pids" != "" ]
	then
		clp_logwrite "$1" "information base already exist." init_ib
		clp_logwrite "$1" "clusterpro_ib start process end." init_ib
		clp_failed $targetos
		exit 1
	fi

	# starting clusterpro information base
	clp_logwrite "$1" "information base start process start." init_ib
	./clpmonp --ibsv -a 2 -r 0 -w 0 > /dev/null 2>&1
	monp_ret=$?
	clp_logwrite "$1" "information base start process end.("$monp_ret")" init_ib

	clp_checkerr "$monp_ret" $targetos
	chk_ret=$?
	if [ "$chk_ret" = "1" ]
	then
		clp_logwrite "$1" "clusterpro_ib start process end." init_ib
		exit 1
	fi

	#
	if [ "$targetos" = "Linux" ]
	then
		clp_logwrite "$1" "touch start." init_ib
		touch /var/lock/subsys/clusterpro_ib
		clp_logwrite "$1" "touch end." init_ib
	fi

	clp_logwrite "$1" "clusterpro_ib start process end." init_ib
	;;
  stop)
	targetos=`/bin/uname`

	clp_filedel "$1" init_ib $targetos
	clp_logwrite "$1" "clusterpro_ib stop process start." init_ib

	# don't remove next line
	#killproc clpibsv

	#
	if [ "$targetos" = "SunOS" ]
	then
		echo "Shutting down clusterpro information base: \c"
	else
		echo -n "Shutting down clusterpro information base: "
	fi

	#
	cd /opt/nec/clusterpro/bin

	#
	clp_logwrite "$1" "clppidof start." init_ib
	pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --ibsv 2>&1`
	clp_logwrite "$1" "clppidof end.(pids=${pids})" init_ib
	
	if [ "$pids" != "" ]
	then
		# shutting down clusterpro information base
		clp_logwrite "$1" "information base stop process start." init_ib
		if [ "$targetos" = "SunOS" ]
		then
			kill -17 "$pids" 2>/dev/null
			kill_ret=$?
		else
			kill -12 "$pids" 2>/dev/null
			kill_ret=$?
		fi
		clp_logwrite "$1" "information base stop process end.("$kill_ret")" init_ib

		i=0
		while [ "$i" -lt "30" ]
		do
			clp_logwrite "$1" "clppidof2 start." init_ib
			pids=`/opt/nec/clusterpro/bin/clppidof /opt/nec/clusterpro/bin/clpmonp --ibsv 2>&1`
			clp_logwrite "$1" "clppidof2 end.(pids=${pids})" init_ib
			
			if [ "$pids" = "" ]
			then
				clp_logwrite "$1" "information base stop success." init_ib
				clp_success $targetos

				#
				if [ "$targetos" = "Linux" ]
				then
					rm -f /var/lock/subsys/clusterpro_ib
				fi
				clp_logwrite "$1" "clusterpro_ib stop process end." init_ib
				exit 0
			fi

			sleep 1
			i=`expr "$i" + 1`
		done
		
		clp_logwrite "$1" "information base stop timeout." init_ib
	else
		clp_logwrite "$1" "information base not exist." init_ib
		clp_success $targetos

		#
		if [ "$targetos" = "Linux" ]
		then
			rm -f /var/lock/subsys/clusterpro_ib
		fi
		clp_logwrite "$1" "clusterpro_ib stop process end." init_ib
		exit 0
	fi
	
	clp_logwrite "$1" "clusterpro_ib stop process end." init_ib
	clp_failed $targetos
	exit 1
	;;
  restart)
	$0 stop
	if [ "$?" != "0" ]
	then
		exit 1
	fi

	sleep 1
	$0 start
	;;
  status)
	targetos=`/bin/uname`
	if [ "$targetos" = "SunOS" ]
	then
		clp_status_s clpibsv
	else
		clp_status clpibsv
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac

exit 0
