#!/bin/sh
#
# Startup script for the CLUSTERPRO daemon
#
# chkconfig: 35 90 5
# description: CLUSTERPRO Daemon Script
#
### LSB #############################################################
# /etc/init.d/clusterpro
#
### BEGIN INIT INFO
# Provides: clusterpro
# Required-Start: clusterpro_trn
# Required-Stop: clusterpro_trn
# Default-Start: 3 5
# Default-Stop: 0 1 2 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
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
	if [ "$targetos" = "SunOS" ]
	then
		echo "Starting clusterpro daemon: \c"
	else
		echo -n "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/lock/subsys/clusterpro
		clp_logwrite "$1" "touch end." init_main
	fi

	clp_logwrite "$1" "clusterpro_main start process end." init_main
	;;
  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

	# 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 [ "$targetos" = "Linux" ]
		then
			rm -f /var/lock/subsys/clusterpro
		fi
		exit 0
	fi

	# stoping clusterpro
	if [ "$targetos" = "SunOS" ]
	then
		echo "Shutting down clusterpro daemon: \c"
	else
		echo -n "Shutting down clusterpro daemon: "
	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

	if [ "$runlv" = "0" ]
	then
		clp_logwrite "$1" "clpdown start." init_main
		./clpdown --type init --nochk > /dev/null 2>&1
		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
		./clpdown --type init --nochk > /dev/null 2>&1
		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
		./clpdown -r --type init --nochk > /dev/null 2>&1
		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 [ "$targetos" = "Linux" ]
		then
			rm -f /var/lock/subsys/clusterpro
		fi
		exit 0
	fi

	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 [ "$targetos" = "Linux" ]
			then
				rm -f /var/lock/subsys/clusterpro
			fi
			exit 0
		fi

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

	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 [ "$targetos" = "Linux" ]
	then
		rm -f /var/lock/subsys/clusterpro
	fi
	exit 0
	;;
  restart)
	targetos=`/bin/uname`
	clp_filedel "$1" init_main $targetos
	# restarting clusterpro
	echo -n "Restarting clusterpro daemon: "

	#
	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 [ "$targetos" = "Linux" ]
	then
		touch /var/lock/subsys/clusterpro
	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
