#!/bin/csh
#
#  SYSINFO --  Display IRAF system diagnostic and configuration information.
#
#  The intent of this program is to provide a means for IRAF site managers
#  to troubleshoot their IRAF installations by running numerous verification
#  tests on the iraf directory structure, networking setup, external package
#  installations, tape configurations, etc.  Failed tests are not always
#  fatal but may indicate a problem in the way something was installed or
#  configured.
#
#  Usage:   sysinfo [ -G | -V | -N | -P | -D | -T | -h ]
#	
#	where:	-G		Print General Info only"
#		-V		Do Verification tests only"
#		-N		Do Networking tests only"
#		-P		Do Extern Package tests only"
#		-D		Do Display tests only"
#		-T		Do Tape Device tests only"
#		-h		Print this message"
#
# ----------------------------------------------------------------------------

unset	noclobber
unalias cd cp cmp echo ln mv rm sed set grep ls chmod chown pwd touch sort 
unalias uniq head sed tr
set	path	= ($path /sbin /usr/sbin /bin /usr/bin /usr/ucb /etc /usr/etc)
onintr	sysinfo_cleanup



###################### START OF MACHDEP DEFINITIONS ########################

# MACHDEP definitions which may be reset below.
set user	= `whoami`			# [MACHDEP]
set LS		= /bin/ls			# [MACHDEP]


# Determine platform architecture.

set UNAME=""
if (-e /usr/bin/uname) then
    set uname_cmd = /usr/bin/uname
    set UNAME=`/usr/bin/uname | tr '[A-Z]' '[a-z]'`
else if (-e /bin/uname) then
    set uname_cmd = /bin/uname
    set UNAME=`/bin/uname | tr '[A-Z]' '[a-z]'`
else
    echo "WARNING: No 'uname' command found to determine architecture."
    exit 1
endif

set hmach = "INDEF"
switch ($UNAME) 
    case sunos:
        if (`$uname_cmd -m | cut -c2-` != "86pc") then
            setenv OSVERSION `uname -r | cut -c1`
            if ($OSVERSION == 5) then			    # Sparc Solaris
                set mach  	= "ssun"
                set hmach 	= "ssol"
		set TAPES	= "/dev/rmt/[0-9]*"
		set shlib	= "no"
		set LS		= /usr/ucb/ls
		set LIBFILES    = ""
            else			   		    # Sparc SunOS 4.x
                set mach  	= "sparc"
                set hmach 	= "sparc"
		set TAPES	= "/dev/*st[0-7]*"
		set shlib	= "no"
		set LIBFILES    = ""
            endif
        else
            set mach 		= "sunos"	    	    # Intel Solaris
            set hmach 		= "ssol"
	    set TAPES		= "/dev/*st[0-7]*"
	    set shlib		= "no"
	    set LIBFILES    	= ""
        endif
        breaksw
    case linux:
        if (`$uname_cmd -m` == "ppc") then		    # LinuxPPC
            if (-f /etc/redhat-release) then
                set mach 	= "linuxppc"
                set hmach 	= "linuxppc"
		set TAPES	= "/dev/*st[0-7]*"
		set shlib	= "no"
		set LIBFILES    = ""
            endif
        else
            if (-f /etc/redhat-release) then		    # Linux RedHat 6.x
                set mach 	= "redhat"
                set hmach 	= "redhat"
		set TAPES	= "/dev/*st[0-7]*"
		set shlib	= "no"
		set LIBFILES    = ""
            else if (-f /etc/SuSE-release) then		    # Linux SuSE 6.x
                set mach 	= "suse"
                set hmach 	= "suse"
		set TAPES	= "/dev/*st[0-7]*"
		set shlib	= "no"
		set LIBFILES    = ""
            else					    # Linux (Slackware)
                set mach 	= "linux"
                set hmach 	= "linux"
		set TAPES	= "/dev/*st[0-7]*"
		set shlib	= "no"
		set LIBFILES    = ""
            endif
        endif
        breaksw
    case freebsd: 					    # FreeBSD 4.0
        set mach 	= "freebsd"
        set hmach 	= "freebsd"
	set TAPES	= "/dev/*sa[0-7]"
	set shlib	= "no"
	set LIBFILES    = ""
        breaksw
    case hp-ux:						    # HP/UX 10.20
        set mach 	= "hp700"
        set hmach 	= "hp700"
	set TAPES	= "/dev/rmt/[0-7]*"
	set shlib	= "no"
	set LIBFILES    = ""
	set LS		= "/bin/ls -s"
        breaksw
    case irix:    					    # SGI IRIX 6.5
    case irix64:  
        set mach 	= "irix"
        set hmach 	= "irix"
	set TAPES	= "/dev/tps[0-7]*"
	set shlib	= "no"
	set LIBFILES    = ""
	set LS		= "/bin/ls -s"
        breaksw
    case aix:     					    # IBM AIX V4
        set mach 	= "rs6000"
        set hmach 	= "rs6000"
	set TAPES	= "/dev/rmt[0-7]*"
	set shlib	= "no"
	set LIBFILES    = ""
	set LS		= "/bin/ls -s"
        breaksw
    case osf1:    					    # DEC Alpha OSF/1
        set mach 	= "alpha"
        set hmach 	= "alpha"
	set TAPES	= "/dev/nrmt[0-7]* /dev/rmt[0-7]*"
	set shlib	= "yes"
	set LIBFILES    = "libiraf.so"
        breaksw
    case ultrix:    					    # DEC Ultrix
        set mach 	= "ultrix"
        set hmach 	= "ultrix"
	set TAPES	= "/dev/[n]rmt[0-7]*"
	set shlib	= "no"
	set LIBFILES    = ""
        breaksw
    default:      
	echo "ERROR: Unable to determine platform architecture."
	exit 1
endsw

###################### END OF MACHDEP DEFINITIONS. ########################


#=============================================================================
# Initializations.
#=============================================================================
set errstat	= 0				# initialize errstat
set ok_count 	= 0
set err_count 	= 0
set warn_count 	= 0
set defstat 	= "OK"


#=============================================================================
# Process any command line arguments.
#=============================================================================

set early_exit	= 0
set general 	= 0
set severe 	= 0

while ("$1" != "")
    switch ("$1")
    case -G:				# Output General Info only
	set general     = 1
	set early_exit	= 1
	goto General
	breaksw
    case -V:				# Verification Test Only
	set early_exit	= 1
	breaksw
    case -N:				# Test Networking Only
	set early_exit	= 1
	goto Networking
	breaksw
    case -P:				# Test Packages Only
	set early_exit	= 1
	goto External_Packages
	breaksw
    case -D:				# Test Display Only
	set early_exit	= 1
	goto Image_Display
	breaksw
    case -T:				# Test Tapes Only
	set early_exit	= 1
	goto Tape_Device
	breaksw

    case -help:
    case -h:
	goto Usage
    default:
	echo "sysinfo: unknown argument $1"
	breaksw
    endsw

    if ("$2" == "") then
	break
    else
	shift
    endif
end



#=============================================================================
#  General Information
#=============================================================================

General:

echo 
echo "			General Information"
echo "			-------------------"

#  Document the system information.
echo ""
echo "Host name:	    "`hostname`
echo "Operating System:   "`uname -a`
echo "Architecture:	    "$mach
echo "HSI architecture:   "$hmach

echo ""

# Check the iraf root directory stuff
echo -n "IRAF environment path:	    "
if ($?iraf == 1) then
    set env_iraf = $iraf
    echo "$iraf"
else
    set env_iraf = ""
    echo "<undefined>"
endif

echo -n "Checking for <iraf.h> 	"
if (-e /usr/include/iraf.h) then
    echo ""
    echo -n "	IRAF path:	    "
    set ip = `grep "^#define	IRAF" /usr/include/iraf.h | sed -e 's/\"//g'`
    if ("$ip" != "") then
        echo $ip[3]
        set irafh_iraf = $ip[3]
    else
	echo "not found"
    endif

    echo -n "	HOST path:	    "
    set ip = `grep "^#define	HOST" /usr/include/iraf.h | sed -e 's/\"//g'`
    if ("$ip" != "") then
        echo $ip[3]
    else
	echo "not found"
    endif

    echo -n "	TMP path:	    "
    set ip = `grep "^#define	TMP" /usr/include/iraf.h | sed -e 's/\"//g'`
    if ("$ip" != "") then
        echo $ip[3]
    else
	echo "not found"
    endif
else
    echo "    not found"
    echo ""
    echo "	*** The file /usr/include/iraf.h was not found on this system"
    echo "	*** which means the install script was not run on this machine."
    if ("$env_iraf" != "") then
        echo "	*** Using environment definition: $env_iraf"
    endif
    echo ""
    set irafh_iraf = ""
endif

