#!/bin/sh -
# http://www.berklix.com/~jhs/dots/.xsession	by Julian Stacey
# /home/jhs/public_html/dots/.xsession
# .xsession@ -> .DOTS/.xsession

# Xsession
# This is the program that is run as the client
# for the display manager.

# A syntax error in here (such as a missing ;; after a case )
# is sufficient to kill the X session.

# Can not leave ~/.xsession with just comments else it logs out immediately,
# so ~/.xsession on laptops will fail unless/ until gbde.sh is run.
# xsm will then instead access /site/usr/local/etc/X11/xsm/system.xsm
# man xsm also refers to ~/.xsmstartup.
# xsm is called from xdm

# Keep colours the same in all of
#	/site/etc/system.fvwmrc
#	~jhs/.Xdefaults.`hostname -s`
#	~jhs/.xsession
#	~jhs/public_html/bin/.csh/consolev
#	~root/.Xdefaults

## touch		/home/jhs/tmp/.hello.from.xsession
## date >>		/home/jhs/tmp/.hello.from.xsession
## echo pid=$$ >>	/home/jhs/tmp/.hello.from.xsession
## echo dollar zero is $0 >>	/home/jhs/tmp/.hello.from.xsession
# cat $0 >>	/home/jhs/tmp/.hello.from.xsession

date > ~/.xsession.log

# Revert from FreeBSD-8.0 background black to xorg stiple
xsetroot -bg default
# On 12.1-STABLE the above does not deliver stipled.

# For debug:
#	echo $DISPLAY > ~/tmp/display
# Gives:
#	:0
# I do not need:
#	DISPLAY=:0.0;export DISPLAY

# xmodmap_dir="/usr/local/share/xmodmap"
# ~/.xmodmap -> ~/public_html/dots/.xmodmap/english
# xmodmap_dir="~jhs/.xmodmap/"
xmodmap_dir="/home/jhs/.xmodmap/"

# english=${xmodmap_dir}/english
# german=${xmodmap_dir}/german
if [ -r	~jhs/public_html/dots/.xmodmap/host_`hostname -s`.init ]; then
	wc -l ~jhs/public_html/dots/.xmodmap/host_`hostname -s`.init >> \
		~/.xsession.log
	xmodmap ~jhs/public_html/dots/.xmodmap/host_`hostname -s`.init
fi

english=${xmodmap_dir}/english
german=${xmodmap_dir}/german

# Import preferences.
if [ -r			~/.Xresources ]; then
	wc -l ~/.Xresources >> ~/.xsession.log
	xrdb -merge	~/.Xresources
fi
if [ -r			~/.Xdefaults ]; then
	wc -l ~/.Xdefaults >> ~/.xsession.log
	xrdb -merge	~/.Xdefaults
fi
if [ -r			~/.Xdefaults.`hostname -s` ]; then
	wc -l ~/.Xdefaults.`hostname -s` >> ~/.xsession.log
	xrdb -merge	~/.Xdefaults.`hostname -s`
fi
# if [ -r			~/.Xdefaults.`printenv DISPLAY` ]; then
#	# Arrange colours/monochrome
#	wc -l ~/.Xdefaults.`printenv DISPLAY` >> ~/.xsession.log
#	xrdb -merge	~/.Xdefaults.`printenv DISPLAY`
# fi

# Xrandr is done before fvwm starts, so fvwm works better.
#	xrandr --auto	# Incomplete Reset
#	notes in ~/tech/xrandr/

