#!/bin/csh -f

switch (`uname -s``uname -r`)
case SunOS5*:
    set HOSTPGM = /usr/local/bin/host
	set PING = /usr/sbin/ping
	set PINGcsw
	set PINGWsw
	set RSH = /usr/local/bin/rsh
#	set SSH = /usr/local/bin/ssh
	set SSH = ~/Sys/ssh-no-Banner
	set TIMEOUT = "/usr/local/bin/timeout 15"
	breaksw
case Linux2*:
case Linux3*:
    set HOSTPGM = /usr/bin/host
	set PING = /bin/ping
	set PINGcsw = -c1	# needed to terminate ping
	set PINGWsw = -W	# to specify timeout
	set RSH = /usr/bin/rsh
#	set SSH = /usr/bin/ssh
	set SSH = ~/Sys/ssh-no-Banner
	set TIMEOUT
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

set NPP0 = "-o NumberOfPasswordPrompts=0"

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -q:
		set QUICK
		shift
		breaksw
	case -w:
		set WIDERGREP
		shift
		breaksw
	case -r:
		set RPFIRST	# do RSHPROBLEM first
		shift
		breaksw
	case -swd:
		set SKIPWEBDAV	# Skip webdav.cs
		shift
		breaksw
	case -sd:
		set SKIPDIMACS	# Skip dimax and dimacs
		shift
		breaksw
	case -h:
		set LISTHOSTS	# which user can apparently login on
		shift
		breaksw
	default:
		if (! $?TARGET) then
			set TARGET = $1
		    shift
		    breaksw
		endif
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

set BASE = `basename $0`
set TMP = /tmp/$BASE.$USER.tmp
set TTMP = /tmp/$BASE.$USER.tmp.$$
set SHLOCK = /tmp/$BASE.shlock

set STATUS = 1
while ($STATUS)
    ~/Sys/shlock -f $SHLOCK -p $$
    set STATUS = $status
end

if ($?RSHPROBLEM) then
    ~/rshproblem
endif

sed -e 's;#.*;;' -e 's;[ 	];;g' ~watrous/.check-ops.hosts> $TTMP
/bin/ls ~/.scpdist.tstamp.*|sed 's;.*/.scpdist.tstamp.;;' | egrep -v '~$|\.hld$' >> $TTMP

#foreach HOST ( `sed 's;#.*;;' ~watrous/.check-ops.hosts;/bin/ls .scpdist.tstamp.*|sed 's;.scpdist.tstamp.;;'` )
foreach HOST ( `sort -u $TTMP` )
    if ($HOST == "webdav.cs" && $?SKIPWEBDAV) continue
    if ($HOST == "dimacs" && $?SKIPDIMACS) continue
    if ($HOST == "dimax" && $?SKIPDIMACS) continue
    set SSTATUS = 0 ; set RSTATUS = 0
    if ($?VERBOSE) echo `date +%T` $HOST
    if (! $?QUICK) then
#	/usr/sbin/ping $HOST 2 >& /dev/null
#	$PING $PINGcsw $HOST $PINGWsw 2 >& /dev/null
	set PHOST = `echo $HOST | sed 's;.*@;;'`
