#!/bin/sh
#
#ident	"$Id: net,v 1.3 2005/01/04 22:03:15 mikew Exp $"
#
#       Automount Test Script
#
# It takes a list of NFS servers from the
# command line.  If no command line args
# are given, it takes the list from /etc/hosts,
# NIS, or NIS+, depending on which is selected
# in the script below (see commented-out lines).
#
#
# This script just iterates through a list
# of NFS servers testing whether the client
# on which it runs can automount their
# NFS exports.
#
# It assumes that the hosts in the list are
# NFS servers and that each server will allow
# this client to mount its exported filesystems.
#
# Note that this script tests only that the
# /net directory works.  It does not test other
# automounter features like map support, replicated
# mounts or loopback mounts (except when it finds
# its own host in the list).
#
# set VERBOSE if you care to see the result
# of the directory listing.
# unset VERBOSE if you don't want to see the directory
# listing output

. ../src/common
TIMEOUT=2	# (sec)
InitFile="../src/tests.init"

if [ -f ${InitFile} ]; then
	. ${InitFile}
fi

if [ $# -gt 0 ]; then
	HOSTLIST=`for h do echo $h ; done`
else
	ypwhich > /dev/null 2>&1
	if [ $? = 0 ]; then
		HOSTLIST=`ypcat hosts | awk '{print $2}'`
	else
		nistest `domainname` > /dev/null 2>&1
		if [ $? = 0 ]; then
			HOSTLIST=`niscat hosts.org_dir | awk '{print $1}'`
		else
			HOSTLIST=`cat /etc/hosts | awk '{print $2}'`
		fi
	fi
fi

fail=0
for HOST in ${HOSTLIST}
do
	echo ------------  $HOST  --------------
	ping $HOST $TIMEOUT >/dev/null || continue

	showmount -e $HOST > /dev/null 2>&1
	rc=$?
	if [ $HOST = `uname -n` ]; then
		echo "\tLocal Host"
		rc=0
	fi

	if  [ $rc = 0 ]; then
		echo "[ ls /net/$HOST - expect success ]"
	else
		echo "\tNothing exported"
		echo "[ ls /net/$HOST - expect failure ]"
	fi	

	if [ x$VERBOSE = x ]; then
		ls /net/$HOST > /dev/null
	else
		ls /net/$HOST
	fi
	rc1=$?
	if [ $rc1 != 0 ]; then
		rc1=1
	fi

	if [ $rc = $rc1 ]; then
		echo "\n\t- OK -"
	else
		echo "\n\t- FAILED -"
		fail = `expr $fail + 1`
	fi
	echo
done

if [ $fail = 0 ]; then
	echo "`basename $0`: SUCCEEDED"
else
	echo "`basename $0`: FAILED"
	exit 1
fi
echo
