#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/xtermv
# ~/public_html/bin/.sh/xconsolev	-> xtermv
# ~/public_html/bin/.sh/xloadv		-> xtermv
# ~/public_html/bin/.sh/xnetstatv	-> xtermv
# ~/public_html/bin/.sh/xpingv		-> xtermv
# ~/public_html/bin/.sh/xpingvall	-> xtermv
# ~/public_html/bin/.sh/xpingvcoax	-> xtermv

# ~/bin/.sh/xconsolev	-> ~/public_html/bin/.sh/xconsolev, called by xconsoles
# ~/bin/.sh/xloadv	-> ~/public_html/bin/.sh/xloadv
# ~/bin/.sh/xnetstatv	-> ~/public_html/bin/.sh/xnetstatv, called by interfaces
# ~/bin/.sh/xpingv	-> ~/public_html/bin/.sh/xpingv, called by xpings
# ~/bin/.sh/xpingvall	-> ~/public_html/bin/.sh/xpingvall
# ~/bin/.sh/xtermv	-> ~/public_html/bin/.sh/xtermv, called by xterms
# ~/bin/.sh/xpingvcoax	-> ~/public_html/bin/.sh/xpingvcoax,

# See also ~/bin/.csh/consolev called by .xsession

nameu="`echo $1|dd conv=ucase`"
namel="`echo $1|dd conv=lcase`"

exec=xterm
flag_exec_in_xterm="-e"
flag_bg="-bg"
flag_n="-n"
flag_title="-T"
flag_jump="-j"
flag_geometry="-geometry"

# color=`host2colour $namel`
# When ~/.xsession calls xterms, the background wrongly opened in white,
# as /site/usr/local/bin/ not in search path
color=`/site/usr/local/bin/host2colour $namel`

# host2number=host2number
host2number=/site/usr/local/bin/host2number
# When ~/.xsession calls xterms, All the xterms except for Mart did
# not arrive on display, & that for Mart was misplaced, Later a manual
# invocation of xterms does place them all OK, as /site/usr/local/bin/
# not in search path of this .xsession

eighty="80x"
zero="0+"	# left margin for default host screens with just 1 device.
less="<"

screen=`xrandr | grep VGA-0 | awk '{printf "%s\n",$3}'`
#	VGA-0 connected 480x640+0+320 left (normal left inverted right x axis y axis) 0mm x 0mm
case $screen in		# {
	480x640+0+320)	#({
		zero="480+"	# Indent right on to main output DVI-0
		;;	# }
	*)	#({
		;;	# }
esac			# }

case `basename $0` in	#{
	"xconsolev")	#{
		bigC="-C"
		fore="-fg magenta"
		from=xconsoles
		geoh=2
		leftright="-"
		xs=xs
		title=rlogin
		command="rlogin -D"
		number=`${host2number} $namel`
		sidebar="-sb -sl 1024"
		last=$namel
		;;	#}
	"xtermv")	#{
		bigC=""
		fore=""
		# from=xterms
		from="/home/jhs/bin/.sh/ xterms xtermv"
		geoh=2
		leftright="+"
		xs=""
		title=rlogin
		command="rlogin -D"
		number=`${host2number} $namel`
		sidebar="-sb -sl 1024"
		last=$namel
		;;	#}
	"xpingvall")	#{
		bigC=""
		fore=""
		from=xpings
		geoh=2
		leftright="+"
		xs=""
		title=ping
		command="ping"
		number=`${host2number} $namel`
		sidebar="-sb -sl 1024"
		last=$namel
		;;	#}
	"xpingvcoax")	#{
		bigC=""
		fore=""
		from=xpings
		geoh=2
		leftright="+"
		xs=""
		title=ping
		command="ping"
		number=`host2coax $namel`
		sidebar="-sb -sl 1024"
		last=$namel
		;;	#}
	"xnetstatv")	#{
		bigC=""
		fore=""
		from=`basename $0`
		geoh=24
		leftright="+"
		xs=""
		title="netstat -w 8"
		sidebar=""
		last="`host2interface $namel`"
		# reduce name {mart|park|gate}.{int|ext|tun} to mart|park
		namel="`basename $namel .int`"
		namel="`basename $namel .ext`"
		namel="`basename $namel .tun`"
		number=`${host2number} $namel`
		command="rsh $namel netstat -w 8 -I "
		# -8 : netstat displays in bytes per time interval, so:
		#	If -w 10 shows eg 300K bytes,
		#		that is {300,000 / 10 x 8} bits / second
		#	If -w 8 shows eg 300K bytes,
		#		that is {300,000 / 8 x 8} bits / second
		# Of course the link wiill actually be carrying more,
		#		with protocol overhead.
		;;	#}
	"xloadv")	#{
		bigC=
		command=
		eighty=
		exec=xload
		flag_title=
		flag_exec_in_xterm=
		flag_geometry=
		flag_jump=
		flag_n=
		fore=
		from=
		geoh=
		last=
		leftright=
		less=
		nameu=
		number=`${host2number} $namel`
		#namel=
		sidebar=
		title=
		xs=
		zero=
	#	-display dpy		X server on which to display
	#	-geometry geom		size and location of window
	#	-fn font		font to use in label
	#	-scale number		minimum number of scale lines
	#	-update seconds		interval between updates
	#	-label string		annotation text
	#	-bg color		background color
	#	-fg color		graph color
	#	-hl color		scale and text color
	#	-nolabel		removes the label from above the chart.
	#	-jumpscroll value	number of pixels to scroll on overflow
	#	-remote host		remote host to monitor
		;;	#}
	*)	#{
		echo "$0 Not a recognised name"
		;;	#}
	esac	# }