if true ; then
	case `hostname -s` in	# {
	fire)	# {
		# Wide display of left VGA vertical adjacent to DVI horixontal.
		# DVI 32 short.
		# xrandr --output VGA-0 --mode 800x600 --pos 0x0 --rotate left \
		#	--output S-video --off \
		#	--output DVI-0 --mode 1024x768 \
		#	--pos 600x32 --rotate normal

		# There is a bug on xrandr, so If above fails on VGA, try
		#	xrandr --output VGA-0 --mode 800x600 --rotate right
		#	xrandr --output VGA-0 --mode 800x600 --rotate left
		# If that too fails, you need to kill the process ps shows as:
		#  /usr/local/bin/X :0 -auth
		#  /var/lib/xdm/authdir/authfiles/A:0-CkjzE8 (Xorg)

		# xmms & # No flags available: -g -geometry --geometry
		# xmms patched out as top reports:
		#  THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
		#   3  20    0   120M  9028K select   0:07  0.00% xmms

		# ---------------
		# Keep paths below using /home/jhs/av/audio/ same as in
		#	~/xtra/Makefile & ~/.fvwm/config .
		# Next 3 commented out 2015-11-07 as no point having
		# the cluuter until I sort out new bbc podcast paths.
		# xterm -g 80x24+0-310 -title "/home/jhs/av/audio/bbc_listen" \
		#	 -e vi /home/jhs/av/audio/bbc_listen &
		# xterm -g 80x6+0-200 -title "/home/jhs/av/audio/bbc_heard" \
		#	 -e vi /home/jhs/av/audio/bbc_heard &
		# xterm -g 80x6+0-100 -title "/home/jhs/av/audio/bbc_old_news" \
		#	 -e vi /home/jhs/av/audio/bbc_old_news &
		# ---------------
		# (cd ~jhs/.screenlayout; xterm -g 60x5+286 -title \
		#	 "~/.xsession ~/.screenlayout/" &)
		# (cd ~jhs/.screenlayout; arandr &)
		# Having an arandr is useful for if the screen starts rotated.
		# however, I no longer actually call arandr, as it calls
		# python with a massive memory usage:
		#	UID___PID__PPID_CPU_PRI_NI____VSZ___RSS_MWCHAN___STAT_TT_________TIME_COMMAND
		#	200_51979_____1___0__20__0_221536_28232_select___I____??______0:00.31_/usr/local/bin/python2.7_/usr/local/bin/arandr
		# ---------------
		;;	# }
	*)
		# For other hosts especially tv on host=lapo
		# see ~/.screenlayout & /site/etc/X11/notes
		;;	# }
	esac		# }
fi

# Determine screen size.
# Done dynamically so if EG a laptop is reconfigured
# to come up after login in reduced pixel dimensions, eg to drive a projector,
# then this dynamic case statement will still work, whereas anything based
# on static knowledge of host physical maximal sizes could fail.
# Also dynamic gives future proofing against future hardware.

screen=`xdpyinfo | grep dimensions | awk '{printf "%s\n",$2}'`
case $screen in # {
	1760x960) # host=fire, iiyama main horzontal, samsung vertical to left
		# xrandr 2014-04-02 shows:
		#	Screen 0: minimum 320 x 200, current 1760 x 960,
		#		maximum 1792 x 1792
		#	VGA-0 connected 480x640+0+320 left (normal
		#		left inverted right x axis y axis) 0mm x 0mm
		#	DVI-0 connected 1280x960+480+0 (normal
		#		left inverted right x axis y axis) 598mm
		HEIGHT="30"
		bifg="50x50+927-0"	# Dummy set JJLATER
		dsl="20x3+320-0"
		;;
	1680x1050) # host=fire, iiyama main horzontal, samsung vertical to left
		# Added 2015-03-24
		HEIGHT="30"		# Dummy set JJLATER
		bifg="50x50+927-0"	# Dummy set JJLATER
		dsl="20x3+320-0"	# Dummy set JJLATER
		;;
	1600x1200)		#( { old fire|laps	# UXGA
		HEIGHT="43"	# keep this same as in
				#	~jhs/bin/.csh/consolev &
				#	~jhs/bin/.sh/xterms
		bifg="50x50+927-0"
		dsl="20x3+122-0"
		;;	# }
	1280x960)		#( { fire dvi-0 f dual head
		HEIGHT="43"		# Dummy set JJLATER
		bifg="50x50+927-0"	# Dummy set JJLATER
		dsl="20x3+122-0"	# Dummy set JJLATER
		;;	# }
	1624x800)		#( { fire svga vertical + dvi horizontal
		HEIGHT="43"		# Dummy set JJLATER
		bifg="50x50+927-0"	# Dummy set JJLATER
		dsl="20x3+122-0"	# Dummy set JJLATER
		;;	# }
	1152x864)		#( { fire dvi-0 f dual head
		HEIGHT="43"		# Dummy set JJLATER
		bifg="50x50+927-0"	# Dummy set JJLATER
		dsl="20x3+122-0"	# Dummy set JJLATER
		;;	# }
	1024x768)		#( { lapd|lapa
		HEIGHT="36"
		bifg="40x40+570-0"
		dsl="20x3+122-0"
		;;	# }
	640x480)		#( { lapl|lapl-p
		HEIGHT="23"
		bifg="30x30+570-0"
		dsl="20x3+122-0"
		;;	# }
	*) #( {
		HEIGHT="36"
		bifg="50x50+570-0"
		dsl="20x3+122-0"
		;;	# }
esac	# }

