#! /bin/sh
#
# 	@(#)vhe_getlist:	$Revision: 1.15.109.1 $	$Date: 91/11/19 14:20:06 $
#
# (c) Copyright 1987 Hewlett-Packard Company
#
# Usage:  vhe_getlist 
#
# This script will write to stdout the contents of the vhe_list.
# It will use Network Information Service to get the contents if NIS is active,
# otherwise it will just use $VHE_FILE directly.
# It deletes comments from $VHE_FILE (the NIS form has them removed
# already).
# It will also remove any blank lines from $VHE_FILE.
# The sort that is done just before putting the data to stdout is done
# to prevent improper ordering of file systems getting mounted.  With
# this, the user can put 
#   nodeA   /users   /vhe/A/users
#   nodeA   /        /vhe/A           wsize=1024
#   nodeA   /nfs/rcs /nfs/rcs
#   nodeB   /nfs     /nfs
# but this script will list the line with "/vhe/A" first then "/vhe/A/users".
# Also added is a filter that will flag an error if there are not enough
# parameters on a line in the vhe_list file.
#
# Set up variables with command names.  The names are full path specified
# to prevent security holes.  They are kept in variables for ease of
# change.
AWK=/usr/bin/awk
CAT=/bin/cat
DOMAINNAME=/bin/domainname
ECHO=/bin/echo
GREP=/bin/grep
PROBLEM=/tmp/vhePB$$
RM=/bin/rm
SED=/bin/sed
SORT=/bin/sort
YPCAT=/usr/bin/ypcat
YPWHICH=/usr/bin/ypwhich
VHE_FILE=/etc/vhe_list
VHE_LIST=vhe_list

# Set up trap handler so temp files will get removed
trap "$RM -rf $PROBLEM " 0
trap "exit 1 " 1 2 3 15

# Look for undefined variables
set -u


$RM -rf $PROBLEM 2>/dev/null


# Testing to see if NIS is being used before doing a ypcat
DOM=`$DOMAINNAME`
if [ $DOM ]
then 
	BOUND=`$YPWHICH 2>/dev/null`
	if [ $BOUND ]
	then NIS="YES"
	else NIS="NO"
	fi
else 
	NIS="NO"
fi

if [ $NIS = "YES" ]
then
	# Check to see if there is a vhe_list map
	$YPCAT -k $VHE_LIST  2>&1 | $GREP "no such map"	> /dev/null 2>&1
	RET_VALUE=$?
	if [ $RET_VALUE = "0" ]
	then
		$ECHO "ERROR: No $VHE_LIST file is available from the Network INformation Service" >&2
	    	$ECHO "       server.  Please make sure that the $VHE_FILE file " >&2
	    	$ECHO "       exists on the server and execute a ypmake on the " >&2
	    	$ECHO "       Network Information Service server.  See the Virtual Home Environment" >&2
		$ECHO "       chapter in the Network File System manual for more details." >&2
		$ECHO >&2
		exit 1
	fi
  	$YPCAT -k $VHE_LIST | $SORT | \
	$AWK ' { print $2,$3,$4,$5 } ' | $SORT -b +2 -3  | $SED '/^$/d' | $SED '/^ /d' | \
	while read ARG1 ARG2 ARG3 ARG4 ARG5
	do
		if [ $ARG3 ]
		then $ECHO $ARG1  $ARG2  $ARG3  $ARG4  $ARG5 
		else $ECHO "ERROR: The following line from $VHE_FILE needs to have at least" >&2
		     $ECHO "       three pieces of information.  The fields needed are:" >&2
		     $ECHO "         <remote_nodename>  <remote_filesystem>  <mount_point>" >&2
		     $ECHO "       Please correct the $VHE_FILE and rerun this command." >&2
		     $ECHO $ARG1  $ARG2  $ARG3  $ARG4  $ARG5 >&2
		     $ECHO >&2
		     > $PROBLEM
		fi
	done 
else
	# Check to see if there is a vhe_list map
	if [ -f $VHE_FILE ]
	then :
	else
		$ECHO "ERROR: There is no $VHE_FILE on this machine.  Please make" >&2
		$ECHO "       sure that one exists.  See the Virtual Home Environment" >&2
		$ECHO "       chapter in the Network File System manual for more details." >&2
		$ECHO >&2
		exit 1
	fi
  	$CAT  $VHE_FILE | \
	$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print NR, $0 }' | \
	$AWK ' { print $2,$3,$4,$5 } ' | \
	$SED '/^#/d' | $AWK ' { print  $1,$2,$3,$4 } ' | \
	$SORT -b +2 -3 | $SED '/^$/d' | $SED '/^ /d' | \
	while read ARG1 ARG2 ARG3 ARG4 ARG5
	do
		if [ $ARG3 ]
		then $ECHO $ARG1  $ARG2  $ARG3  $ARG4  $ARG5
		else $ECHO "ERROR: The following line from $VHE_FILE needs to have at least" >&2
		     $ECHO "       three pieces of information.  The fields needed are:" >&2
		     $ECHO "         <remote_nodename>  <remote_filesystem>  <mount_point>" >&2
		     $ECHO "       Please correct the $VHE_FILE and rerun this command." >&2
		     $ECHO $ARG1  $ARG2  $ARG3  $ARG4  $ARG5 >&2
		     $ECHO >&2
		     > $PROBLEM
		fi
	done 
fi

# I have to do this klugey stuff with a file because if I tried to 
# use a shell variable, it won't work because the while loop is in
# separate process and therefore the setting of a variable there
# won't be seen at this layer in the shell.
if [ -r $PROBLEM ]
then exit 1
fi
