#!/bin/sh
#
### LSB #####################################################
# /etc/init.d/clusterpro
#
### BEGIN INIT INFO
# Provides: clusterpro
# Required-Start: clusterpro_trn
# Required-Stop: clusterpro_trn
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the clusterpro daemon
### 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
elif [ -f /lib/lsb/init-functions ]
then
	. /lib/lsb/init-functions
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_=2

export PATH
export LD_LIBRARY_PATH
export MALLOC_CHECK_

# See how we were called.
case "$1" in
  start)
	targetos=`/bin/uname`

	clp_filedel "$1" init_main $targetos
	clp_logwrite "$1" "clusterpro_main start process start." init_main

	# don't remove next line
	#daemon clppm

	#
	cd /opt/nec/clusterpro/bin

	# export all volmgr resource
	clp_logwrite "$1" "clpvolmgrc start." init_main
	./clpvolmgrc -d > /dev/null 2>&1
	retvolmgrc=$?
	clp_logwrite "$1" "clpvolmgrc end.("$retvolmgrc")" init_main

	# starting clusterpro
	logging=`clpcfget -g /root/event/container/logging` 
	cfget_ret=$?
	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		log_begin_msg "Starting clusterpro daemon" > /dev/null 2>&1
	else
		log_begin_msg "Starting clusterpro daemon"
	fi

	#
	clp_logwrite "$1" "clpcl -s start." init_main
	./clpcl -s --type init > /dev/null 2>&1
	clrets=$?
	clp_logwrite "$1" "clpcl -s end.("$clrets")" init_main

	clp_checkerr "$clrets" $targetos
	chk_ret=$?
	if [ "$chk_ret" = "1" ]
	then
		clp_logwrite "$1" "clusterpro_main start process end." init_main
		exit 1
	fi

	#
	if [ "$targetos" = "Linux" ]
	then
		clp_logwrite "$1" "touch start." init_main
		touch /var/run/clusterpro
		clp_logwrite "$1" "touch end." init_main
	fi

	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		clp_logwrite "$1" "clusterpro_main start process end." init_main
		log_end_msg $? > /dev/null 2>&1
	else
		clp_logwrite "$1" "clusterpro_main start process end." init_main
		log_end_msg $?
	fi
	;;
  stop)
	targetos=`/bin/uname`

	clp_filedel "$1" init_main $targetos
	clp_logwrite "$1" "clusterpro_main stop process start." init_main

	# don't remove next line
	#killproc clppm

	logging=`clpcfget -g /root/event/container/logging` 
	cfget_ret=$?
	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		log_begin_msg "Shutting down clusterpro daemon" > /dev/null 2>&1
	else
		log_begin_msg "Shutting down clusterpro daemon"
	fi

	# check running clppm
	clp_logwrite "$1" "clppidof start." init_main
	pid=`clppidof /opt/nec/clusterpro/bin/clppm 2>&1`
	clp_logwrite "$1" "clppidof end.(pid=${pid})" init_main

	if [ "$pid" = "" ]
	then
		clp_logwrite "$1" "clusterpro_main is not exist." init_main
		clp_logwrite "$1" "clusterpro_main stop process end." init_main
		clp_success $targetos

		if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
		then
			if [ "$targetos" = "Linux" ]
			then
				rm -f /var/run/clusterpro
			fi
			log_end_msg $? > /dev/null 2>&1
		else
			if [ "$targetos" = "Linux" ]
			then
				rm -f /var/run/clusterpro
			fi
			log_end_msg $?
		fi
		exit 0
	fi

	#
	cd /opt/nec/clusterpro/bin

	# shutting down clusterpro
	clp_logwrite "$1" "runlevel start." init_main
	if [ "$targetos" = "SunOS" ]
	then
		runlv=`who -r 2>&1 | awk '{print $3}'`
	else
		runlv=`runlevel 2>&1 | gawk '{print $NF}'`
	fi
	clp_logwrite "$1" "runlevel end.(runlv=${runlv})" init_main

	forcestop_env=$(clpcfget -g /root/forcestop/env)
	forcestop_ret=$?
	clp_logwrite "$1" "forcestop(cfget_ret=${forcestop_ret}, env=${forcestop_env})" init_main

	if [ "$runlv" = "0" ]
	then
		clp_logwrite "$1" "clpdown start." init_main
		if [ "$forcestop_ret" != "0" ] || [ "$forcestop_env" = "disabled" ]
		then
			./clpdown --type init --nochk > /dev/null 2>&1
		else
			./clpdown --type init --nochk --apito 1 > /dev/null 2>&1
		fi
		downret=$?
		clp_logwrite "$1" "clpdown end.("$downret")" init_main

		if [ "$downret" != "0" ]
		then
			clp_logwrite "$1" "clpdown failed." init_main
		fi
	elif [ "$runlv" = "5" -a "$targetos" = "SunOS" ]
	then
		clp_logwrite "$1" "clpdown start." init_main
		if [ "$forcestop_ret" != "0" ] || [ "$forcestop_env" = "disabled" ]
		then
			./clpdown --type init --nochk > /dev/null 2>&1
		else
			./clpdown --type init --nochk --apito 1 > /dev/null 2>&1
		fi
		downret=$?
		clp_logwrite "$1" "clpdown end.("$downret")" init_main

		if [ "$downret" != "0" ]
		then
			clp_logwrite "$1" "clpdown failed." init_main
		fi
	elif [ "$runlv" = "6" ]
	then
		clp_logwrite "$1" "clpdown -r start." init_main
		if [ "$forcestop_ret" != "0" ] || [ "$forcestop_env" = "disabled" ]
		then
			./clpdown -r --type init --nochk > /dev/null 2>&1
		else
			./clpdown -r --type init --nochk --apito 1 > /dev/null 2>&1
		fi
		downretr=$?
		clp_logwrite "$1" "clpdown -r end.("$downretr")" init_main

		if [ "$downretr" != "0" ]
		then
			clp_logwrite "$1" "clpdown -r failed." init_main
		fi
	else
		clp_logwrite "$1" "clpcl -t start." init_main
		./clpcl -t --type init --nochk > /dev/null 2>&1
		clrett=$?
		clp_logwrite "$1" "clpcl -t end.("$clrett")" init_main

		if [ "$clrett" != "0" ]
		then
			clp_logwrite "$1" "clpcl -t failed." init_main
		fi
	fi

	#
	clp_logwrite "$1" "clppidof start." init_main
	pid=`clppidof /opt/nec/clusterpro/bin/clppm 2>&1`
	clp_logwrite "$1" "clppidof end.(pid=${pid})" init_main

	if [ "$pid" = "" ]
	then
		clp_logwrite "$1" "cluster stop success." init_main
		clp_logwrite "$1" "clusterpro_main stop process end." init_main
		clp_success $targetos

		if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
		then
			if [ "$targetos" = "Linux" ]
			then
				rm -f /var/run/clusterpro
			fi
			log_end_msg $? > /dev/null 2>&1
		else
			if [ "$targetos" = "Linux" ]
			then
				rm -f /var/run/clusterpro
			fi
			log_end_msg $?
		fi
		exit 0
	fi

	if [ "$forcestop_ret" != "0" ] || [ "$forcestop_env" = "disabled" ]
	then
		i=0
		while [ "$i" -lt "30" ]
		do
			clp_logwrite "$1" "clppidof2 start." init_main
			pid=`clppidof /opt/nec/clusterpro/bin/clppm 2>&1`
			clp_logwrite "$1" "clppidof2 end.(pid=${pid})" init_main

			if [ "$pid" = "" ]
			then
				clp_logwrite "$1" "cluster stop success." init_main
				clp_logwrite "$1" "clusterpro_main stop process end." init_main
				clp_success $targetos

				if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
				then
					if [ "$targetos" = "Linux" ]
					then
						rm -f /var/run/clusterpro
					fi
					log_end_msg $? > /dev/null 2>&1
				else
					if [ "$targetos" = "Linux" ]
					then
						rm -f /var/run/clusterpro
					fi
					log_end_msg $?
				fi
				exit 0
			fi

			sleep 1
			i=`expr "$i" + 1`
		done
	fi

	clp_logwrite "$1" "haltp start." init_main
	if [ "$runlv" = "0" ]
	then
	./clpuserw --halt & 2>&1
	elif [ "$runlv" = "5" -a "$targetos" = "SunOS" ]
	then
		./clpuserw --halt & 2>&1
	elif [ "$runlv" = "6" ]
	then
		./clpuserw --halt & 2>&1
	else
		clp_logwrite "$1" "haltp not execute." init_main
	fi
	clp_logwrite "$1" "haltp end." init_main

	clp_logwrite "$1" "clusterpro_main stop process end." init_main
	clp_success $targetos

	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		if [ "$targetos" = "Linux" ]
		then
			rm -f /var/run/clusterpro
		fi
		log_end_msg $? > /dev/null 2>&1
	else
		if [ "$targetos" = "Linux" ]
		then
			rm -f /var/run/clusterpro
		fi
		log_end_msg $?
	fi
	exit 0
	;;
  restart)
	targetos=`/bin/uname`
	clp_filedel "$1" init_main $targetos
	# restarting clusterpro

	logging=`clpcfget -g /root/event/container/logging` 
	cfget_ret=$?
	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		log_begin_msg "Restarting clusterpro daemon" > /dev/null 2>&1
	else
		log_begin_msg "Restarting clusterpro daemon"
	fi
	#
	cd /opt/nec/clusterpro/bin

	clp_logwrite "$1" "clpcl -r start." init_main
	#
	./clpcl -r --type init > /dev/null 2>&1
	clret=$?
	clp_logwrite "$1" "clpcl -r end.("$clret")" init_main

	clp_checkerr "$clret" $targetos
	chk_ret=$?
	if [ "$chk_ret" = "1" ]
	then
		clp_logwrite "$1" "clusterpro_main restart process end." init_main
		exit 1
	fi

	#
	if [ "$cfget_ret" = "0" -a "$logging" = "1" ]
	then
		if [ "$targetos" = "Linux" ]
		then
			touch /var/run/clusterpro
		fi
		log_end_msg $? > /dev/null 2>&1
	else
		if [ "$targetos" = "Linux" ]
		then
			touch /var/run/clusterpro
		fi
		log_end_msg $?
	fi
	;;
  status)
	targetos=`/bin/uname`
	if [ "$targetos" = "SunOS" ]
	then
		clp_status_s clppm
	else
		clp_status clppm
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac

exit 0