if ("$env_iraf" != "" && "$irafh_iraf" != "") then
  if ("$env_iraf" != "$irafh_iraf") then
    echo ""
    echo "WARNING: iraf environment and <iraf.h> root directories differ."

    # First make sure we have a valid path.
    if ( !(-e $env_iraf/unix/hlib/zzsetenv.def)) then
	echo "	    ***  Environment definition looks incorrect."
        echo "	    ***  Using <iraf.h> definition '$irafh_iraf'"

	# Strip off any trailing '/'.
    	set iraf  = `echo $irafh_iraf  | sed -e 's+/\(["]*\)$+\1+'`

    else if ( !(-e $irafh_iraf/unix/hlib/zzsetenv.def)) then
	echo "	    ***  <iraf.h> definition looks incorrect."
        echo "	    ***  Using environment definition '$env_iraf'"

	# Strip off any trailing '/'.
    	set iraf  = `echo $env_iraf  | sed -e 's+/\(["]*\)$+\1+'`
    endif

    echo ""

  else
     if ("$env_iraf" != "") then
       set iraf  = `echo $env_iraf  | sed -e 's+/\(["]*\)$+\1+'`
     else
       set iraf  = `echo $irafh_iraf  | sed -e 's+/\(["]*\)$+\1+'`
     endif
  endif

else if ("$env_iraf" == "" && "$irafh_iraf" == "") then
  echo ""
  echo "	*** No 'iraf' defined in the environment and there is no"
  echo "	*** <iraf.h> file on this machine.  Please specify an iraf"
  echo "	*** path in the environment or run the install script to"
  echo "	*** proceed."
  echo ""
  exit 1

else if ("$env_iraf" == "") then
    # Strip off any trailing '/'.
    set iraf  = `echo $irafh_iraf  | sed -e 's+/\(["]*\)$+\1+'`

else if ("$irafh_iraf" == "") then
    # Strip off any trailing '/'.
    set iraf  = `echo $env_iraf  | sed -e 's+/\(["]*\)$+\1+'`
endif


echo ""
echo "Using iraf root path:       $iraf"
echo ""

# Initialize for files we'll be checking later.
set tapecap_file  = "$iraf/dev/tapecap"
set termcap_file  = "$iraf/dev/termcap"
set graphcap_file = "$iraf/dev/graphcap"



# Get the motd header string.
echo -n 'IRAF version:	    	    '
if (-e $iraf/unix/hlib/motd) then
    set motd = "$iraf/unix/hlib/motd"
    set ver = `head -1 $motd | sed -e 's/NOAO//g' -e 's/EXPORT//g'`
    echo $ver
else 
    echo "<unknown>"
endif
echo ""


# Get the last time the hlib$utime (install) was updated.
echo -n "Install script last run:    "
if (-e $iraf/unix/hlib/utime) then
    set dat = `$LS -lLtg $iraf/unix/hlib/utime | head -2 | tail -1`
    set utime = `echo -n $dat | awk '{printf ("%3s %2s %-5s\t", $6, $7, $8)}'`
    echo $utime
else 
    echo "<unknown>"
endif

# Look around for the local iraf commands directory.
echo -n "IRAF command directories:   "
set clpath = `which cl`
set lbins = ""
if ("`echo $clpath | grep 'Command not found'`" == "") then
    set d_lbin = $clpath:h

    foreach p ($path)
        if (-d $p) then
            if (-e $p/cl) then
                set lbin = $p
                set lbins = `echo $lbins $p`

		if ("$lbins" == "$lbin") then
    		    echo $p
		else
    		    echo "			    $p"
		endif
	    endif
	endif
    end

    if ("$lbin" == "") then
        echo "not found"
    endif
else
    echo "not found"
endif


# If we use a shared library see if it's in the path someplace.
if ("$shlib" == "yes") then
    echo -n "Local shared lib directory: "
    set llib = ""
    set llibs = ""

    # Look around and come up with a likely candidate directory.
    set sysp  = "/usr/local/lib /local/lib /usr/lib /var/shlib /lib"
    if ($?LD_LIBRARY_PATH) then
        set userp = `echo $LD_LIBRARY_PATH | sed -e 's/:/ /g'`
    else
        set userp = ""
    endif
    foreach p ($sysp $userp)
        if (-d $p) then
	    foreach l ($LIBFILES) 
        	if (-e $p/$l) then
        	    set llib = $p
                    set llibs = `echo $llibs $p`

		    if ("$llibs" == "$llib") then
    		        echo $p
		    else
    		        echo "			    $p"
		    endif
		endif
	    end
	endif
    end

    if ("$llib" == "") then
        echo "not found"
    else
        echo ""
    endif
endif


# Get the default imdir from the mkiraf.csh
echo -n "Default image storage dir:  "
set hlib = $iraf/unix/hlib/
if (-e $hlib/mkiraf.csh) then
    set p = `grep "^set	imdir" $hlib/mkiraf.csh | sed -e 's/\"//g'`
    if ("$p" != "") then
        echo $p[4]
        set imdir = $p[4]
    else
	echo "not found"
    endif
else 
    echo "<unknown>"
endif


# Print some interesting default system settings from hlib$zzsetenv.def.
set WS	= '[ 	]'
set ZZDEFS = "printer stdplot editor imtype stdimage cmbuflen min_lenuserarea graphcap termcap tapecap"

echo ""
echo 'Default settings in the hlib$zzsetenv.def file:'

if (-e $hlib/zzsetenv.def) then
    foreach i ($ZZDEFS)
        set p = `grep "^set$WS$i" $hlib/zzsetenv.def | sed -e 's/\"//g'`
        if ("$p" != "") then
	    set $i = '$p[4]'
            echo ${i}: $p[4] | awk '{ printf ("    %-12s\t    %s\n", $1, $2)}'

	    # Save the *cap file for later use.
	    if ("$i" == "termcap" && "$p[4]" != 'dev$termcap') then
		set termcap_file = $p[4]
	    else if ("$i" == "graphcap" && "$p[4]" != 'dev$graphcap') then
		set graphcap_file = $p[4]
	    else if ("$i" == "tapecap" && "$p[4]" != 'dev$tapecap') then
		set tapecap_file = $p[4]
	    endif
        else
            echo ${i}: | awk '{ printf ("    %-12s\tnot found\n", $1, $2)}'
        endif
    end
else
    echo '	*** hlib$zzsetenv.def file not found'
    set severe = 1
endif


echo ""
echo ""
echo "Compilers and Development Tools Available:"
set found = 0
foreach i (cc gcc acc c89 f77 g77 f90 fort f2c yacc bison lex flex)
    set p = `which $i |& grep -i "^\/"`
    if ($status == 0) then
        echo "    "$p
	set found = 1
    endif
end
if ($found == 0) then
  echo "        None Found"
  echo ""
endif


if ($severe == 1) then
  echo ""
  echo ""
  echo "======================================================================"
  echo "SYSINFO:  Aborting due to severe errors."
  echo "======================================================================"
  echo ""
  exit 1

else if ($general == 1) then
    goto exit_early
endif

#=============================================================================
#  Installation Verification
#=============================================================================

Verification:

echo; echo ""
echo "======================= Verifying Installation ======================="
echo ""

##############################################################################
#
#  Step 1: VERIFICATION
#
#  	Run some simple checks to be sure the system was unpacked correctly 
#  and the settings used are correct.  Verification tests include:
#
#       o  Verify the machine type and document settings.
#       o  Check iraf root directory looks correct.
#       o  Check iraf root and imdir aren't the same
#       o  Check iraf user exists in passwd file.
#       o  Check iraf user login path in passwd file is iraf$local.
#       o  Check iraf tree for proper structure.
#       o  Check iraf tree is owned by 'iraf'
#       o  Check binary dirs are both populated correctly.
#       o  Check that the local bin directory exists.
#       o  Check that the local lib directory exists.
#
#  An error at this stage will cause the script to exit so we can reset and
#  try again.  
#
##############################################################################


# ============================================
# The following is partially system dependent.
# ============================================

# Set the BINDIRS pathnames - directories where the HSI executables go.
set host        = "$iraf/unix"
set hbin        = "$iraf/unix/bin.$hmach"
set hlib        = "$iraf/unix/hlib"
set fbin        = "$iraf/bin"

# Replace any // by /.
set host        = `echo $host | sed -e "s+//+/+g"`
set hbin        = `echo $hbin | sed -e "s+//+/+g"`
set fbin        = `echo $fbin | sed -e "s+//+/+g"`
set hlib        = `echo $hlib | sed -e "s+//+/+g"`

# Strip any trailing /.
set host        = `echo $host | sed -e 's+/\(["]*\)$+\1+'`
set hbin        = `echo $hbin | sed -e 's+/\(["]*\)$+\1+'`
set fbin        = `echo $fbin | sed -e 's+/\(["]*\)$+\1+'`
set hlib        = `echo $hlib | sed -e 's+/\(["]*\)$+\1+'`
set BINDIRS     = "$hbin $hlib $fbin $host"

