#!/bin/bash
# An intelligent NFS mounting script, that makes sure the remote host
#   is alive before mounting.

# Copyright (c) 1997 by Ryan Kirkpatrick (rkirkpat@nag.cs.colorado.edu). 
#   All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# 

# Too few parameters, print help.
if [ $# -lt 2 ]; then
	echo "Usage: AliveMount.sh num host"
	exit
fi

# Check to see if host is alive using a small program
#  that sends data to the host on the echo port,
#  waits one seconds for a response, and then returns
#  whether or not there was a response.
status=`/usr/sbin/alive $2`

# If the host does not respond, then set an at job
#  to run this script again in an ever increasing
#  number of minutes. Also email root if the host
#  does not respond after 7 attempts.
if [ $status != "Alive" ]; then
	nxt=`expr $1 "*" 2`
	echo "  $2 not responding, waiting $nxt minutes..."
	at now + $nxt minutes <<EOF >/dev/null 2>&1
/usr/sbin/AliveMount.sh $nxt $2
EOF
	exit
fi

for item in `cat /etc/fstab | grep $2 | sed 's/ .*//'`; do
	echo "  Mounting $item..."
	mount $item
done
