#!/bin/bash
#
# pvfs2-client  Mount pvfs2 network filesystems.
#
# Authors:      Josko Plazonic <plazonic@math.princeton.edu>
#
# chkconfig: 345 26 74
# description: Mounts and unmounts all pvfs2 file systems

[ -f /etc/sysconfig/network ] || exit 0
. /etc/init.d/functions
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

if [ -f /etc/pvfs2tab ]; then
	PVFS2TABFILES="/etc/fstab /etc/pvfs2tab"
else
	PVFS2TABFILES="/etc/fstab"
fi

# we read PVFS2_CLIENT options
if [ -f /etc/sysconfig/pvfs2-client ]; then
	. /etc/sysconfig/pvfs2-client
fi

PVFS2TAB=`LC_ALL=C awk '!/^#/ && $3 == "pvfs2" && $4 !~ /noauto/ { print $2 }' $PVFS2TABFILES`
PVFS2MTAB=`LC_ALL=C awk '!/^#/ && $3 == "pvfs2" { print $2 }' /proc/mounts`

case $1 in 
	start)
		# No reason to do anything if no pvfs2 filesystems
		if [ -n "$PVFS2TAB" ]; then
			echo -n $"Starting up pvfs2 client"
			daemon /sbin/pvfs2-client ${PVFS2_CLIENT}
			RETVAL=$?
			echo
			if [ $RETVAL -eq 0 ]; then
				touch /var/lock/subsys/pvfs2-client
				for i in $PVFS2TAB; do
					AWKSCRIPT='!/^#/ && $3 == "pvfs2" && $4 !~ /noauto/ && $2 == "'$i'" { printf "mount -t pvfs2 ";  if ($4 != "defaults") printf "-o "$4" "; print $1 " " $2 }'
					action $"Mounting pvfs2 file system $i" `LC_ALL=C awk "$AWKSCRIPT" $PVFS2TABFILES`
				done
			fi
		fi
		;;
	stop)
		if [ -n "$PVFS2MTAB" ]; then
			action $"Unmounting pvfs2 filesystems: " umount -a -t pvfs2
		fi
		# Now stop the daemon
		action $"Shutting down pvfs2 client" kill `pgrep -f /sbin/pvfs2-client` `pgrep pvfs2-client-`
		rm -f /var/lock/subsys/pvfs2-client
		;;
	status)
		if [ -f /proc/mounts ] ; then
			[ -n "$PVFS2TAB" ] && {
				echo $"Configured PVFS2 mountpoints: "
				for fs in $PVFS2TAB; do echo $fs ; done
			}
			[ -n "$PVFS2MTAB" ] && {
				echo $"Active PVFS2 mountpoints: "
				for fs in $PVFS2MTAB; do echo $fs ; done
			}
		else
			echo $"/proc filesystem unavailable"
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	reload)
		$0 start
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|reload|status}"
		exit 1
		;;
esac

exit 0
