#!/bin/csh 
#
#				Yohkoh IDL Setup Script setys.src
#				S.L.Freeland
#				Version 2.0
#				15-May-1993 (add quiet parameter)
#				31-Jul-1993 (add idlback parameter)
#
# This file performs Yohkoh SW Environmental Setup for a variety of Yohkoh
# SW host machines - Multiple SW host machines at a site are supported to
# provide backup capability in the event that the primary server is down.
# Usually called from .yslogin in users home directory
#
# Version 2.0 extends machine independence
# Currently supported (tested) systems include [SGI, Mips, SUN, Ultrix]
# Code added to avoid re-pinging (assume child process forked)
#
############################################################################
# Phase I - Determine Operating System for system dependent ping output
# (Logic for determination based on existing directories/files borrowed
# from RSI supplied /bin/sh script [$IDL_DIR/bin/idl]
#
# The following code avoids re-execution if this is a child process
# Setting refresh_ys before sourcing script forces re-execution
# Avoids unwanted terminal output and wasted time pinging
#

if (($?ys) && !($?refresh_ys)) then	# ys defined and no forced re-execution
    exit				# unstructured but effective
else 
    if (!($?ys_quiet)) echo Yohkoh IDL environment setup 
    unset refresh_ys			# reset this for next time
endif


unset OS				
if ( -e /bin/arch ) then		#SunOS
	set OS="sunos"
	set pingtype=0
else if (-e /bin/machine ) then		#DEC Ultrix (sometimes)
	set OS="ultrix"
	set pingtype=0
else if ( -e /hp-ux ) then		#HP-UX
	set OS="hp"
else if (-e /bin/4d ) then		#SGI Irix
	set OS="sgi"
	set pingtype=1
else if (-e /dev/root ) then		#Mips
	set OS="mips"
	set pingtype=2
else if (-e /vmunix ) then		# DEC Ultrix
	OS="ultrix"
	set pingtype=0
else if (-e /bin/smit ) then			# IBM
	set OS="ibm"
else if (-e /dev/root ) then			# Mips
	OS="mips"
	set pingtype=2
endif

############################################################################

#
# Phase II - check Yohkoh SW hosts for health vi ping
#
#########################################################################
#
if (!( $?IDL_DIR)) setenv IDL_DIR /usr/local/lib/idl
#
# find ping on local machine (not always where you expect it)
#
if (-e /etc/ping) then 
   set ping=/etc/ping
else 
   if (-e /usr/etc/ping) set ping=/usr/etc/ping
endif
#
#
unsetenv ys					#start from scratch
#
#
# get possible ys host names and paths from yspaths.config
#
if (-e ~/yspaths.config) then			#user copy
   set test=`cat ~/yspaths.config`
else if (-e $IDL_DIR/yspaths.config)  then	#system copy
   set test=`cat $IDL_DIR/yspaths.config`
else if (-e /ys) then				# no copy
   setenv ys /ys				# no yspaths.config
   set test = 0					# so skip host checks!
endif
#

if ($?idlback) then
   if (!($?ys_quiet)) echo Skipping primary host, checking backup host...
   shift test
   shift test
endif
#   
#  loop through hosts until signs of life are discovered (or out of hosts)
#
while ($#test > 1)
#
   if (!($?ys_quiet)) echo "Checking Yohkoh SW host: "$test[1]
#
#  setup host dependent ping input/output parameters
#  (want only 1 packet and then return - syntax is system dependent)
#  I am glad unix is so standardized that this garbage is required

   switch ($pingtype)				# repeat case (!!)
      case 0:					# Ultrix and SUNOS
         set ping_match="alive"			# (its only a machine)
         set match_pos=3
         set ping_cmd="$ping $test[1]"
      breaksw
      case 1:      				# SGI
         set ping_match=1			# (number packets received)
         set match_pos=13
         set ping_cmd="$ping -q -c 1 $test[1]"
      breaksw
      case 2:					# Mips
         set ping_match=1			# (number packets received)
         set match_pos=18
         set ping_cmd="$ping $test[1] -c 1"	
      breaksw
   endsw
#
#
   set host_chk=`$ping_cmd`				# execute the ping
#
   if ($ping_match == $host_chk[$match_pos])  then	# live host
       setenv ys $test[2] 				# assign ys
       set test=0
   else
       if (!($?ys_quiet)) echo Yohkoh Host: $test[1] Not Responding	# dead host
       shift test					# try next one
       shift test
   endif
end
   
##############################################################################  
