#!/bin/bash
#
# pool		start/stop pool layer
#
# chkconfig: 345 15 85
# description: Starts and stops the pool layer.  Only the pool names listed \
#              $POOLS will be operated on.  If $POOLS is not defined, then \
#	       all pools that are detected on that system will be operated on.
#
### BEGIN INIT INFO
# Provides: 
### END INIT INFO

. /etc/init.d/functions
. /etc/init.d/gfs-functions
[ -f /etc/sysconfig/gfs ] && . /etc/sysconfig/gfs



#
# print out the pools that assembled
#
assembled_pools()
{
	pool_info | awk '(/^No pools/){exit 0} ($1 != "Major"){print $3}'
}

start()
{
	sts=0
	if [ -n "$POOLS" ]
	then
		for pool in $POOLS
		do
			echo -n "Starting pool $pool:"
			if pool_assemble -O -q $pool 2>/dev/null
			then
				success "startup"
			else
				failure "startup"
				sts=1
			fi
			echo
		done
	else
		echo -n "Starting pool:"
		if pool_assemble -O -q 2>/dev/null 
		then
			success "startup"
		else
			failure "startup"
			sts=1
		fi
		echo
	fi
	return $sts
}

stop()
{
	sts=0
	if [ -n "$POOLS" ]
	then
		pools=$POOLS
	else
		pools=$(assembled_pools | xargs -r)
	fi

	for pool in $pools
	do
		echo -n "Stopping pool $pool:"
		if pool_assemble -qr $pool 2>/dev/null
		then
			success "shutdown"
		else
			failure "shutdown"
			sts=1
		fi
		echo
	done
	return $sts
}	

status()
{
	if grep -qE "^pool " /proc/modules
	then
		if [ -n "$POOLS" ]
		then
			for pool in $POOLS
			do
				if pool_info -q $pool
				then
					echo $pool is assembled
				else
					echo $pool is not assembled
				fi
			done
		else
			pools=$(assembled_pools | xargs -r)
			for pool in $pools
			do
				echo $pool is assembled
			done
		fi
	else
		echo pool module is not loaded
	fi
}

rtrn=1

# See how we were called.
case "$1" in
  start)
	load_module pool MODULE_POOL
	start
	rtrn=$?
	touch /var/lock/subsys/pool
	;;

  stop)
	stop 
	rtrn=$?
	[ $rtrn -eq 0 ] && rm -f /var/lock/subsys/pool
	unload_module pool > /dev/null 2>&1
	;;

  restart)
	$0 stop
	$0 start
	rtrn=$?
	;;

  status)
	status
	rtrn=0
	;;

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

exit $rtrn

