#!/bin/csh
# ~jhs/bin/.csh/consolev == ~jhs/bin/.csh/xloadv,
#	called by ~jhs/.xsession
# See also  ~jhs/bin/.csh/xconsolev called by xconsoles
# 2 optional parameters:	1 screen name
#				2 screen size in pixels
# function dependent on name.
# Starts an xterm with console logging on your local display.
	# Alternate that does not allow input:
	#	xconsole -daemon -notify -verbose -fn fixed -exitOnFail

set CONS = "consolev"
# I used to call it console, but something in ports/ now creates console.

set HNAME = `hostname`
set HNAMECOLOUR = `hostname -s`
#	switch ($HNAME) # { over ride defaults
#		case land.berklix.org:
#			# setenv DISPLAY test88.bsn.com:0
#			breaksw
#		case slim.berklix.org:
#			breaksw
#		case lapa.js.berklix.net:
#		case lapd.js.berklix.net:
#		case laph.js.berklix.net:
#		case lapl-p.js.berklix.net:
#		case lapl.js.berklix.net:
#		case lapn.js.berklix.net:
#		case lapo.js.berklix.net:
#		case lapr.js.berklix.net:
#		case laps.js.berklix.net:
#		case laps.no.berklix.net:
#			# Laptops with own screens, perhaps closed lids.
#			# setenv DISPLAY fire:0
#			breaksw
#		default:
#			# Do not override default DISPLAY
#			breaksw
#		endsw	# }

set DISP = `printenv DISPLAY`
# DISP is a trick to force the following "if" to work, without it, if
# DISPLAY is unset, the script aborts

# set XX =`xdpyinfo|grep "name of display"|sed -e "s/name of display: //"`
# Can fail with:
#	xdpyinfo: unable to open display ""
#	set: Variable name must begin with a letter.

if ( "$#" == "0" ) then
	# echo "No Parameter being Display name" > /tmp/consolev.debug
else
	set DISP = $1
endif

if ( "x$DISP" == "x" ) then
	echo "DISPLAY environment variable not set"
	echo -n "Assuming your DISPLAY environment variable is "
	switch ($HNAME)
		case blak.js.berklix.net:
		case dual.js.berklix.net:
		case film.js.berklix.net:
		case fire.js.berklix.net:
		case flip.js.berklix.net:
		case john.js.berklix.net:
		case king.js.berklix.net:
		case lapa.js.berklix.net:
		case lapd.js.berklix.net:
		case laph.js.berklix.net:
		case lapl-p.js.berklix.net:
		case lapl.js.berklix.net:
		case lapn.js.berklix.net:
		case laps.js.berklix.net:
		case lapo.js.berklix.net:
		case lapr.js.berklix.net:
		case loft.js.berklix.net:
		case mart.js.berklix.net:
		case mini.js.berklix.net:
		case park.js.berklix.net:
		case rain.js.berklix.net:
		case scan.js.berklix.net:
		case slim.js.berklix.net:
		case snow.js.berklix.net:
		case sony.js.berklix.net:
		case test.js.berklix.net:
		case wind.js.berklix.net:
		case wood.js.berklix.net:
			setenv DISPLAY disp.js.berklix.net:0
			# setenv DISPLAY laps.js.berklix.net:0
			breaksw
		case laps.no.berklix.net:
			setenv DISPLAY laps.no.berklix.net:0
			breaksw
		case lapd.bsn.com
			setenv DISPLAY :0
			breaksw
		default:
			setenv DISPLAY unknown
			breaksw
		endsw
	echo $DISPLAY
else
	setenv DISPLAY $DISP
endif

# echo "Starting on display $DISPLAY"

# X Windows screen initialiser -g WIDTHxHEIGHT+-XOFF+-YOFF
#	+/- XOFF left/right side of display to left/right side of window
#	+/- YOFF top/bottom of display to top/bottom of screen

