#!/bin/bash
#
#
#      ./get-TS-info [DEV]   
#
#      ./get-TS-info       -Will run on DEV or PROD      
#
#	 
#       Get all the terminal servers from screeniocs file
#
#
#	Cycle through all the terminal servers
#        -Copy over latest copy of ts-info
#        -Check is terminal server is in file
#         --Skip check if in ts-info
#        -If NOT in ts-info
#	   -Check if device is up
#          -if UP
#            -then check if MOXA
#            -else DIGI
#          -else if NOT up
#            -write DOWN
#
#	-Create file of changes
#       -Send email if changes
#
#---------------------------------------------------------------------------
#
DT=`date +%b-%d-%Y-%H:%M:%S`
MOXA_CMD=/nfs/slac/g/lcls/sys/moxa/mcc_tool
if [ -e /afs ]
  then
    LIST=`cat $IOC/screeniocs | grep -v ^# | grep " ts-" | awk '{print $2}' | sort -u`
    MOXA_CMD=/nfs/slac/g/lcls/sys/moxa/mcc_tool
    TSFILEAREA=/nfs/slac/g/lcls/sys/TerminalServer
    TSFILE=$TSFILEAREA/ts-info
    TSFILECHG=$TSFILEAREA/ts-info-chg-DEV-$DT
    OFILE=/tmp/ts-info-chg-DEV
  else
    LIST=`cat $IOC/screeniocs | grep -v ^# | grep " ts-" | awk '{print $2}' | sort -u`
    MOXA_CMD=/usr/local/admin/moxa/mcc_tool
    TSFILEAREA=/usr/local/admin/TerminalServer
    TSFILE=$TSFILEAREA/ts-info
    TSFILECHG=$TSFILEAREA/ts-info-chg-$DT
    OFILE=/tmp/ts-info-chg
    
fi

#Make a copy of ts-info file
cp $TSFILE ${TSFILE}-$DT
rm -f $OFILE

for x  in $LIST
do
	echo "checking $x"
	#---------------------------------------------------------
	#See if TS is already in file and NOT DOWN skip if it is
	#---------------------------------------------------------
	TSCHK=`cat $TSFILE | grep -v DOWN | grep $x >/dev/null;echo $?`
	if [ $TSCHK == 0 ]
	    then
	        echo "$x is in file"
	    else
	      #---------------------------------------------
	      #Perform a ping check -make sure device is up
	      #Wake up the port
	      #---------------------------------------------
	      ping -c1 -w1 $x >/dev/null
	      PCHK=`ping -c1 -w1 $x >/dev/null; echo $?`
	      if [ "$PCHK" == 0 ]
	        then  # Device is up
		  HIP=`host $x | awk '{print $4}'`
		  CHK=`$MOXA_CMD -fw -r -i $HIP  > /dev/null; echo $?`
		  if   [ "$CHK" != 0 ]
		    then
		      printf "\n$x\tDIGI" >> $OFILE
		    else
	              printf "\n$x\tMOXA" >> $OFILE
		  fi
	        elif [ "$PCHK" == 1 ] # Device does not ping
		  then
		    printf "\n$x\tDOWN"    >> $OFILE
		  else #device is not in DNS
		    echo "$x in screeniocs file is not in DNS" | mailx -s "$x not in DNS" "brobeck@slac.stanford.edu"
	      fi	  
	fi
done
#----------------------------------------------------------
# Update any changes to ts-info
# Send mail
#----------------------------------------------------------
if [ -e $OFILE ]
  then
	cat $OFILE >> $TSFILE
	#Remove Duplicates
	cat $TSFILE | sort -u > $TSFILE.junk
	cp $TSFILE.junk $TSFILE
	cat $OFILE | mailx -s "get-TS-info ($HOSTNAME) $DT" "brobeck@slac.stanford.edu"
fi

# clean up files
find $TSFILEAREA/ts-info-* -mtime +1 -exec rm {} \;