#!/bin/sh -

CTWM_LOOP=true	# 2022-07-27 need ctwm 4.0.3 but it can crash?
		#	see/do also Function "Exit" in twmrc
		# 2023-11-10 now also use to reload ENV from .cshrc

XRESOURCES=${XRESOURCES-$HOME/etc/xresources.ad}
export XRESOURCES

# Not sure why not in X11 environment via .xsession ... assumes is defined
export LD_LIBRARY_PATH=`csh -c 'echo $LD_LIBRARY_PATH'`

# .xinitrc - personalized X11 client startup script
# Steve Kinzler, steve@kinzler.com, Oct 93
# https://kinzler.com/me/home.html#x11

# Consider these if they exist:
# 	. /etc/X11/xinit/xinitrc.common
# 	/etc/X11/xinit/xinitrc.d/*

trap 'exec xterm -ls -T Failsafe -geometry 80x24-0-0' \
	EXIT SIGHUP SIGINT SIGPIPE SIGTERM SIGIO

cd $HOME/work || exit $?

# Run "# locale-gen en_US; dpkg-reconfigure locales" for Ubuntu errors here
# Setting LC_ALL here screws up sorting, etc; LANGUAGE may be okay
case "$LANG" in
''|C)	LANG=en_US;;	# for 8-bit window titles and xcb, SEE ALSO binp/x
esac
export LANG
L="$LANG"; export L
case "$LANG" in
en_US.UTF-8)	CTWM_LANG=en_US;;	# avoid big titlebars &tc w/ UTF-8
*)		CTWM_LANG="$LANG";;
esac

unset  WINDOWID
export WINDOWID

xterm="`xterm -which`"
case "$xterm" in
/*)	XTERM="$xterm"; export XTERM;;
esac
# Except on primary (:0) DISPLAYs, eg on RDP desktops, we assume that urxvt
#   isn't safe to use with its backgroundPixmap's that crash.  (Handling it
#   in xtbg isn't really feasible -- see xtbg).  HACK KLUDGE
# 2018-06-27 With Ubuntu 18.04, it seems primary DISPLAYs may be :1.0, so
#   let's try excepting that, too; see also xloaddash(1)
case "$XTERM,$DISPLAY" in
*,*:[01]|*,*:[01].*)	;;
*urxvt*,*)		xterm=`echo "$BASEPATH" | sed 's/ /:/g'`
			xterm=`PATH="$xterm" which xterm`
			case "$xterm" in
			/*)	XTERM="$xterm"; export XTERM;;
			*)	unset XTERM;;
			esac;;
esac

host=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
case "$host" in
'')     host=`uname -n`;;
esac

constype=`(constype) 2> /dev/null`

###############################################################################

vbox=/etc/X11/Xsession.d/98vboxadd-xclient
# run any VirtualBox Guest Additions client so we can disable mouse integration
# and entirely capture the mouse for proper ctwm/xwit/etc functioning
sh "$vbox" 2> /dev/null &

rm -f "$HOME/.cache/xdo-initcycle-state" 2> /dev/null

case "$XWM_1ST" in
CTWM)	LANG="$CTWM_LANG" ${CTWM-ctwm} -W -f ${CTWMRC-$HOME/.ctwmrc} &
	sleep 2;;
TWM)	kitty &
	twm -f ${TWMRC-$HOME/.twmrc} &
	sleep 2;;
?*)	kitty &
	$XWM_1ST &
	sleep 2;;
esac

xrdb -Uunix -Usun ${XRESOURCES-$HOME/.Xdefaults}

(audioctl -s; audioctl -v 12; env LANG=C speckeysd) 2> /dev/null &

case "$LINUX" in
?*)	case "$host" in
	[pl]kinzler*)	LINUX=del; export LINUX;;
	*)		LINUX=bs;  export LINUX;;
	esac;;
esac

case "$host" in
aludel*)	xdo dynex padoff 2> /dev/null;;
vkinzler*)	xdo jelly	 2> /dev/null;;
esac

###############################################################################

rm -fr "$HOME/.mozilla/tmp/.??*" "$HOME/.mozilla/tmp/*"

xt -C -name xterm-console &

(xset fp+ /l/fonts/TrueType
 xset fp+ /l/fonts/85dpi,/l/fonts/charsets,/l/fonts/75koi8) &
xdo setrepeat 2> /dev/null &
test -f "$vbox" && xset m 1 0 2> /dev/null &

xtmain 1 &

#case "$MAILHOST,$MAILHOST2,$MAILHOST3" in
#$host*|*,$host*)	facemon -G xmail &;;
#esac

xcb -name xcb-placed &
#dclock -date '%w, %d %m %y' &	#1988
#dclock -date '%a %e %b %y %Z%z' &
dclock -date '%a %y-%m-%d %Z%z' &
#xeyes -name xeyes-placed &

(sleep  1; xloaddash -all) &	# <- this sleep is for fast vkinzler
(sleep 30; xloaddash) &		#    else ssh for these disables mux for terms
(sleep 60; xloaddash) &
(sleep 90; xloaddash) &

#test -f ../.icq/micqrc && xterm -e micq &
#test -d ../.Skype && sleep 5 && skype &

rhn-applet-gui &
#xdo panel &	# kinda a pain w/ my ctwm 4.0.3 so let's start w/o

#automount=/usr/lib/gnome-settings-daemon/gnome-fallback-mount-helper
#test -x "$automount" && "$automount" &	# eg to ~/.gvfs or /media
# or use nautilus to manually manage removable device mounts

# Noted that xscreensaver might have hosed the mouse in a VirtualBox session;
# VMs should use the host OS's screensaver anyway
#test -f "$vbox" || screensaver start &
# Let's try screensaver in VirtualBox again anyway:
screensaver start &

xhost - > /dev/null &

imgbg RANDOM 1 2 3 4

test ! -d $HOME/.mozilla && { mkdir $HOME/.mozilla
			      chmod 700 $HOME/.mozilla; }
cp $HOME/etc/img/firefox.xpm $HOME/.mozilla/icon.xpm

xmod	# before wm but not too soon on fast machines ?

case "$XWM,$XWM_1ST" in
?*,)	kitty &
	$XWM;;
,)	ctwm=${CTWM-ctwm}
	case "`($ctwm -display xBOGUSx) 2>&1`" in
	*isplay*|*xBOGUSx*)
		case "$CTWM_LOOP" in
		?*)	while :; do	# handle ctwm crashes if needed
			LANG="$CTWM_LANG" csh -c \
				    "exec $ctwm -W -f ${CTWMRC-$HOME/.ctwmrc}"
			done;;		# kill xinitrc to exit
		*)	LANG="$CTWM_LANG" $ctwm -W -f ${CTWMRC-$HOME/.ctwmrc};;
		esac;;
	*)	kitty &
		twm			   -f ${TWMRC-$HOME/.twmrc};;
	esac;;
*)	case "$XWM_EXIT" in
	?*)	$XWM_EXIT;;	# set to "true" eg to exit immediately
	*)	xlogo -name xlogo-placed;;	# can't use -render in Xnest
	esac;;
esac

###############################################################################

case "$constype" in
?*)	kbd_mode -a
	(clear_colormap) 2> /dev/null;;
esac