case `basename $0` in	#{
	"xnetstatv") #{
		# Assume only a few hosts are on, & user will be happy
		# if necessary to manualy place with mouse a few 80x24 xterms
		# displaying net traffic (eg to find which one is soaking
		# gate bandwidth.
		# The ping is slow when looping on many hosts, so
		# we only ping if we cant avoid it.
		pixels=""
		ping -q -c 1 -t 1 $namel
		;;	#}
	"xloadv")	#{
		pixels=""
		;;	#}
	*)	#{
		# Assume lots of slim xterms to place, set geometry to
		# automatically allocate placement, to save a lot of
		# mouse work. The clutter of xterms to hosts that are down
		# will disappear off screen after half a minute or so.
		# height=41 # Keep same as ~/bin/.csh/consolev
		height=37 # Keep same as ~/bin/.csh/consolev
		# Only if we want to squeeze on shorter left VGA-0 :
		#	 case $screen in		# {
		#		480x640+0+320)	#({
		#			height="27"
		#			;;	# }
		#		*)	#({
		#			;;	# }
		#	 esac			# }
		pixels=`echo "${number} ${height} * p" | dc`
		true
		;;	#}
	esac	# }

if [ "X${flag_title}" = "X" ]; then # {
	string_title=
else	# }{
	string_title="$nameu $title $less $from"
fi	# }
if [ "X${command}" = "X" ]; then	# {
	string_command=
else	# }{
	string_command="$command $last"
fi	# }

case $? in
	0)	#{
	# echo "Host Up xx $? xx $namel xx"
		if [ "X`basename $0`" = "Xxloadv" ]; then	# {
if 0; then	# {{ CLUNKY BARS
			ping -q -c 1 -t 1 $namel && \
			 xload -remote $namel -label $namel -bg $color &
			# ping first because otherwise loads of xloads get
			# started even though the host is turned off.
			# Bug: doesnt inherit colour from .Xdefaults
			# Bug fails on host=mart & jail
			#	Maybe an ipfw problem ?
			# Bug very low precision & unresponsive & square shapes
			# compared to manual xload
			# -remote needs rwho running on both remote & local
			# man rwhod: "Status messages are generated 
			#	approximately once every 3 minutes" 
			# So that's what produces those horrible wide
			# vertical column block bar graphs.
else	# }{ NEW 2022-12-12 Fine resolution
			# Need to add a ssh $namel -e before xload
			ping -q -c 1 -t 1 $namel && \
			ssh -g -X -Y jhs@$namel xload \
			${flag_bg} ${color} \
			-label $namel \
			&
fi	# }}
		else	# }{
		# xs is needed for Console, but not for eg xload
		$xs $exec $fore $bigC \
			${flag_geometry} ${eighty}${geoh}${leftright}$zero$pixels \
			${sidebar} \
			${flag_bg} ${color} \
			${flag_jump} \
			${flag_n} $namel \
			${flag_title} "${string_title}" \
			${flag_exec_in_xterm} "${string_command}" \
			&
			# The 2 lines above with "" still break on xloadv
			# but ok with all the rest.
			# 80x2+0+50	left column
			# 80x2-0+50	right column same place as consoles
		fi	# }
	;;	#}
	*)	#{
	# echo "Host Down xx $? xx $namel xx"
	;;	#}
	esac
