#!/bin/sh

# $Log: S14dyn-dcslib,v $
# Revision 1.3  2004/04/23 16:21:38  lopez
# updated list of DCSLIB servers
#
# Revision 1.2  2004/04/08 14:05:39  strombrg
# dcslib3.acs -> dcslib3.nacs
#
# Revision 1.1  2004/03/19 21:58:35  stevet
# Renamed S11dyn-dcslib to S14dyn-dcslib
#
# Revision 1.8  2002/04/26 16:59:00  strombrg
# Be rid of polaris and northstar.  Add dcslib3.acs
#
# Revision 1.7  2001/04/24 22:40:16  strombrg
# Added a couple of echo's to make things look nicer on redhat consoles
#
# Revision 1.6  2001/04/24 22:21:28  strombrg
# echo to console if $DDC is empty
#
# Revision 1.5  1999/03/02 21:13:31  strombrg
#
# Added "-w 15" to traceroute's in an attempt to contend with strange
# traceroute output during dcslib server failure
#
# Revision 1.4  1998/09/16  19:12:50  strombrg
# Modified to have a sun4-5 target for sed
#
# Revision 1.3  1998/07/06  20:16:06  strombrg
# Changed mounts to -o hard,intr,ro
#
# Revision 1.2  1998/03/05  17:37:02  strombrg
# Added -n's to tracroutes.  This seems to eliminate some "254" return statuses.
# Credit due to Mike I.
#
# Revision 1.1.1.1  1997/12/02  22:39:02  strombrg
# Initial checkin.
#

# Default dcslib server to be defined in /var/adm/default-dcslib-server
# List of servers to check in /var/adm/dcslib-servers
#
# Neither file is required
#
# If /var/adm/default-dcslib-server is specified, and that server is up,
# then /var/adm/dcslib-servers is ignored.
#
# If the dcslib client machine is equidistant from any two servers listed
# in /var/adm/dcslib-servers, then the order of /var/adm/dcslib-servers
# will determine which server is used - the first server is preferred

case "$1" in
	start)
		if [ "$DDC" != "" ]
		then
			:
		else
			# !@#$ redhat
			exec > /dev/console 2>&1
			echo
			echo
		fi
		PATH=/sbin:/bin:/etc:/usr/bin:/usr/sbin
		export PATH

		if [ -f /var/adm/default-dcslib-server ]
		then
			default_server="`cat /var/adm/default-dcslib-server`"
			maxtime 10 traceroute -w 15 -n "$default_server" > /dev/null 2>&1
			if [ "$?" = 0 ]
			then
				best_server="$default_server"
				echo Using default dcslib server, "$best_server"
			else
				echo Default dcslib server is down
				echo
			fi
		fi
		if [ "$best_server" = "" ]
		then
			echo Picking a dcslib server
			echo
			best_lines=99999
			best_server=dcslib3.nacs.uci.edu
			if [ -f /var/adm/dcslib-servers ]
			then
				servers="`cat /var/adm/dcslib-servers`"
			else
				servers="dcslib3.nacs.uci.edu dcslib1.nacs.uci.edu dcslib2.nacs.uci.edu dcslib4.nacs.uci.edu"
			fi
			if [ -f /usr/bin/maxtime ]
			then
				if [ -f /usr/bin/traceroute ] || [ -f /usr/sbin/traceroute ]
				then
					DO_IT=true
				fi
			fi
			if [ "$DO_IT" = true ]
			then
				for server in $servers
				do
					echo $server
					raw="`maxtime 10 traceroute -w 15 -n $server 2> /dev/null`"
					status=$?
					lines="`echo \"$raw\" | \
						grep -c '^'`" 
					(
						echo distance: $lines
						echo status: $status
					) | sed 's/^/	/'
					echo
					if [ "$status" = 0 ] && [ "$lines" -lt "$best_lines" ]
					then
						best_lines=$lines
						best_server=$server
					fi
				done
				echo Best server choice is $best_server at distance $best_lines
				echo
			else
				echo Dynamic determination skipped, requisite executables
				echo not yet present.  Using $best_server.
				echo
			fi
		fi

		# the which-dcslib (in caps) thing is sed'd by the each script
		# that installs this file
		systype=sun4-5
#		case "`uname -s`-`uname -r`" in
#			SunOS-5*)
#				systype=sun4-5
#				;;
#			SunOS-4*)
#				systype=sun4-41
#				;;
#			IRIX*)
#				systype=sgi-5
#				;;
#			OSF1*)
#				systype=decalpha
#				;;
#			ULTRIX*)
#				systype=decmips
#				;;
#			Linux*)
#				systype=linux
#				;;
#		esac
		# do the mounts in the foreground - if none of the servers appears to
		# be alive, it's probably the network that's dead.
		while :
		do
			mount -o hard,intr,ro $best_server:/dcspdb/$systype /dcs
			if [ -d /dcs/bin ]
			then
				break
			fi
			sleep 10
		done
		while :
		do
			mount -o hard,intr,ro $best_server:/dcspda /dcslib
			if [ -d /dcslib/allsys ]
			then
				break
			fi
			sleep 10
		done
		;;
esac