# Export X Authorisation
if [ -r	~/bin/.sh/xauth.sh ]; then	#{
	wc -l ~/bin/.sh/xauth.sh >> ~/.xsession.log
	~/bin/.sh/xauth.sh &
#	The "&" after xauth.sh is not strictly necessary, as for each
#	host, xauth.sh launches a seperate asynchronous process & then
#	exits, but I have so many hosts it takes too long waiting before
#	fvwm starts.
fi	#}

# Wait for xauth to propogate to gate before starting phone number display.
( sleep 7 ; /home/jhs/bin/.sh/telno_x ) &

JSDOM="js.berklix.net"
host2colour="/site/usr/local/bin/host2colour"

case `hostname` in	# {
	fire.$JSDOM|lapa.$JSDOM|lapd.$JSDOM|lapl-p.$JSDOM|\
	lapl.$JSDOM|lapn.$JSDOM|laps.$JSDOM)	# ( {
		xterm -geometry $dsl -e ~/bin/.csh/dsl &
		# xterm -T From.xsession_debug_trace -bg blue &
		# date > /home/jhs/tmp/.xsession_debug_trace.tmp
		( sleep 5 ; /home/jhs/bin/.sh/xterms ) &
		# sleep: Allow time for xauth.sh
		;;	# }
	blak.no.berklix.net|blak.$JSDOM) #( {
		xterm -sb -sl 1024 -n blak -title "Blak rlogin" \
		-g 80x24 -bg `${host2colour} blak` &
		;;      # }
	dual.$JSDOM|erik.$JSDOM|film.$JSDOM|john.$JSDOM|\
	king.$JSDOM|loft.$JSDOM|mini.$JSDOM|mini.$JSDOM|park.$JSDOM|\
	rain.$JSDOM|test.$JSDOM|wind.$JSDOM) #( {
		# Headless or switched monitor.
		xterm -title \
		 "For a Headless or switched monitor from .xsession" &
		;;	# }
	lapd.no.berklix.net|lapd.$JSDOM) #( {
		xterm -sb -sl 1024 -n lapd -title "LapD rlogin" \
			-g 80x24 -bg `${host2colour} lapd` -fn 9x15bold &
		;;	# }
	lapl.no.berklix.net|lapl.$JSDOM) #( {
		xterm -sb -sl 1024 -n lapl -title "LapL rlogin" \
			-g 80x24 -bg `${host2colour} lapl` -fn 9x15bold &
		# large font to read in dark restaurants.
		;;	# }
	lapr.no.berklix.net|lapr.$JSDOM) #( {
		xterm -sb -sl 1024 -n lapr -title "LapR rlogin" \
			-g 80x24 -bg `${host2colour} lapr` &
		;;	# }
	dell.no.berklix.net|dell.$JSDOM) #( {
		xterm -sb -sl 1024 -n lapr -title "Dell rlogin" \
			-g 80x24 -bg `${host2colour} dell` &
		;;	# }
	laps.no.berklix.net|laps.$JSDOM) #( {
		xterm -sb -sl 1024 -n local -title "Local" \
			-g 80x24+0+`echo "0 $HEIGHT * p" | dc` \
			-bg `${host2colour} localhost` -e rlogin -D localhost &
		xterm -sb -sl 1024 -n laps -title "LapS rlogin" \
			-g 80x24+0+`echo "2 $HEIGHT * p" | dc` \
			-bg `${host2colour} laps`		-e rlogin -D laps &
#		xterm -sb -sl 1024 -n lapd -title "Lapd rlogin" \
#			-g 80x24+0+`echo "1 $HEIGHT * p" | dc` \
#			-bg `${host2colour} lapd`		-e rlogin -D lapd &
#		xterm -sb -sl 1024 -n lapt -title "LapT rlogin" \
#			-g 80x24+0+`echo "3 $HEIGHT * p" | dc` \
#			-bg `${host2colour} lapt`		-e rlogin -D lapt &
		;;	# }
	slim.berklix.org|land.berklix.org) #( {
		xterm -title \
		 "Remote host from .xsession" -g 80x3-0 -fg black -bg white &
		;;	# }
	*) #( {
		xterm -title \
		 "Unknown host `hostname` from .xsession" -g 80x3-0 -fg black -bg white &
		;;	# }
esac		# }

