#! /sbin/sh
#Tag 0x00000f00

# XLV control
# "$Revision: 1.12 $"

always=true

startup_xlv ()
{
	xlv_labd_opt=`cat /etc/config/xlv_labd.options 2> /dev/null`
	#
	# Get the local nodename directly from the
	# /etc/sys_id file. Cannot use hostname(1)
	# because the system name may not be set yet.
	#
	if [ -f /etc/sys_id ] ; then
		name=`cat /etc/sys_id | sed -e 's/\([0-z]*\)\..*/\1/'`
	else
		name="IRIS"
	fi

	if /sbin/chkconfig verbose ; then
		echo "Starting XLV:"
		assemble_opt="-t"
	else
		assemble_opt="-q"
	fi

	# Start the plex revive daemon with a max of 4
	# concurrent revives. This daemon needs to be
	# started before any mirrored volumes are assembled.
	#
	if [ "$always" = "true" ]
	then
		killall xlv_plexd > /dev/null 2>&1
		/sbin/xlv_plexd -m 4
	else
		ps -ef | grep -v grep | grep xlv_plexd > /dev/null 2>&1
		if [ $? != 0 ]
		then
			/sbin/xlv_plexd -m 4
		fi
	fi

	# Set the geometry of the logical volume.
	#
	/sbin/xlv_assemble -h $name $assemble_opt

	# Make sure that any configuration files written by xlv_assemble
	# are written to disk.
	/sbin/sync

	# Start the label daemon. Note that we start it
	# after running xlv_assemble because xlv_labd
	# will only hang around if there are logical
	# volumes.
	#
	if [ -x /sbin/xlv_labd ]
	then
		if [ "$always" = "true" ]
		then
			killall xlv_labd > /dev/null 2>&1
			/sbin/xlv_labd $xlv_labd_opt
		else
			ps -ef | grep -v grep | grep xlv_labd > /dev/null 2>&1
			if [ $? != 0 ]
			then
				/sbin/xlv_labd $xlv_labd_opt
			fi
		fi
	fi
}


#
# M a i n
#

case $1 in
'init')
	#
	# init process
	#
	# Remove cruft from the last time system
	# was restarted.
	#
	rm -rf /etc/.xlv_plexd_request_fifo > /dev/null 2>&1
	rm -rf /dev/dsk/xlv > /dev/null 2>&1
	rm -rf /dev/rdsk/xlv > /dev/null 2>&1

	startup_xlv
	;;

'start')
	#
	# (Re)Start operation
	#
	# Set $always to "false" because we don't want to
	# start xlv daemons multiple times.
	#
	always=false
	startup_xlv
	;;

'stop')
	#
	# Shutdown operation
	#
	if /sbin/chkconfig verbose ; then
		echo "Shutting down XLV:"
	fi
	# /sbin/xlv_shutdown

	# Stop the label daemon
	killall xlv_plexd > /dev/null 2>&1
	killall xlv_labd > /dev/null 2>&1
	;;

*)
	echo "usage: /etc/init.d/xlv {init|start|stop}"
	;;
esac