# The following file lists are partially system dependent.
set PATHFILES   = "mkiraf.csh libc/iraf.h cl.csh"
set MODEFILES   = "cl.csh fc.csh mkiraf.csh mkfloat.csh mkmlist.csh $host/reboot generic.e mkpkg.e rmbin.e rmfiles.e rpp.e rtar.e wtar.e xc.e xpp.e xyacc.e sgidispatch.e $hbin/sgi2*.e"
set LINKFILES   = "cl.e mkiraf.csh mkmlist.csh generic.e mkpkg.e rmbin.e rmfiles.e rtar.e sgidispatch.e wtar.e rpp.e xpp.e xyacc.e xc.e"
set CMDLINKS    = "cl mkiraf mkmlist generic mkpkg rmbin rmfiles rtar sgidispatch wtar rpp xpp xyacc xc"
# ------------------------------------------


# Check for <iraf.h> file.
echo -n "Checking for <iraf.h> file ...				"
if (! (-e /usr/include/iraf.h)) then
  echo "[ FAILED ]" ; set errstat = 1 ; set iraf_root_ok = 0
  echo ""
  echo "    *** The file /usr/include/iraf.h was not found which means"
  echo "    *** the install script was not run on this machine."
  echo ""
  set err_count = `expr $err_count + 1`
else
  echo "[ OK ]" ; set iraf_root_ok = 1
  set ok_count = `expr $ok_count + 1`
endif


# Check $iraf path in PATHFILES
echo -n "Checking iraf path in system files ...			"

set err_seen = 0
foreach i ($PATHFILES)
    if (-e $iraf/unix/hlib/$i) then
        grep $iraf $iraf/unix/hlib/$i >& /dev/null
    endif
    if ($status == 1 || (! (-e $iraf/unix/hlib/$i))) then
        if ("$err_seen" == 0) then
            echo "[ FAILED ]"
            set err_seen = 1 ; set err_count = `expr $err_count + 1`
        endif
        echo "   ***  File $i not found or contains wrong iraf path"
    endif
end
if ("$err_seen" == 0) then
    echo "[ $defstat ]"
endif



# Check that the specified local bin directory exists.
echo -n "Checking that local command bin directory exists ...	"
if ("$lbin" == "") then
  echo "[ FAILED ]" ; set errstat = 1
  echo ""
  echo "    *** No local bin directory found on this machine which"
  echo "    *** implies that either your path does not include the"
  echo "    *** local bin dir or else the install script was not run"
  echo "    *** on this machine."
  echo ""
  set err_count = `expr $err_count + 1`
else
  if (-d "$lbin") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
  else
    echo "[ FAILED ]" ; set errstat = 1
    echo ""
    echo "    *** The specified local bin directory does not exist.  This"
    echo "    *** directory should be a common local bin directory which "
    echo "    *** is found in all user's paths, e.g. /usr/local/bin."
    echo "    *** Please create the directory or else reset and try again."
    echo ""
    set err_count = `expr $err_count + 1`
  endif
endif


# Check that the specified local lib directory exists.
if ("$shlib" == "yes") then
  echo -n "Checking that local lib directory exists ...		"
  if (-d "$llib") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
  else
    echo "[ FAILED ]" ; set errstat = 1
    echo ""
    echo "    *** The specified local lib directory does not exist.  This"
    echo "    *** directory should be a common local lib directory which "
    echo "    *** is found in all user's paths, e.g. /usr/local/lib."
    echo "    *** This directory is required for the iraf shared library."
    echo "    *** Please create the directory or else reset and try again."
    echo ""
    set err_count = `expr $err_count + 1`
  endif
endif


# Check mode on MODEFILES
echo -n "Checking iraf file permissions ...			"
set err_seen = 0
foreach i ($MODEFILES)
    set file = $i
    if (! -e $file) then
        foreach j ($BINDIRS)
            if (-e $j/$i) then
                set file = $j/$i
                break
            endif
        end
    endif
        
    if (-e $file) then
        if ("`$LS -l $file | grep '.rwxr.xr.x'`" == "") then
            if ("$err_seen" == 0) then
                echo "[ WARNING ]"
                set err_seen = 1 ; set err_count = `expr $err_count + 1`
            endif
            echo "    ***  File $file:t not mode 0755."
        endif
    else
        if ("$err_seen" == 0) then
            echo "[ FAILED ]"
            set err_seen = 1 ; set err_count = `expr $err_count + 1`
        endif
        echo "   ***  File $file:t not found."
    endif
end
if ("$err_seen" == 0) then
    echo "[ $defstat ]"
endif


# Check valid links on CMDLINKS
echo "Checking iraf command links ..."
set err_seen = 0
foreach p ($lbins)
    echo -n "    Checking command dir $p ...		"

    foreach cmd ($CMDLINKS)

        # Locate the file to be linked to.
        set file1 = $cmd:r
        foreach j ($BINDIRS)
            set file2 = $j/$file1.csh
            if (-e $file2) then
                break
            endif
            set file2 = $j/$cmd.e
            if (-e $file2) then
                break
            endif
        end

	# See first if it exists.
	if (! (-e $p/$cmd)) then
            if ("$err_seen" == 0) then
                echo "[ FAILED ]"
                set err_seen = 1 ; set err_count = `expr $err_count + 1`
            endif
            echo "	***  File $p/$cmd not found."

	# Then check that the link is correct
	else if ("`$LS -l $p/$file1 | grep $file2`" == "") then
            if ("$err_seen" == 0) then
                echo "[ FAILED ]"
                set err_seen = 1 ; set err_count = `expr $err_count + 1`
            endif
            echo "	***  Link $p/$cmd is invalid."
	endif
    end
    if ("$err_seen" == 0) then
        echo "[ $defstat ]"
    endif
end
if ($err_seen == 1) then
  echo ""
  echo "    *** 1) A 'not found' error means that one or more of the commands"
  echo "    ***    normally installed by the install script was not found."
  echo "    ***    This may indicate a non-standard installation."
  echo "    *** 2) A 'link is invalid' message means that the command link"
  echo "    ***    was found but either points to a nonexistent file or to"
  echo "    ***    the wrong file for this platform (e.g. the HSI bin.sparc"
  echo "    ***    directory instead of bin.ssol)."
  echo "    *** 3) Multiple local bin directories are not strictly an error"
  echo "    ***    but may confuse users.  Unneeded links should be removed."
  echo "    ***"
  echo "    *** The command links should be checked in either cases and the"
  echo "    *** install script rerun to clear the error."
  echo ""
endif


# Check iraf root directory looks correct.
echo ""
echo -n "Checking contents of iraf root directory ...		"
if (! ((-d $iraf/dev) && (-d $iraf/pkg) && (-d $iraf/noao))) then
  echo "[ FAILED ]" ; set errstat = 1 ; set iraf_root_ok = 0
  echo ""
  echo "    ***  The definition of '$iraf' looks incorrect.  The iraf root"
  echo "    ***  directory is the place where the AS distribution was unpacked,"
  echo "    ***  it contains subdirectories such as 'dev', 'local', 'noao', and"
  echo "    ***  'pkg' and the binary directory links."
  echo ""
  set err_count = `expr $err_count + 1`
else
  echo "[ OK ]" ; set iraf_root_ok = 1
  set ok_count = `expr $ok_count + 1`
endif


# Cannot have iraf and imdir the same.
echo -n "Checking iraf root and imdir directory ...		"
if ($iraf == $imdir) then
  echo "[ FAILED ]" ; set errstat = 1
  echo ""
  echo "    ***  'imdir' shouldn't be the same as the iraf root directory."
  echo ""
  set err_count = `expr $err_count + 1`
else
  echo "[ OK ]"
  set ok_count = `expr $ok_count + 1`
endif

# Check iraf user.
echo -n "Checking for iraf user account ...    			"
set pass = ""
if (!(-r /etc/passwd)) then
  echo "[ FAILED ]" ; set errstat = 1
  echo ""
  echo "    *** The /etc/passwd file is not readable so I can't check for"
  echo "    *** and iraf user."
  echo ""
