#!/bin/bash
#
# GPFS configuration (SLES only)
#
# /etc/init.d/gpfs
#
### BEGIN INIT INFO
# Provides:       gpfs
# Required-Start: $local_fs $network portmap
# Should-Start: 
# Required-Stop:
# Should-Stop:
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Configure the GPFS environment and optionally mount filesystems
### END INIT INFO

. /etc/rc.status

rc_reset

if [ ! -f /var/mmfs/etc/nfsdefs ]; then
    	echo "$0: Can't find NFS defines (nfsdefs) in /var/mmfs/etc. Terminating."
	exit 0
fi
if [ ! -f /var/mmfs/etc/nfsfuncs ]; then
    	echo "$0: Can't find NFS functions (nfsfuncs) in /var/mmfs/etc. Terminating."
	exit 0
fi
. /var/mmfs/etc/nfsdefs
. /var/mmfs/etc/nfsfuncs

start() {
	# We need to do the following before starting GPFS:
	# 1. Start ssh (or rsh) because we may need to communicate with other nodes
	#    if this is a node failback
	# 2. Don't start NFS before GPFS because it causes nfsd restart
	#    to fail in restart of lockd
	# 3. Configure NLM ports here so a subsequent start of nfsd will get the
	#    correct ports assigned
	# 4. Cleanup any stale files left over on reboot

	if [ -f $NODELIST.shutdown ]; then
		if [ ! -f $NODELIST ]; then
			mv $NODELIST.shutdown $NODELIST
		else
			rm $NODELIST.shutdown
		fi
	fi
        startRshd
	configNLMPorts $(myGPFSIP)
	if [ $? -ne 0 ]; then
		echo "$0: cannot configure NLM ports for HA-NFS. NLM failover may not function correctly."
	fi

	if [ -e /usr/lpp/mmfs/bin/mmautoload ]; then
		/usr/lpp/mmfs/bin/mmautoload
	fi
}

stop() {
	if [ -e /usr/lpp/mmfs/bin/mmshutdown ]; then
		/var/mmfs/etc/nfsmonitor -e
		/usr/lpp/mmfs/bin/mmshutdown
		/var/mmfs/etc/cleanup
	fi
}

case "$1" in
	start)
		echo -n "Starting GPFS "
		start
		rc_status -v
		;;
	stop)
		echo -n "Stopping GPFS "
		stop
		rc_status -v
		;;
	shutdown)
		echo -n "Stopping GPFS (no failover) "
		[ -f $NODELIST ] && mv $NODELIST $NODELIST.shutdown
		stop
		rc_status -v
		[ -f $NODELIST.shutdown ] && mv $NODELIST.shutdown $NODELIST
		;;
	restart|try-restart)
		echo -n "Restarting GPFS "
		[ -f $NODELIST ] && mv $NODELIST $NODELIST.shutdown
		stop
		[ -f $NODELIST.shutdown ] && mv $NODELIST.shutdown $NODELIST
		start
		rc_status -v
		;;
	status)
		rc_failed 4
		rc_status -v
		;;
	reload|force-reload)
		# Currently, this only restarts the monitor
		if [ -e /var/mmfs/etc/nfsmonitor ]; then
			echo -n "Restarting monitor "
			/var/mmfs/etc/nfsmonitor restart
		fi
		rc_status -v
		;;	
    	*)
		echo "Usage: $0 {start|stop|shutdown|restart|try-restart|reload|force-reload|status}"
		exit 1
esac
rc_exit
