#!/bin/sh

# $Id$

#Check for any pnfs mountpoints in the /etc/fstab file that are not mounted.

FSTAB=/etc/fstab

#We need timed_func to handle running a command for a period of time before
# it should be killed (if still running).
timed_func()
{
#Run the command.
$* &
pid=$!
#Wait a minute for the command to complete, if not done, kill it.
(sleep 60; kill -s 0 $pid; if [ $? -eq 0 ]; then kill $pid; fi; exit) & > /dev/null 2>&1
#Join up with the done processes.
wait $pid > /dev/null 2>&1
rtn=$?
return $rtn
}

#Detect if /etc/fstab exists on this platform.
if [ -f /etc/fstab ]; then
    mount_points=`grep pnfs $FSTAB | grep -v "#" | awk '{print $2}'`
else
    echo "$FSTAB does not exist on `hostname`." 1>&2
    exit 1
fi 

#Detect if the mount point exists, if it does determine if it is mounted.
rtn_code=0
for item in $mount_points; do
    if [ ! -d $item ]; then
       echo "$item does not exist on `hostname`." 1>&2 
       rtn_code=`expr $rtn_code + 1` #increment the exit status
       continue
    fi

    #Allow both PNFS and Chimera with .(tags)().
    timed_func cat "$item/.(tags)()"  > /dev/null 2>&1
    rtn=$?
    if [ $rtn -ne 0 ]; then
       echo "$item not mounted on `hostname`." 1>&2
       rtn_code=`expr $rtn_code + 1` #increment the exit status
    fi
done


exit $rtn_code