else
  set pass = `grep ^iraf: /etc/passwd`
  if ("$pass" == "") then
    echo "[ FAILED ]" ; set errstat = 1
    echo ""
    echo "    *** No 'iraf' user was found in the /etc/passwd file.  The iraf"
    echo "    *** user should be created before installing the system to ensure"
    echo "    *** all files are owned by the iraf user, and the have the proper"
    echo "    *** environment defined for installation and maintanence."
    echo ""
  else
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`

    # Check iraf user login path in passwd file is iraf$local.
    echo -n "Checking iraf user login directory ...			"
    set pass = `grep ^iraf: /etc/passwd | sed -e 's/ /_/g'|sed -e 's/:/ /g'`

    set c = `echo $pass | wc -w`
    set indx = `expr $c - 1`

    set ihome = `echo $pass[$indx] | sed -e 's+/\(["]*\)$+\1+'`
    set shel =  `echo $pass[$c] | sed -e 's+/\(["]*\)$+\1+' | grep csh`
    if ("$ihome" != "$iraf/local" || $shel == "") then
      echo "[ FAILED ]" ; set errstat = 1
      echo ""
      echo "    *** The iraf user login directory appears to be incorrect."
      echo "    *** For the given iraf root this path should be '$iraf/local',"
      echo "    *** please edit the /etc/passwd file to change this.  The iraf"
      echo "    *** user account should also be defined to use a C-shell."
      if ("$iraf_root_ok" == 0) then
        echo "	*** (This error may be related to the incorrect definition of"
        echo "    *** the iraf root directory seen above.)"
      endif
      echo ""
    else
      echo "[ OK ]"
      set ok_count = `expr $ok_count + 1`
    endif
  endif
endif


# Check iraf tree for proper structure.
set iraf_r  = $iraf			# iraf root directory
set iraf_p  = $iraf_r:h			# iraf parent directory
set iraf_b  = $iraf_p/irafbin		# irafbin directory
set iraf_ib = $iraf_b/bin.$mach		# irafbin IB directory
set iraf_nb = $iraf_b/noao.bin.$mach	# irafbin NB directory
set iraf_tree_ok = 1

echo "Checking for proper iraf tree structure in $iraf_p ..."

echo -n "    Checking for 'iraf' subdir ...			"
if (-d "$iraf_p/iraf") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
else
    echo "[ FAILED ]" ; set errstat = 1 ; set iraf_tree_ok = 0
    set err_count = `expr $err_count + 1`
endif

echo -n "    Checking for 'irafbin' subdir ..."
if (-d "$iraf_p/irafbin") then
    echo "			[ OK ]"
    set ok_count = `expr $ok_count + 1`
else
    set temp    = $iraf_tree_ok
    echo "not found" ; set errstat = 1 ; set iraf_tree_ok = 0

    # Look for a fallback to recover ...
    echo -n "    Checking for fallback tree structure ...		"
    set iraf_p = $iraf/../
    set iraf_b  = $iraf_p/irafbin		# irafbin directory
    set iraf_ib = $iraf_b/bin.$mach		# irafbin IB directory
    set iraf_nb = $iraf_b/noao.bin.$mach	# irafbin NB directory
    if (-d "$iraf_p/irafbin") then
        echo "[ OK ]" ; set errstat = 0 ; set iraf_tree_ok = $temp
        set ok_count = `expr $ok_count + 1`
    else
        echo "[ FAILED ]" ; set errstat = 1 ; set iraf_tree_ok = 0
        set err_count = `expr $err_count + 1`
    endif
endif

echo -n "    Checking for 'irafbin/bin.$mach' subdir ...     	"
if (-d "$iraf_p/irafbin/bin.$mach") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
else
    echo "[ FAILED ]" ; set errstat = 1 ; set iraf_tree_ok = 0
    set err_count = `expr $err_count + 1`
endif

echo -n "    Checking for 'irafbin/noao.bin.$mach' subdir ...	"
if (-d "$iraf_p/irafbin/noao.bin.$mach") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
else
    echo "[ FAILED ]" ; set errstat = 1 ; set iraf_tree_ok = 0
    set err_count = `expr $err_count + 1`
endif


if ("$iraf_tree_ok" == 0) then
  set back = $cwd ; chdir $iraf_p ; set iraf_p = $cwd; chdir $back
  echo ""
  echo "	***  An error was detected in the structure of the iraf tree."
  echo "	***  Your directory tree should look something like:"
  echo "	***"
  echo "        ***         	$iraf_p"
  echo "        ***         	  /  \"
  echo "        ***	(AS) /iraf  /irafbin"
  echo "        ***                   /  \"
  echo "        *** 	(IB) bin.$mach  noao.bin.$mach (NB)"
  echo "	***"
  echo "	***  The AS, IB, and NB distribution files are shown where they"
  echo "	***  should be unpacked."
  echo ""
endif


echo -n "    Checking file ownerships ... 			"

if ($mach == "hp700" || $mach == "rs6000" || $mach == "irix") then
    set o1 = `$LS -lLd $iraf_p/iraf | awk '{print ($4)}'`
    set o2 = `$LS -lLd $iraf_p/iraf/mkpkg | awk '{print ($4)}'`
else
    set o1 = `$LS -lLd $iraf_p/iraf | awk '{print ($3)}'`
    set o2 = `$LS -lLd $iraf_p/iraf/mkpkg | awk '{print ($3)}'`
endif

if ("$o1" == "iraf" && "$o2" == "iraf") then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
else
    echo "[ FAILED ]"
    echo ""
    echo "	***  The iraf tree should be owned by the iraf user so"
    echo "	***  it can be updated and maintained properly."
    echo "	***  (iraf root owned by $o1, iraf files owned by $o2)"
    echo ""
    set errstat = 1 ; set err_count = `expr $err_count + 1`
endif


# Check that binary dirs are populated correctly.
set  archs = ""
set back = $cwd ; chdir $iraf_p ; set iraf_p = $cwd; chdir $back

echo "" ; echo ""
echo "Core system binaries in $iraf_p/irafbin ..."
echo ""
echo "			 Size	Date			Status"
echo "			 ----	----			------"
foreach i ($iraf_p/irafbin/bin.*)
  set dir = $i:t
  if ($dir:r == "bin") then
    set sz = `(chdir $i ; du -ks | awk '{printf ("%d", $1)}')`
    echo -n $dir $sz | awk '{printf ("%18s\t%5d\t", $1, $2)}'

    if (`$LS -lL $i | wc -l` > 1) then
      set dat = `$LS -lLtg $i/* | head -2 | tail -1`
      echo -n $dat | awk '{printf ("%3s %2s %-5s\t", $6, $7, $8)}'

      if (! (-e "$i/cl.e" && -e "$i/x_system.e")) then
        echo "	[ FAILED ]" ; set errstat = 1
        echo ""
        echo "    *** The core system binary directory, $iraf_ib, does"
        echo "    *** not appear to contain the proper binaries.  The IB dist-"
        echo "    *** ribution files should be unpacked in this directory."
        echo ""
        set err_count = `expr $err_count + 1`
      else
        echo "	[ OK ]"
        set ok_count = `expr $ok_count + 1`

	# Save the list of installed binaries, allow for changes between
	# the binary arch and HSI arch here (e.g. ssun->ssol).
	if ("$dir:e" == "ssun") then
            set archs = `echo $archs ssol`
	else
            set archs = `echo $archs $dir:e`
	endif
      endif

    else
	echo "< empty >		[ FAILED ]" ; set errstat = 1
        echo "	*** bin directory is empty or does not contain proper HSI binaries."
    endif
  endif
end


echo ""
echo ""
echo "NOAO Package binaries in $iraf_p/irafbin ..."
echo ""
echo "			 Size	Date			Status"
echo "			 ----	----			------"
foreach i ($iraf_p/irafbin/noao.bin.*)
  set dir = $i:t
  if ($dir:r == "noao.bin") then
    set sz = `(chdir $i ; du -ks | awk '{printf ("%d", $1)}')`
    echo -n $dir $sz | awk '{printf ("%18s\t%5d\t", $1, $2)}'

    if (`$LS -lL $i | wc -l` > 1) then
      set dat = `$LS -lLtg $i/* | head -2 | tail -1`
      echo -n $dat | awk '{printf ("%3s %2s %-5s\t", $6, $7, $8)}'

      if (! (-e "$i/x_apphot.e" && -e "$i/x_rv.e")) then
        echo "	[ FAILED ]" ; set errstat = 1
        echo ""
        if (-e "$iraf_ib/x_apphot.e" && -e "$iraf_ib/x_rv.e") then
         echo "    *** The NOAO package binary directory, $iraf_nb, is"
         echo "    *** empty but the binaries appear to have been unpacked in"
         echo "    *** the core system directory, $iraf_ib.  These will need to"
         echo "    *** be moved, please delete the binaries and start again, be"
         echo "    *** sure to unpack the NB distribution files in the $iraf_nb"
         echo "    *** directory and the core system file in the $iraf_ib"
	 echo "    *** directory."
        else
         echo "    *** The NOAO package binary directory, $iraf_nb, does"
         echo "    *** not appear to contain the proper files.  The NB dist-"
         echo "    *** ribution files should be unpacked in this directory."
        endif
        echo ""
        set err_count = `expr $err_count + 1`
      else
        echo "	[ OK ]"
        set ok_count = `expr $ok_count + 1`
      endif

    else
	echo "< empty >		[ FAILED ]" ; set errstat = 1
        echo "    *** bin directory is empty or does not contain HSI binaries."
    endif

  else
    continue
  endif
end


# Check the HSI binaries.
echo ""
echo ""
echo "HSI system binaries in $iraf/unix ..."
echo ""
echo "			 Size	Date			Status"
echo "			 ----	----			------"

set  delbin = ""
foreach i ($iraf_p/iraf/unix/bin.*)
  set dir = $i:t
  if ($dir:r == "bin") then
    set sz = `(chdir $i ; du -ks | awk '{printf ("%d", $1)}')`
    echo -n $dir $sz | awk '{printf ("%18s\t%5d\t", $1, $2)}'

    if (`$LS -lL $i | wc -l` > 1) then
      set d = `$LS -lLtg $i/* | head -2 | tail -1`
      echo -n $d | awk '{printf ("%3s %2s %-5s\t", $6, $7, $8)}'

      if (! (-e "$i/alloc.e" && -e "$i/xc.e")) then
        echo "	[ FAILED ]	" ; set errstat = 1
      else
        echo "	[ OK ]"
        set ok_count = `expr $ok_count + 1`
      endif
      if ("`echo $archs | grep $dir:e`" == "") then
	set delbin = `echo $delbin $dir`
      endif

    else
	echo "< empty >		[ WARNING ]" ; set errstat = 1
	if ($sz > 8 && "`echo $archs | grep $dir:e`" == "") then
	    set delbin = `echo $delbin $dir`
	endif
        set warn_count = `expr $warn_count + 1`
    endif

  else
    continue
  endif
end
echo ""

if ("$delbin" != "") then
  echo ""
  echo "	*** The following bin directories were found to be"
  echo "	*** unneeded for this installation.  If disk usage is a"
  echo "	*** concern the contents may be deleted to reclaim space:"
  echo "	***"
  foreach i ($delbin)
    echo "	***	  $iraf/unix/$i"
  end
endif
echo ""

if ($early_exit == 1) then
    goto exit_early
endif

#=============================================================================
#  Networking Information
#=============================================================================

Networking:

set net_errstat = 0			# initialize error status

echo ""
echo "======================= Networking Information ======================="
echo ""

set hname	= `hostname`
set lhost_abbr	= `hostname | awk '{printf ("%16.16s\n", $1 ) }'`
set ihosts 	= $iraf/dev/hosts
set nhost_files	= `$LS -1 $iraf/dev/hosts* | wc -l`

echo 'Local host name:           '$hname
echo 'Truncated host name:       '$lhost_abbr
if (`echo $hname | grep "\."` != "") then
    # When using FQDN lnode may not be set....
    set is_fqdn	= yes
    set domain  = `hostname | sed -e 's/^[a-zA-Z0-9_\-]*\.//g'`
    set lhost = `hostname | sed -e 's/\.[a-zA-Z0-9]*//g'`