# echo	"( $HOST => $PHOST )"
# echo	$HOSTPGM $PHOST \> /dev/null
	$HOSTPGM $PHOST > /dev/null
	set STATUS = $status
	if ($STATUS) then
	    echo $HOSTPGM $PHOST failed "($STATUS)"
	    goto bothfailed
	endif
	$PING $PINGcsw $PHOST $PINGWsw 2 >& /dev/null
	set PSTATUS = $status
	if (! $PSTATUS) then
	    if ($?DEBUG) echo $SSH $NPP0 -n $HOST Sys/ypcat passwd ...
	    $TIMEOUT $SSH $NPP0 -n $HOST Sys/ypcat passwd >& $TMP.$HOST.new
	    set SSTATUS = $status
	    set NOR = $0.norsh
	    if (! -e $NOR) set NOR = check-slide.norsh
	    if (! -e $NOR) set NOR = /dev/null
	    grep "^$HOST"'$' $NOR > /dev/null
	    set NORSTATUS = $status
	    if ($SSTATUS && $NORSTATUS) then
		    if ($?DEBUG) echo $RSH $HOST -n Sys/ypcat passwd ...
		    $TIMEOUT $RSH $HOST -n Sys/ypcat passwd >& $TMP.$HOST.new
		    set RSTATUS = $status
		    if ($RSTATUS) then
			echo ssh/rsh to $HOST failed "($SSTATUS/$RSTATUS)"
			goto bothfailed
		    endif
	    endif
	    if (! $NORSTATUS) set RSTATUS = 1109
	    if (! ($SSTATUS && $RSTATUS)) then
		grep "does not run" /dev/null $TMP.$HOST.new
		if (! $status) goto bothfailed
		if (-e $TMP.$HOST) /bin/mv $TMP.$HOST{,.old}
		/bin/mv $TMP.$HOST{.new,}
	   endif
	    if ($?DEBUG) echo $SSH $NPP0 -n $HOST Sys/ypcat group ...
	    $TIMEOUT $SSH $NPP0 -n $HOST Sys/ypcat group >& $TMP.group.$HOST.new
	    set SSTATUS = $status
	    if ($SSTATUS && $NORSTATUS) then
		    if ($?DEBUG) echo $RSH $HOST -n Sys/ypcat group ...
		    $TIMEOUT $RSH $HOST -n Sys/ypcat group >& $TMP.group.$HOST.new
		    set RSTATUS = $status
		    if ($RSTATUS) then
			echo ssh/rsh to $HOST failed "($SSTATUS/$RSTATUS)"
		    endif
	    else
		if ($SSTATUS) echo ssh to $HOST failed "($SSTATUS)"
	    endif
	    if (! ($SSTATUS && $RSTATUS)) then
		if (-e $TMP.group.$HOST) /bin/mv $TMP.group.$HOST{,.old}
#		/bin/mv $TMP.group.$HOST{.new,}
# sometimes groups switch around from ypcat. print them in order; 1/29/09
		/bin/sort $TMP.group.$HOST.new > $TMP.group.$HOST
		/bin/rm $TMP.group.$HOST.new
	   endif
	else
#	    echo ping of $HOST failed "($PSTATUS)"
	    echo ping of $PHOST failed "($PSTATUS)"
	endif
    endif
bothfailed:
    if (-e $TMP.$HOST) then
	if (! $?WIDERGREP) then
	    grep "^${TARGET}:" $TMP.$HOST > $TTMP
	else
	    grep "${TARGET}" $TMP.$HOST > $TTMP
	endif
	if (! -z $TTMP) then
	    if (! $?LISTHOSTS) then
		cat $TTMP | sed "s;.;${HOST}: &;"
		if (-e $TMP.group.$HOST && ! $?WIDERGREP) echo "   " `grep -w $TARGET $TMP.group.$HOST | sed 's;:.*;;'`
	    else
		set USHELL = `sed 's;.*:;;' $TTMP`
		switch ("$USHELL")
		case /bin/bash:
		case /bin/csh:
		case /bin/ksh:
		case /bin/ocsh:
		case /bin/sh:
		case /bin/tcsh:
		case /bin/zsh:
		case /sbin/sh:
		case /usr/bin/tcsh:
				if ($?DEBUG) echo Good shell: $TARGET $HOST
				goto goodshell
				breaksw
		case /bin/echo:
		case /bin/kerbshell/tcsh:
		case /bin/false:
		case /sbin/nologin:
		case /usr/local/bin/kerbshell/tcsh:
				if ($?DEBUG) echo Bad shell: $TARGET $HOST "($USHELL)"
				goto badshell
				breaksw
		default:
				if ($?DEBUG) echo Unknown shell: $TARGET $HOST "($USHELL)"
				goto badshell
				breaksw
		endsw
goodshell:
		set GROUPS = `grep -w $TARGET $TMP.group.$HOST | sed 's;:.*;;'`
		if ("$GROUPS" != "") then
		    if (! $?HOSTS) set HOSTS
		    set HOSTS = ( $HOSTS $HOST )
		endif
badshell:
	    endif
	endif
    endif
end

if ($?HOSTS) then		# HOSTS only defined if $?LISTHOSTS
    echo ${TARGET}: $HOSTS
endif

/bin/rm $TTMP $SHLOCK
