#!/bin/sh -

# 2022-07-11 gdm+lightdm logins failed for xsession logins after 22.04 upgrade.
#	As a bug workaround, installed /etc/X11/Xsession.d/01x11-has_option
#	from https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/1922414 #7

# 2013-10-23 found that (most) new kinzler & root sessions from Ubuntu lightdm
#	were failing due to bad .Xauthority files, it worked if they were
#	deleted first, so may need to handle this differently now? ...
#	(note, root maybe just because it gets copies sometimes of kinzler
#	.Xauthority file)

# Note: Newer Ubuntu (13.*?) systems seem to not use .Xauthority files anymore
#	but can have local users enabled via xhost(1); made xhost+r and xhost-r
#	aliases to enable root shells to start X commands (let's leave it off
#	for root by default jic).

# Note: On (newer?) gdm systems, it's probably best to start X11 via
#	a "User Defined Session" in gdm, using my .xsession file,
#	instead of this script, assuming there is or can be made
#	/usr/share/xsessions/xsession.desktop.  You may also be able
#	start a recovery terminal from gdm and run .xsession manually
#	from there.  Otherwise, consider approaches in Notes below to
#	avoid gdm.
#		% cat /usr/share/xsessions/xsession.desktop
#		[Desktop Entry]
#		Name=User Defined Session
#		Comment=Custom ~/.xsession script
#		Exec=default
#		% cat /usr/share/xsessions/xterm.desktop
#		[Desktop Entry]
#		Encoding=UTF-8
#		Name=Recovery Console
#		Comment=Failsafe session with only xterm
#		Exec=xterm
#		TryExec=xterm
#		Icon=
#		Type=Application
#		X-Ubuntu-Gettext-Domain=gdm

# Note: On (older?) grub[1] systems, you can make a new first entry in
#	/boot/grub/menu.lst copying the lastest entry but with
#	"[text] /boot/grub/menu.lst" appended to the title,
#	s/quiet splash/text/ in the kernel line and without the "quiet" line,
#	for a default entry that boots into console mode.
# Note: On grub2 systems, you may be able to something similar via the
#	/etc/default/grub and boot/grub/grub.cfg files, though this is
#	unexplored.  Installing startupmanager may help with this.

# Note: On grub systems, you may have "(recovery mode)" options to eventually
#	boot into console mode, though this may not work as expected with
#	grub2.  Try pressing Esc (or Shift) during bootup to get grub menu
#	if it skips it by default.
# Note: On Ubuntu 15.10, it was noticed that /etc/default/grub needs to be
#	fixed from "GRUB_HIDDEN_TIMEOUT=0" to "GRUB_HIDDEN_TIMEOUT=" to get
#	the grub menu to come up.  It may be preferred to also make
#	GRUB_CMDLINE_LINUX_DEFAULT and GRUB_CMDLINE_LINUX empty, too.

# Note: On (older?) gdm systems (like Ubuntu pre-11.10), you can
#		# echo manual > /etc/init/gdm.override
#	to suppress the graphical login, so you can get to console mode
#	after a Ctrl-Alt-F1.
#	Then you can also append this to /etc/motd.tail:
#		 * Run `start gdm` as root for graphical login and desktop.

# Note: You can also perhaps
#		# telinit 1
#	(after a Ctrl-Alt-F1 to get to a terminal login)
#	to bring a system down to single-user, non-graphical mode.
#	For convenience, make this command and login:
#		# (echo '#!/bin/sh -'; echo exec `which telinit` 1) > \
#		  /usr/local/bin/rl1; chmod +x /usr/local/bin/rl1
#		rl1:x:0:0:Runlevel 1 Single-User,,,:/tmp:/usr/local/bin/rl1
#		rl1:???:14557:0:99999:7:::
#	With Ubuntu 11.10 (and later?), add this as the first line of the
#	script to address its dbus problem:
#		rm /run/dbus/*; /etc/init.d/dbus restart
#	You may also wish to tweak /etc/init/cups.conf and others so they're
#	running in runlevel 1.  You may also append this to /etc/motd.tail:
#		 * Run `rl1` as root to disable graphical login.

XINITRC=${XINITRC-"$HOME/etc/xinitrc"}

case "`opsys`" in
sunos5)	args='-ar1 200 -ar2 25';;
*)	args=;;
esac

# x - X11 startup script
# Steve Kinzler, steve@kinzler.com, Feb 94/Jan 02/Jun 12
# https://kinzler.com/me/home.html#x11

case "$1" in
-h)	cat << EOF 1>&2
usage: $0 [ = | =if ] [ Xserver_arg ... ]
	=	skip DNS lookups for xauth FQDNs
	=if	start network interface "if"
Runs any \$XINITCLIENT starting with a / on the new X server,
default $XINITRC.
Possible values might be found in /usr/share/xsessions/* Execs.
Useful Xserver_args may be like ":1 vt8" to run the server on virtual
terminal 8 (Ctrl-Alt-F8) with display :1.  (Safer to exit by killing
this script in this scenario rather than exiting the client.)
EOF
	exit 1;;
esac

case "$XINITCLIENT" in
/*)	;;
?*)	XINITCLIENT=;;
esac

DISPLAY=${DISPLAY-:0}
echo "DISPLAY=$DISPLAY"

export XINITRC DISPLAY

case "$1" in
=)	echo 'Skipping DNS lookups for xauth FQDNs.'
	NO_DNS=1; export NO_DNS;;
=*)	if=`echo "$1" | sed 's/=//'`; shift
	echo "Starting network interface $if ..."
	ifup "$if";;
esac

mkxauthority "$DISPLAY"
for arg
do
	case "$arg" in
	*:[0-9]*)	mkxauthority "$arg";;
	esac
done

NO_DNS=

case "$LANG" in
''|C)	LANG=en_US;;	# for 8-bit cut+paste, SEE ALSO etc/xinitrc
esac
export LANG

exec ssh-agent xinit $XINITCLIENT -- ${1+"$@"} \
	-auth ${XAUTHORITY-$HOME/.Xauthority} $args