else
    set is_fqdn	= no
    set domain  = "<unknown>"
    set lhost = $hname
endif
echo 'Domain name:               '$domain
echo 'No. of dev$host* files:    '$nhost_files
echo 'Using IRAF hosts file:     '$ihosts


echo ""
echo -n 'Checking for iraf hosts file ...			'
if (-e $ihosts) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`

    set nhosts	= `grep irafks.e $ihosts | wc -l`
    set lnode	= `grep irafks.e $ihosts | sort | grep $lhost | head -1`

    # The following madness is required to workaround shortcomings in the
    # GNU versions of the sed command (i.e. Linux/FreeBSD).  Working sed 
    # commands for Sun here.
    #
    #  set n      = `cat $ihosts | sort | grep $lhost | head -1`
    #  set irafks = `echo $n | sed -e 's/^[a-zA-Z0-9 	_\!\/\-\.:]*//g'`
    #  set nalias = `echo $n | sed -e 's/:[a-zA-Z0-9 	_\!\/\-\.]*//g'`
    #  set rhs	  = `echo $n | sed -e 's/^[a-zA-Z0-9 	_\!\-\.]*://g'`
    #  set nnode  = `echo $rhs | sed -e 's/\![a-zA-Z0-9 	_\!\/\-\.]*//g'`

    set TEMP = "/tmp/_$$"
    echo $lnode | sed -e 's/\!/ /g'  > $TEMP
    set line = `cat $TEMP`
    /bin/rm -f $TEMP

    set nalias	= ""
    set irafks	= ""
    set nnode	= ""
    if ("$line" != "") then
        while ("$line[1]" != "")
	    if ("$line[1]" == ":") then
	        break
	    else
	        set nalias = `echo $nalias $line[1]`
	    endif
	    shift line
        end
        shift line
        set nnode  = $line[1] ; shift line
        set irafks = $line[1] ; shift line
    endif

    # Print out the entry information found.
    echo '    No. nodes in hosts file:   '$nhosts
    echo '    irafks.e pathname:         '$irafks
    echo '    Node aliases:              '$nalias
    echo '    Network node name:         '$nnode
    echo ""

    # Make sure the node is in the file.
    echo -n '    Checking for local node in hosts file ...		'
    if ("`grep $lhost $ihosts | head -1`" != "") then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
    else
        echo "[ FAILED ]" ; set net_errstat = 1
        echo ""
        echo "    ***  The local host, $lhost, was not found in the iraf hosts"
	echo "    ***  file $iraf/dev/hosts.  This means that IRAF networking"
	echo "    ***  will not be available from this machine to others in the"
	echo "    ***  iraf network."
        echo ""
        set err_count = `expr $err_count + 1`
    endif

    # See if the irafks.e path is correct on this machine.
    if ("$irafks" != "") then
      echo -n '    Checking for irafks.e binary ...			'
      if (-e $irafks) then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
      else
        echo "[ FAILED ]" ; set net_errstat = 1
        echo ""
        echo "    ***  The named irafks.e binary was not found on this system."
        echo ""
        set err_count = `expr $err_count + 1`
      endif
    endif

    # Look for duplicate host names which may trigger a CL bug.
    echo -n '    Checking for duplicate hosts in dev$hosts file ...	'
    set dup_hosts = `grep irafks.e $ihosts | sort | awk '{print $1}' | uniq -d`
    if ("$#dup_hosts" == 0) then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
    else
        echo "[ WARNING ]" ; set net_errstat = 1
        echo ""
        echo '    ***  Duplicate hosts found in dev$hosts file:'
	    grep irafks.e $ihosts | \
	    sort | \
	    awk '{print $1}' | \
	    uniq -d | \
	    awk '{printf("    ***\t\t%s\n", $1)}'
        echo "    ***"
        echo "    ***  Duplicate node names should be removed to ensure"
        echo "    ***  networking and the CL operate properly."
        echo ""
        set warn_count = `expr $warn_count + 1`
    endif

else
    echo "[ FAILED ]" ; set net_errstat = 1
    echo ""
    echo "    ***  The IRAF hosts file, $ihosts, was not found."
    echo ""
    set err_count = `expr $err_count + 1`
endif


# See what NETSTATUS says about this setup.
echo ""
echo -n 'Verify NETSTATUS says iraf networking is enabled ...	'
set system = $iraf/bin.$mach/x_system.e
if (-e $system) then
    set net = `$system netstatus | grep -i "interface disabled"`
    if ("$net" == "") then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
    else
        echo "[ FAILED ]" ; set net_errstat = 1
        echo ""
        echo "    ***  The NETSTATUS task claims that networking is disabled."
        echo ""
        set err_count = `expr $err_count + 1`
    endif
else
    echo "[ FAILED ]" ; set net_errstat = 1
    echo ""
    echo "    ***  The NETSTATUS task binary could not be executed."
    echo ""
    set err_count = `expr $err_count + 1`
endif


# See if we're in the trusted hosts file for rsh access.
echo ""
echo -n 'Checking for host in /etc/hosts.equiv ...		'
set equiv = /etc/hosts.equiv
if (-e $equiv) then
    if ("`grep $lhost $equiv | head -1`" != "") then
      echo "[ OK ]"
      set ok_count = `expr $ok_count + 1`
    else
      echo "[ WARNING ]" ; set net_errstat = 1
      echo ""
      echo "    ***  The local host, '$lhost', is not present in the $equiv"
      echo "    ***  file.  This may mean that the 'rsh' connection protocol"
      echo "    ***  is not available and IRAF networking will require password"
      echo "    ***  prompts. This is not a fatal error."
      echo ""
      set warn_count = `expr $warn_count + 1`
    endif
else
    echo "[ WARNING ]" ; set net_errstat = 1
    echo ""
    echo "    ***  The trusted hosts file, '$equiv', is not present on this"
    echo "    ***  system.  This may mean that the 'rsh' connection protocol"
    echo "    ***  is not available and IRAF networking will require password" 
    echo "    ***  prompts."
    echo ""
    set warn_count = `expr $warn_count + 1`
endif


# Print out the recommended dev$hosts entry for this machine.
echo ""
echo "----------------------------------------------------------------------"
echo ""
echo "Recommended $ihosts file entry for this machine: "
echo ""
echo -n "  "
if ("$is_fqdn" == "no") then
    echo $lhost "	: "$hname"\!"$iraf/bin.$mach/irafks.e
else
    echo $lhost $lhost_abbr " : " $nnode"\!"$iraf/bin.$mach/irafks.e
endif
echo ""


if ($early_exit == 1) then
    goto exit_early
endif

#=============================================================================
#  Image Display Device Information
#=============================================================================

Image_Display:

echo ""
echo "===================== Image Display Device Info ======================"
echo ""

if (! ($?termcap_file)) then
    set termcap_file  = "$iraf/dev/termcap"
endif
if (! ($?graphcap_file)) then
    set graphcap_file = "$iraf/dev/graphcap"
endif

set imtoolrc	  = /usr/local/lib/imtoolrc

echo -n "Display Servers Available:				"
set found = 0
set have_sockets = 0
foreach i (ximtool saoimage ds9 saotng)
    set p = `which $i |& grep -i "^\/"`
    if ($status == 0) then
	if ("$found" == 0) then
	    echo "[ OK ]"
            set ok_count = `expr $ok_count + 1`
	endif
	set d = `$LS -lLtg $p | head -2 | tail -1`
	echo $d | awk '{printf ("    ( Date: %3s %2s %-5s )\t", $6, $7, $8) }'
	echo "    "$p
        set found = 1
	if ("$i" != "saoimage") then
	    set have_sockets = 1
	endif
    endif
end

if ($found == 0) then
  echo "[ FAILED ]" ; set errstat = 1
  echo "	None Found"
  echo ""
  echo "    *** No display servers were found on this machine or in the user"
  echo "    *** path.  A display server such as XImtool/SAOimage/SAOtng/DS9"
  echo "    *** is required to be running on the local machine before an iraf"
  echo "    *** display command (e.g. DISPLAY/TVMARK/IMEXAMINE) will work."
  echo "    ***"
  echo "    *** Remote displays (i.e. the server on one machine and IRAF on"
  echo "    *** another) require either iraf networking be enabled or the X"
  echo "    *** 'DISPLAY' variable be set so the server appears on the remote"
  echo "    *** machine."
  echo "    ***"
  echo "    *** XImtool can be downloaded from"
  echo "    ***"
  echo "    ***	        ftp://iraf.noao.edu/iraf/x11iraf"
  echo "    ***"
  echo "    *** or it's mirror sites.  Contact site support with questions."
  echo ""
  set err_count = `expr $err_count + 1`
endif
echo ""


echo -n "Graphics Terminals Available:				"
set found = 0
foreach i (xgterm xterm)
    set p = `which $i |& grep "^\/"`
    if ($status == 0) then
	if ("$found" == 0) then
	    echo "[ OK ]"
            set ok_count = `expr $ok_count + 1`
	endif
	set d = `$LS -lLtg $p | head -2 | tail -1`
	echo $d | awk '{printf ("    ( Date: %3s %2s %-5s )\t", $6, $7, $8) }'
	echo "    "`which $i`
        set found = 1
    endif
end
if ($found == 0) then
  echo "[ FAILED ]" ; set errstat = 1
  echo "	None Found"
  echo ""
  echo "    *** No 'xterm' or 'xgterm' binary was found on this systen or"
  echo "    *** in the user path.  IRAF graphics require some form of "
  echo "    *** graphics-enabled terminal window to be running or else"
  echo "    *** garbarge characters will appear on the screen.  Windows"
  echo "    *** such as 'cmdtool', 'rxvt', 'aixterm', 'hpterm', 'decterm'"
  echo "    *** do not supoort graphics and should not be used for IRAF."
  echo "    ***"
  echo "    *** The default terminal type is set in the login.cl when"
  echo "    *** a user runs MKIRAF, this is the type of window they should"
  echo "    *** be running when starting IRAF.  Users can use the 'show"
  echo "    *** terminal' command to see the current setting, or 'stty"
  echo "    *** xterm' or 'stty xgterm' command (or rerun MKIRAF and reset"
  echo "    *** the default terminal type) to change the default iraf terminal."
  echo "    ***"
  echo "    *** XGterm can be downloaded from"
  echo "    ***"
  echo "    ***	        ftp://iraf.noao.edu/iraf/x11iraf"
  echo "    ***"
  echo "    *** or it's mirror sites.  Contact site support with questions."
  set err_count = `expr $err_count + 1`
endif
echo ""


# Check for imtoolrc file and /usr/local/lib dir.
set errstat = 0
echo ""
echo -n "Checking for /usr/local/lib directory ...		"
if (-d /usr/local/lib) then
  echo "[ OK ]"
  set ok_count = `expr $ok_count + 1`

  # Check for imtoolrc file...
  echo -n "Checking for imtoolrc file ...				"
  if (-e $imtoolrc) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`

    set islink = `$LS -l $imtoolrc | grep "^l"`
    set file = $iraf/dev/imtoolrc
    if ("$islink" != "" && -e $file) then

      # Check for valid link
      echo -n "Checking for valid imtoolrc link ...			"
      set tmp = `diff $imtoolrc $file | wc -l`
      if ("$tmp" == 0) then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
      else
        echo "[ FAILED ]" ; set errstat = 1
        echo ""
        echo "    ***  The /usr/local/lib/imtoolrc link is invalid"
        echo ""
        set err_count = `expr $err_count + 1`
      endif
    endif

  else
    echo "[ FAILED ]" ; set errstat = 1
    echo ""
    echo "    ***  The /usr/local/lib/imtoolrc file is missing."
    echo ""
    set err_count = `expr $err_count + 1`
  endif


  # Check for imtoolcmap directory...
  echo -n "Checking for imtoolcmap directory ...			"
  if (-d /usr/local/lib/imtoolcmap) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`

  else
    echo "[ WARNING ]"
    echo ""
    echo "    ***  The /usr/local/lib/imtoolcmap directory is missing."
    echo "    ***  This directory is not required but provides extra"
    echo "    ***  colormap options for XImtool.  The colormaps may be"
    echo "    ***  be obtained from "
    echo "    ***  "
    echo "    ***  	ftp://iraf.noao.edu/iraf/x11iraf/imtoolcmap.tar"
    echo "    ***  "
    echo "    ***  This should be unpacked as the /usr/local/lib/imtoolcmap"
    echo "    ***  directory.  This is not a fatal error."
    echo ""
    set warn_count = `expr $warn_count + 1`
  endif


else
  echo "[ FAILED ]" ; set errstat = 1
  echo ""
  echo "    ***  The /usr/local/lib directory does not exist."
  echo ""
  set err_count = `expr $err_count + 1`
endif
if ($errstat == 1) then
  echo "    *** The imtoolrc file is used by the display servers (XImtool,"
  echo "    *** SAOimage, etc) to set various frame buffer sizes.  Without"
  echo "    *** this file the server can only use a 512x512 frame buffer,"
  echo "    *** and displays to larger buffers will result in the message"
  echo "    *** 'attempt to write out of bounds on framebuf'.  Sites can"
  echo "    *** rerun the install script or make the link by hand, users"
  echo '    *** can copy the dev$imtoolrc file to their $HOME directory as'
  echo "    *** '.imtoolrc' as a workaround."
endif

echo -n "Checking file descriptor limits:			"
set ds = `limit descriptors | awk '{print ($2)}'`
if ($ds > 64) then
    echo "[ WARNING ]"
    echo ""
    echo "    ***  SAOimage has a hardwired restriction of 64 file descriptors,"
    echo "    ***  however this user shell allows more.  When there are many"
    echo "    ***  windws open SAOimage may fail, this warning applies only"
    echo "    ***  to users of SAOimage.  A 'limit descriptors 64' command"  
    echo "    ***  can be used to reset the limit."
    echo ""
    set warn_count = `expr $warn_count + 1`
else
    echo "[ OK ]"
endif


# See whether we have fifo pipes installed.
set errstat = 0
foreach p (/dev/imt1i /dev/imt1o)
    echo -n "Checking for $p fifo pipe ...			"
    if (-e $p) then
        if ("`$LS -l $p | grep '.rw.rw.rw.'`" == "") then
	    if ($have_sockets == 1) then
                echo "[ WARNING ]" ; set errstat = 1 
	        set warn_count = `expr $warn_count + 1`
	    else
                echo "[ FAILED ]" ; set errstat = 1 
	        set err_count = `expr $err_count + 1`
	    endif
            echo "    ***  File $p not mode 666."
        else 
            echo "[ OK ]"
            set ok_count = `expr $ok_count + 1`
        endif
    else
	if ($have_sockets == 1) then
            echo "[ WARNING ]" ; set errstat = 1
            set warn_count = `expr $warn_count + 1`
	else
            echo "[ FAILED ]" ; set errstat = 1
            set err_count = `expr $err_count + 1`
	endif
        echo "    ***  The $p fifo pipe is missing."
    endif
end

echo -n "Checking for /dev/imt1 fifo pipe link ...		"
if (-e /dev/imt1) then
    if ("`$LS -l /dev/imt1 | grep imt1o`" != "") then
        echo "[ OK ]"
        set ok_count = `expr $ok_count + 1`
    else
	if ($have_sockets == 1) then
            echo "[ WARNING ]" ; set errstat = 1
            set warn_count = `expr $warn_count + 1`
	else
            echo "[ FAILED ]" ; set errstat = 1
            set err_count = `expr $err_count + 1`
	endif
        echo "    ***  The /dev/imt1 fifo pipe link is invalid."
    endif
else
    if ($have_sockets == 1) then
        echo "[ WARNING ]" ; set errstat = 1
        set warn_count = `expr $warn_count + 1`
    else
        echo "[ FAILED ]" ; set errstat = 1
        set err_count = `expr $err_count + 1`
    endif
    echo "    ***  The /dev/imt1 fifo pipe link is missing."
endif
if ($errstat == 1 && $have_sockets == 0) then
  echo ""
  echo "    *** The /dev fifo pipes are used only by SAOimage as a default"
  echo "    *** communication with IRAF or as a fallback for other display"
  echo "    *** servers such as XImtool.  Except for SAOimage or in the case"
  echo "    *** of some other mechanism such as a private graphcap file or"
  echo "    *** IMTDEV environment variable, missing /dev pipes should not be
  echo "    *** fatal for image display.  A 'Cannot open device' message may"
  echo "    *** simply mean there is no display server running, or the 'node'
  echo '    *** CL environment variable has been set to an invalid node or"
  echo "    *** iraf networking is not enabled on the host.  The pipes are"
  echo "    *** created by the install script (which can be run on this host)"
  echo "    *** or they can be created by hand as root with the 'mknod' or"
  echo "    *** 'mkfifo command."
  echo ""
endif


# Make sure there are entries in the the termcap and graphcap files for xgterm
# and imtool.

echo -n "Checking termcap file for an XGterm entry ...		"
set temp = `grep -l xgterm $termcap_file | grep -v "^#"`
if ("$temp" == "") then
    echo "[ FAILED ]" ; set errstat = 1
    set err_count = `expr $err_count + 1`
else
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
endif

echo -n "Checking graphcap file for XGterm/imtool entries ...	"
set gcok = yes
foreach i (xgterm imtool)
    set temp = `grep -l $i $graphcap_file | grep -v "^#"`
    if ("$temp" == "" && "$gcok" == "yes") then
        echo "[ FAILED ]" ; set errstat = 1
  	set err_count = `expr $err_count + 1`
        set gcok = no
    endif
end
if ($gcok == yes) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
endif

if ($early_exit == 1) then
    goto exit_early
endif

#=============================================================================
#  Tape Device Information
#=============================================================================

Tape_Device:

echo ""
echo "====================== Tape Device Information ======================="
echo ""

set errstat = 0

if (! ($?tapecap_file)) then
    set tapecap_file  = "$iraf/dev/tapecap"
endif

# Check the alloc.e binary.
echo ""
set file = "$iraf/unix/bin.$hmach/alloc.e"
echo -n 'Checking for hbin$alloc.e binary ...			'
if (-e $file) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`

    echo -n 'Checking hbin$alloc.e ownership ...			'
    if (-e $file) then
        if ("`$LS -l $file | grep 'root'`" == "") then
            echo "[ FAILED ]"
            set errstat = 1 ; set err_count = `expr $err_count + 1`
            echo "    ***  File $file:t not owned by root."
        else 
	    echo "[ OK ]"
            set ok_count = `expr $ok_count + 1`
        endif
    endif

    echo -n 'Checking hbin$alloc.e binary permissions ...		'
    if (-e $file) then
        if ("`$LS -l $file | grep '.rwsr.xr.x'`" == "") then
            echo "[ FAILED ]"
            set errstat = 1 ; set err_count = `expr $err_count + 1`
            echo "    ***  File $file:t not mode 4755."
        else 
	    echo "[ OK ]"
            set ok_count = `expr $ok_count + 1`
        endif
    endif

    if ($errstat == 1) then
      echo ""
      echo "    *** The alloc.e binary is used to allocate tape devices"
      echo "    *** for exclusive access by the user.  It does this by"
      echo "    *** changing the ownership and permissions on the /dev"
      echo "    *** files associated with each device.  For this reason"
      echo "    *** it must be owned by root with setuid permissions so"
      echo "    *** it can execute properly.  Tape devices are not required"
      echo "    *** to be allocated before they are used but executing the"
      echo "    *** ALLOC command w/in iraf will result in an error."
      echo "    *** This error can be cleared by running the IRAF install"
      echo "    *** script as root, or issuing the commands:"
      echo "    ***"
      echo "    ***         # cd $hbin"
      echo "    ***         # chown 0 alloc.e ; chmod 4755 alloc.e"
      echo ""
    endif