case `hostname` in	# {
	fire.$JSDOM) # {	Only 1 host to prevent multiple beeps.
		xbiff -g $bifg -file mail/.xbiff -volume 0 &
		# no (new) mail: horizontal lever, flag in middle, background
		#	black (unless overridden by .Xdefaults.`hostname -s`
		# mail pending: vertical lever, flag at top, background:
		#	white (unless overridden by .Xdefaults.`hostname -s`
		# xbiff old stuff from Michael Elbel @ PCS
		#	MBOX=$(nice nslookup -query=mb ${USER}.aliases \
		#		| grep mail)
		#	nice xrsh ${MBOX#*=} xbiff & ; unset MBOX
		# The -volume 0 makes no difference,
		# neither does "mixer 0"
		;;	# }
	*) #( {
		;;	# }
esac		# }

# Map laptops to differing physical keyboards.
case `hostname -s` in	#{
	lapa)	#({	kbd
		# xmodmap ~/public_html/dots/.xmodmap/host_`hostname -s`.init
		# Internal: Black Keys, White letters, English.
		# External: White Keys, Black letters, American.
		# xmodmap $german
		;;	# }
	lapl|lapl-p)	#({
		# English on internal keyboard.
		xmodmap $english
		;;	# }
	lapn)	#({
		# xmodmap /usr/local/lib/X11/etc/xmodmap.std
		# Internal: Black Keys, White letters, German
		xmodmap $german
		;;	# }
	lapo)	#({
		xmodmap $english
		;;	# }
	lapr)	#({
		# English on internal keyboard,
		# American or German or English on external.
		# xmodmap /usr/local/lib/X11/etc/xmodmap.std
		if [ -r	~/public_html/dots/.xmodmap/host_`hostname -s`.init ];\
			then
			xmodmap \
			 ~/public_html/dots/.xmodmap/host_`hostname -s`.init
		fi
		# Above to convert back to American in case last was German.

		# Uncomment next line if using internal English keyboard
		# Comment it out if using external American keyboard.
		# xmodmap $english

		# See also /usr/ports/x11/xmodmap
		# See also /usr/ports/x11/xkeycaps
		;;	# }
	dell)	#({
		# English on internal keyboard,
		# American or German or English on external.
		# xmodmap /usr/local/lib/X11/etc/xmodmap.std
		if [ -r	~/public_html/dots/.xmodmap/host_`hostname -s`.init ];\
			then
			xmodmap \
			 ~/public_html/dots/.xmodmap/host_`hostname -s`.init
		fi
		# Above to convert back to American in case last was German.

		# Uncomment next line if using an internal English keyboard
		# (however the internal is probably american not english)
		# Comment it out if using external American keyboard.
		# xmodmap $english

		# See also /usr/ports/x11/xmodmap
		# See also /usr/ports/x11/xkeycaps
		;;	# }
	laps)	#({
		# English on internal keyboard,
		# American or German or English on external.
		# xmodmap /usr/local/lib/X11/etc/xmodmap.std
		if [ -r	~/public_html/dots/.xmodmap/host_`hostname -s`.init ];\
			 then
			xmodmap \
			 ~/public_html/dots/.xmodmap/host_`hostname -s`.init
		fi
		# Above to convert back to American in case last was German.
		# xmodmap $english
		# see also xkeycaps
		;;	# }
	*)	#({
		;;	#}
esac		# }

# For Germans: setenv LANG de_DE.ISO8859-1

# Start some root consoles to monitor hosts.
hfull=`hostname`
hshort=`hostname -s`
domain=`echo $hfull | sed s/$hshort.//`
if [ "$domain" = "bsn.com" ]; then	# {
elif [ "$domain" = "berklix.org" ]; then	# }{
	hosts="slim.berklix.org land"
elif [ "$domain" = "$JSDOM" ]; then	# }{
	hosts="`/site/usr/local/bin/hosts`"
elif [ "$domain" = "no.berklix.net" ]; then	# }{
	hosts="localhost"
elif [ "$domain" = "xx.berklix.net" ]; then	# }{
	hosts="localhost"
elif [ "$domain" = "$JSDOM" ]; then	# }{
	hosts="localhost"
else	# * }{
	echo "No domain recognised by $0"
	hosts=""
fi	#}

# Start a window manager. Do it before changing DISPLAY
#	If one is test running this .xsession after a session has already
#	started, then this will produce an error:
#		[fvwm][SetupICCCM2]: <<ERROR>> another ICCCM 2.0 \
#		compliant WM is running, try -replace
#	maybe JJLATER I might add a test if one is aleady running.

if [ -x /usr/local/bin/fvwm2		]; then
	/usr/local/bin/fvwm2
elif [ -x /usr/local/bin/fvwm		]; then
	/usr/local/bin/fvwm