if ( "`basename $0`" == "$CONS" ) then		# { xconsole
	set BASE = 80
	if ( "$#" == "2" ) then		
		# {{
		# echo "Screen Geometry specified by caller." 
		switch ( $2 )	
			# {
			# echo "Resolution: $2" # to know which HEIGHT to tune.
			case 1600x1200:		# { fire|laps
				set HEIGHT = 41	# Keep same as ~/bin/.csh/xtermv
				breaksw		# }
			1280x1024:		# { fire-iiyama
				set HEIGHT = 37	# Keep same as ~/bin/.csh/xtermv
				breaksw         # }
			1024x768:		# { lapd|lapa
				set HEIGHT = 36	# Keep this same as .xsession
				breaksw		# }
			640x480:		# { lapl|lapl-p
				set HEIGHT = 23	# Keep this same as .xsession
				breaksw		# }
			default:		# {
				set HEIGHT = 36	# Keep this same as .xsession
				breaksw		# }
			# }
		endsw	
	else	# }{ Screen Geometry unknown.
		set HEIGHT = 40
	endif				# }}
else					# }{ xloadv
	set BASE = 0
	set HEIGHT = 140
endif					# }
# echo "HEIGHT is $HEIGHT" # Debug

set COLOUR=`host2colour $HNAMECOLOUR`

set xs = xs
set console = Console
# For some reason, host=fire (5.3) & host=laps (5.3) & host=dual (5.3)
# but not host=lapd (5.1) the xterm
# flashes on screen & disappears again immediately, not sure why,
# maybe cos no custom kernel with XSERVER yet ?  maybe /dev/fs maybe
# failing to open or unlock console I guess, For now cludge it so the
# xterm stays, with
#	set xs = ; set console = NonRootConsole
# host=fire does collect root errors even though not run with xs

# Error message		"xterm: cannot open console: Device busy"
# comes from
#	host=lapl uname-r=4.10
#	host=lapa uname-r=4.9
#  as /etc/ttys has
#	"ttyv8 "/usr/local/bin/xdm -nodaemon" xterm on secure"
# thus local host lcd screen already has a console error log open.
# however, removing the xdm line does not solve the puzzle of why
#	"set xs = ; set console = NonRootConsole"
# is needed for certain hosts.

# Determine offset.
set MULTIPLE = "`host2number`"

switch ($HNAME) # { internal name manipulated by /etc/csh.cshrc
	case dual.js.berklix.net:
	case film.js.berklix.net:
	case fire.js.berklix.net:
	case laph.js.berklix.net:
	case laps.js.berklix.net:
	case lapr.js.berklix.net:
	case laps.no.berklix.net:
	case mini.js.berklix.net:
	case scan.js.berklix.net:
	case wind.js.berklix.net:
		set xs = ; set console = NonRootConsole
		breaksw
	default:
		breaksw
endsw	#}

# Stop it going above top of 1024 x 768 laptop screen
# next bit of code not finished.
# echo -n "MULTIPLE is "
# echo "$BASE $MULTIPLE p"|dc
# if ( `echo "$BASE $MULTIPLE p"|dc` > 630 ) then
#	set MULTIPLE = 630
#	endif

if ( "`basename $0`" == "$CONS" ) then	# {{
	set xxx = `echo "$MULTIPLE $HEIGHT * $BASE + p"|dc`
	# To start at bottom, leaving $BASE for an fvwm panning window,
	#	& $HEIGHT for a telno xterm:
	# -geometry 60x3-0-`echo "$MULTIPLE $HEIGHT * $BASE + $BASE + p"|dc`
	# To start at top
	# -geometry 60x3-0+`echo "$MULTIPLE $HEIGHT * p"|dc` &
	$xs xterm			\
		-sb						\
		-sl 1024					\
		-fg magenta					\
		-bg $COLOUR					\
		-C						\
		-j						\
		-T "`hostname -s` $console Xterm"		\
		-geometry 80x2-0+`echo "$MULTIPLE $HEIGHT * p"|dc` &
		# -n consolev
	else					# }{ xloadv
		xload -bg $COLOUR -g \
			+`echo "$BASE $MULTIPLE $HEIGHT * + p"|dc`-0 &
	endif					# }}

# X Windows screen initialiser -g WIDTHxhEIGHT+-XOFF+-YOFF
#	+/- XOFF left/right side of display to left/right side of window
#	+/- YOFF top/bottom of display to top/bottom of screen