else
    echo "[ FAILED ]" ; set errstat = 1 
    set err_count = `expr $err_count + 1`
    echo "    ***  File $file not found."
endif


# Check for a tapecap file.
set errstat = 0
echo ""
echo -n 'Checking for tapecap file ...				'
if (-e $iraf/dev/tapecap.`hostname`) then
  echo "[ OK ]"
  set ok_count = `expr $ok_count + 1`
  echo "     Using file: $iraf/dev/tapecap.`hostname`"
else
  if (-e $tapecap_file) then
    echo "[ OK ]"
    set ok_count = `expr $ok_count + 1`
    echo "    Using tapecap file: $tapecap_file"
  else
    echo "[ FAILED ]" ; set errstat = 1 
    set err_count = `expr $err_count + 1`
  endif
endif
if ($errstat == 1) then
  echo ""
  echo '    *** No tapecap file found on this system.'
  echo ""
  echo '    *** IRAF will first attempt to find a file in the iraf$dev'
  echo '    *** directory called "tapecap.\<node\>", if that fails it will'
  echo '    *** fallback to use the dev$tapecap file (or whichever file is'
  echo '    *** named by the 'tapecap' variable in the hlib$zzsetenv.def file).'
  echo '    *** '
  echo '    *** Not all IRAF distributions come with a default tapecap file'
  echo '    *** appropriate for the given machine.  For example, the default'
  echo '    *** dev$tapecap for Sun/IRAF is for SunOS and will not generally'
  echo '    *** work for Solaris.  The PC-IRAF distribution comes with generic'
  echo '    *** tapecap.linux, tapecap.freebsd, etc files which must be renamed'
  echo '    *** "tapecap" to be used.  Generic device entries are provided with'
  echo '    *** each tapecap file (e.g. mtp for a DAT on unit 0) but in most'
  echo '    *** cases the tapecap file must be installed and/or configured'
  echo '    *** before devices will be accessible.'
  echo '    *** '
  echo '    *** Further information on configuring tapecaps can be found in'
  echo '    *** the last IRAF Newsletter at:'
  echo '    *** '
  echo '    ***      http://iraf.noao.edu/irafnews/apr98/irafnews.1f.html'
  echo '    *** '
  echo '    *** or by contacting site support (iraf@noao.edu).'
  echo ""