elif [ -x /usr/local/bin/flwm		]; then
	/usr/local/bin/flwm
elif [ -x /usr/local/bin/twm		]; then
	/usr/local/bin/twm
elif [ -x /usr/local/bin/metacity	]; then
	/usr/local/bin/metacity	# for gnome
else
	twm	# Hope there's one somewhere in search path.
fi

case `hostname -s` in	#{
	film|fire|laps|dual|john|king|loft|mini|park|rain|test|wind) #({
		DISPLAY=$hfull:0
		export DISPLAY
		;;	#}
	lapa|lapd) #({
		DISPLAY=$hfull:0
		export DISPLAY
				#lapd.$JSDOM:0
		;;	#}
	lapl|lapl-p)	#({
		# DISPLAY=`hostname -s`:0
		# DISPLAY=$hfull:0
		# echo "$DISPLAY" > ~/tmp/xxd
			#lapl-p:0	from `hostname -s`:0	LOG OUT
			#lapl-p.$JSDOM:0	from `$hfull:0` LOG OUT
			# Maybe these LOGOUTS are from the PLIP interface ?
		# export DISPLAY
		;;	#}
	*)	#({
		;;	#}
	esac		#}

case `hostname` in	#{
	dual.$JSDOM | \
	film.$JSDOM | \
	fire.$JSDOM | \
	john.$JSDOM | \
	king.$JSDOM | \
	lapa.$JSDOM | \
	lapd.$JSDOM | \
	lapl.$JSDOM | \
	lapn.$JSDOM | \
	laps.$JSDOM | \
	loft.$JSDOM | \
	mart.$JSDOM | \
	mini.$JSDOM | \
	park.$JSDOM | \
	rain.$JSDOM | \
	scan.$JSDOM | \
	slim.$JSDOM | \
	temp.$JSDOM | \
	snow.$JSDOM | \
	sony.$JSDOM | \
	test.$JSDOM | \
	thin.$JSDOM | \
	wind.$JSDOM \
		)	#({ ie all host except lapl-p where this causes LOG OUT
	for i in $hosts ; do
		( sleep 7 ; rsh -t 200 $i bin/.csh/consolev `printenv DISPLAY`\
			$screen ) 2> /dev/null &
		# Sleep to allow time for previously called xauth.sh to
		# complete.
		# but we dont want to sleep too long, cos we're probably
		# waiting to mouse click another screen.

		# Note the DISPLAY env. var does not make it across the rsh,
		# so it is passed as a visible parameter, else
		# if you start an xession on eg lapd or lapl,
		# all the consolev''s get started on default disp:0 or laps:0.
		done
	;;	#}
esac		#}

# gkrellm -g -0 &
# -g -0 places it at right of screen,
# gkrellm patched out as top reports:
#  THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
#   1  22    0   148M 17796K select   0:37  1.76% gkrellm

# sleep 4
#	sleep else the focus sometimes used to go top left to open the
#	xsm window, before fvwm1 hah finished filling in the fourth
#	("graphics") block of its panel manager.

# Session Manager Proxy
#	If one is test running this .xsession after a session has already
#	started, then this will produce an error:
#		smproxy: unable to connect to session manager
#	maybe JJLATER I might add a test if one is aleady running.
smproxy

# Next xterm is seen after fvwm finishes
#	So I will realise if I kill this xterm, it would log me out.
#	Sometimes when I am running xrandr multi headed I may not want to
#	logout, but just want to start a new fvwm with a different screen
#	config.
#	(However I will not see the title bar if fvwm stops running).
xterm -bg lightblue -title "xterm from .xsession" -g 80x24+480-20
# -bg lightblue -title "xterm from ~/.xsession"
# -g 80x24+0-20
#	Otherwise when fvwm exits, the xterm is at top left of screen,
#	which is not a displayed area when my screen comprises 2 output
#	devices, when xrandr shows:
#		VGA-0 connected 480x640+0+320 left \
#			(normal left inverted right x axis y axis) 0mm x 0mm
#		DVI-0 connected primary 1280x960+480+0 \
#			(normal left inverted right x axis y axis)
#			 598mm x 336mm
#	First I had +0-0 ie base left, then I had +0-20 to leave
#	icons on base line showing, then after exiting session &
#	restarting a dual screen for some reason the SVGA showed
#	black background nothing except mouse, so move the xterm
#	in to the main DVI-0 with 80x24+480-20

# man xsm: "The last program executed by your .xsession file should be xsm."