endif



echo ""
echo "Tape Device Template:		$TAPES"

set nloks = `$LS -lL /tmp/*.lok |& grep -v -i "no match" | wc -l`
if ($nloks == 0) then
    set nloks = "<none>"
endif
echo "Lok Files on this machine:	$nloks"
if ("$nloks" != '<none>') then
    $LS -lL /tmp/*.lok
endif

echo ""

set ntapes = `$LS -lL $TAPES |& grep -v -i "no match" | wc -l`
if ($ntapes == 0) then
    set ntapes = "<none>"
endif
echo "Tape Devices Available:		$ntapes"
echo ""

if ("$ntapes" != '<none>') then

echo ""
echo "    ******************************************************************"
echo "    ** More tape devices files may be defined than there are actual **"
echo "    ** devices on the machine.  For those file  which correspond to **"
echo "    ** a physical device the file should have mode 666 and be owned **"
echo "    ** by root.                                                     **"
echo "    ******************************************************************"
echo ""

    ls -lL $TAPES
endif

if ($early_exit == 1) then
    goto exit_early
endif


#=============================================================================
#  External Package Information
#=============================================================================

External_Packages:

echo ""
echo "=================== External Package Information ====================="
echo ""

# Check the iraf root directory stuff
if ($?iraf == 1) then
    set hlib = $iraf/unix/hlib
else
    if (-e /usr/include/iraf.h) then
        set ip = `grep "^#define    IRAF" /usr/include/iraf.h |sed -e 's/\"//g'`
        if ("$ip" != "") then
            set iraf = $ip[3]
        endif
        set hlib = $iraf/unix/hlib
    else
	echo "    *** No 'iraf' defined in your environment and no <iraf.h>"
	echo "    *** file found on this system."
	echo "    *** "
	echo "    *** Aborting..."
    endif
endif

set epkg = $hlib/extern.pkg


# Get the helpdb string.
set helpdb = `cat $epkg | grep -v "^#" | grep helpdb`


# Get the list of tasks defined in the extern.pkg file.
set t = `cat $epkg | grep -v "^#" | grep task | sed -e 's/task//g' -e 's/=//g' -e 's/\.pkg//g' -e 's/\$//g'`

set tasks = ""
while ("$t[1]" != "")
    set tasks = `echo $tasks $t[1]`
    shift t ; shift t
    if ("$#t" == "0") then
	break
    endif
end

# Get the variables and paths declared, includes data directories.

cat $epkg | grep -v "^#" | grep -v helpdb | grep ^set   >  /tmp/_ext$$
cat $epkg | grep -v "^#" | grep -v helpdb | grep ^reset >> /tmp/_ext$$

set l = `cat /tmp/_ext$$ | sed -e 's/reset//g' -e 's/set//g' -e 's/=//g'`

set pkg   = ""
set ppath = ""
while ("$l[1]" != "")
    set pkg   = `echo $pkg $l[1]`
    set ppath = `echo $ppath $l[2]`
    shift l; shift l

    if ("$#l" == "0") then
	break
    endif
end
/bin/rm -f /tmp/_ext$$

echo ""
echo "Number of declared packages:	" $#tasks
echo "Number of logical directories:	" $#ppath
echo ""

#echo "tasks:    "$tasks
#echo "packages: "$pkg
#echo "paths:    "$ppath



echo "Checking packages ..."
echo ""
echo "    1)  The 'Path' check verifies that the extern.pkg path exists and is"
echo "        not located in the iraf root directory (NOAO excepted)."
echo "    2)  The 'Helpdb' check verifies that the package is declared in the"
echo "        helpdb string."
echo "    3)  The 'Binaries' check prints the most recent file date for the"
echo "        installed binaries.  A failed test indicates no binaries for"
echo "        the current architecture."
echo "    4)  The date given is the date of the most recent file in the named"
echo "        bin directory.  Dates before 12/99 indicate binaries which should"
echo "        be recompiled for Y2K compliance (assuming IRAF V2.11.3 or later)"
echo ""
echo ""
echo "     Package	Path	 Helpdb	    Binaries	Date"
echo "     -------	----	 ------	    --------	----"
echo ""

set  CL	 = $iraf/bin.$mach/cl.e

set lp = ""
set lpath = ""
set delpak = ""
foreach p ($pkg)
    set decl = `cat $epkg | grep task | grep $p`
    set pat = $ppath[1]
    if ("$decl" != "") then

	echo $p | awk '{printf ("%12.12s", $1 ) }'

	# See if this is an iraf logical path and resolve it.
	echo $pat | egrep '\$' >& /dev/null
	if ($status == 0) then
	   set cmd = `echo $pat | awk '{printf("=osfn(\"%s\");logout\n", $1)}'`
	   echo "#\!$CL -f"	> /tmp/_cmd$$
	   echo $cmd 		>> /tmp/_cmd$$
	   chmod 755 /tmp/_cmd$$

	   set pat = `/tmp/_cmd$$`
	   /bin/rm -rf /tmp/_cmd$$
	endif

	# Check that the path exists.
	if (-d $pat) then
	    if ("$p" == "noao") then
	        echo -n "	[ OK ]"
                set ok_count = `expr $ok_count + 1`
	    else
	        if (`echo $pat | grep $iraf | grep $iraf/\.\.` == "") then
	            echo -n "	[ OK ]"
                    set ok_count = `expr $ok_count + 1`
	        else
	            echo "	[FAIL] " ; set errstat = 1
	    	    echo "	    *** invalid path: $p = $pat"
	    	    echo "	    *** package should not be in iraf root dir"
  	            set err_count = `expr $err_count + 1`
		    set delpak = `echo $delpak $p `
		    goto pkg_err
	        endif
	    endif
 	else
	    echo "	[FAIL] " ; set errstat = 1
	    echo "	    *** invalid path: $p = $pat"
	    set delpak = `echo $delpak $p `
  	    set err_count = `expr $err_count + 1`
	    goto pkg_err
 	endif


	# See if the package was declared with help.
	echo $helpdb | grep $p >& /dev/null
	if ($status == 0) then
	    echo -n "	 [ OK ]	    "
            set ok_count = `expr $ok_count + 1`
	else
	    echo -n "	 [FAIL]     " ; set errstat = 1
  	    set err_count = `expr $err_count + 1`
	endif

	# See if binaries exist for this platform.
	set dir = $pat/bin.$mach
	if (-d $dir) then
	    if (`$LS -lL $dir | wc -l` > 1) then
	        set d = `$LS -lLtg $pat/bin.$mach/* | head -2 | tail -1`
	        set dat = `echo $d |awk '{printf("%s %s %s",$6,$7,$8)}'`

	        #echo -n "[ OK ]"
		if ("`echo $dat | grep 199 | grep Dec`" == "") then
		    if ("`echo $dat | grep 200`" == "" && "`echo $dat | grep ':'`" == "") then
		        echo -n "[WARN]"
                        set warn_count = `expr $warn_count + 1`
		    else
		        echo -n "[ OK ]"
                        set ok_count = `expr $ok_count + 1`
		    endif
		else
		    echo -n "[ OK ]"
                    set ok_count = `expr $ok_count + 1`
		endif

	        echo $d | awk '{printf ("\t%3s %2s %-5s", $6, $7, $8) }'
	        echo  "   "bin.$mach
	    else
	        echo -n "[FAIL]      " ; set errstat = 1
  	        set err_count = `expr $err_count + 1`
	        echo "<not found>    "bin.$mach
	        #goto pkg_err
	    endif
 	else
	    echo -n "[FAIL]      " ; set errstat = 1
	    echo "<not found>    "bin.$mach
  	    set err_count = `expr $err_count + 1`
 	endif

	# Check for other architectures installed
	if (-e $pat/bin) then
	    foreach dir ($pat/bin.*)
	       set b = $dir:t
	       if ("$b" != "bin.generic" && "$b" != "bin.$mach") then
		    if (`$LS -l $dir | wc -l` > 1) then
	              	set d = `$LS -lLtg $pat/$b/* | head -2| tail -1`
	              	set dat = `echo $d |awk '{printf("%s %s %s",$6,$7,$8)}'`
			echo -n "			"
			if ("`echo $dat | grep 199 | grep Dec`" == "") then
		            if ("`echo $dat | grep 200`" == "" && "`echo $dat | grep ':'`" == "") then
			        echo -n "	    [WARN]	"
                        	set warn_count = `expr $warn_count + 1`
			    else
			        echo -n "	    [ OK ]	"
                        	set ok_count = `expr $ok_count + 1`
			    endif
			else
			    echo -n "	    [ OK ]	"
                            set ok_count = `expr $ok_count + 1`
			endif
	              	echo $d |awk '{printf ("%3s %2s %-5s", $6, $7, $8)}'
	              	echo "   "$b
		    endif
	       endif
	    end
	endif

    else
	set lp = `echo $lp $p`
	set lpath = `echo $lpath $pat`
    endif 

pkg_err:
    echo ""
    shift ppath
end


# Now check logical directories defined in the file.
set dellog = ""
if ("$lp" != "") then
    echo ""
    echo "Checking logical directories ..."
    echo ""
    echo "   Logical Directory	Path"
    echo "   -----------------	----"

    foreach p ($lp)
        set pat = $lpath[1]
	echo $p | awk '{printf ("%20.20s", $1 ) }'
	if (-d $pat) then
	    echo "	[ OK ]"
            set ok_count = `expr $ok_count + 1`
 	else
	    echo "	[ FAILED ]" ; set errstat = 1
	    echo "	    *** invalid path: $p = $pat"
	    set dellog = `echo $delpak $p `
  	    set err_count = `expr $err_count + 1`
 	endif

        shift lpath
    end
endif


# Report unnecessary packages or logical dirs.
if ("$delpak" != "") then 
  echo ""
  echo ""
  echo "    *** The folowing packages were found to be invalid for the"
  echo '    *** current platform and can be removed from the hlib$extern.pkg'
  echo "    *** file:"
  echo "    ***"
  foreach i ($delpak) 
      echo "    ***		$i"
  end
  echo ""
endif

if ("$dellog" != "") then 
  echo ""
  echo ""
  echo "    *** The folowing logical directories were found to be invalid for"
  echo '    *** current platform and can be removed from the hlib$extern.pkg'
  echo "    *** file:"
  echo "    ***"
  foreach i ($dellog) 
      echo "    ***		$i"
  end
  echo "    ***"
endif


exit_early:

  echo ""
  echo ""
  echo "======================================================================"
  echo ""
  echo "SYSINFO completed with:	    Tests Passed:  $ok_count"
  echo "			        Warnings:  $warn_count"
  echo "			          Errors:  $err_count"
  if ($err_count > 0) then
    echo ""
    echo "    Not all errors listed here will be fatal but may indicate a"
    echo "    problem with some aspect of the system, or will  reveal the"
    echo "    likely cause of a problem currently being seen."
    echo ""
    echo "    Users should contact  iraf@noao.edu  if  help is  needed in"
    echo "    correcting any problems, or with  suggestions/comments  for"
    echo "    future versions of this diagnostic script."
  endif
  echo ""
  echo "======================================================================"
  echo ""

sysinfo_cleanup:
  /bin/rm -rf /tmp/_cmd$$
  /bin/rm -f /tmp/_ext$$

exit 0


#  Print usage information.  We will not get here unless the "-help" flag
#  was issued.

Usage:

    echo ""
    echo "Usage:	sysinfo [ -G | -V | -N | -P | -D | -T | -help ]"
    echo ""
    echo "	where:	-G		Print General Info only"
    echo "		-V		Do Verification tests only"
    echo "		-N		Do Networking tests only"
    echo "		-P		Do Extern Package tests only"
    echo "		-D		Do Display tests only"
    echo "		-T		Do Tape Device tests only"
    echo "		-h		Print this message"
    echo ""
