#!/bin/sh -
## **************************************************************************
## Derived from the PCDM system in PC-BSD/TrueOS
## **************************************************************************

allow-auto-login() {
	# Allow Auto-Login the first time PCDM starts
	touch "/tmp/.PCDMAutoLogin"
}

check-bootflags() {
	echo 1>&2 "Checking for X display setup flags"

	# Check whether the user has selected to (re-)run stuff from the boot loader.
	if runwiz="`kenv runwiz 2>/dev/null`"
	then
		if test _"${runwiz}" = _"YES"
		then
			rm -f -- "runxsetup"
			kenv -u runwiz
		fi
	fi
	if xvesa="`kenv xvesa 2>/dev/null`"
	then
		if test _"${xvesa}" = _"YES"
		then
			touch "runxvesa"
			kenv -u xvesa
		fi
	fi
	# Check whether the user has selected to (re-)run stuff from the PCBSD Control Panel.
	if test -e /var/.runxsetup
	then
		rm -f -- "runxsetup"
		rm -f -- /var/.runxsetup
	fi
	if test -e /var/.pcbsd-firstgui
	then
		rm -f -- "runfirstgui"
		rm -f -- /var/.pcbsd-firstgui
	fi
}

run_firstboot() {
	fluxbox &
	PID=$!
	userenv --default-path --default-tools --default-timezone --set-other --default-locale pc-firstboot
	kill -9 -- ${PID}
}

run() {
	## \bug FIXME: This ignores the setting in /etc/login.conf .
	test -e /var/run/nologin && exit 111

	# Switch to VESA 1024x768 if there is a flag file saying to do so.
	if test -e "runxvesa"
	then
		rm -f -- "runxvesa"
		echo 1>&2 "Using failsafe VESA 1024x768 mode."
		mv /etc/X11/xorg.conf /etc/X11/xorg.conf.preVesa.backup
		cp /usr/local/share/pcbsd/xorg-gui/cardDetect/XF86Config.compat /etc/X11/xorg.conf
	fi

	# (Re-)run the display setup wizard if there is no flag file saying that it has been run.
	if ! test -e "runxsetup"
	then
		touch "runxsetup"
		exec /usr/local/share/pcbsd/xorg-gui/bin/rundetect.sh
	fi

	# Run first boot setup if there is no flag file saying that it has been run.
	if ! test -e "runfirstgui"
	then
		touch "runfirstgui"
		cp -R /usr/local/share/pcbsd/fluxboxinit /root/.fluxbox
		exec startx "$0" run_firstboot
	fi

	display=":0"

	# Randomize the authorization file location
	authfilesdirectory="/run/PCDM/auth"
	install -d -m 01777 "${authfilesdirectory}"
	authfile="${authfilesdirectory}/`openssl rand -hex 10`"
	export XAUTHORITY="${authfile}" # Set the auth file location
	: > "${authfile}" # Create the (empty) authorization file
	xauth -f "${authfile}" add "${display}" MIT-MAGIC-COOKIE-1 "`openssl rand -hex 64`" # Add a default entry to the file (randomized)

	# Clear out a failed marker
	rm -f -- "/tmp/.pcdm-x-failed"

	# Remove our X success marker
	rm -f -- "/tmp/.pcdm-x-started"

	# Now start the X Server
	# The xinit will be a local reaper, which it can handle.
	# The "session" instance of this helper will be a local reaper, which the shell can handle.
	local-reaper true xinit /bin/exec local-reaper true "$0" session -- "${display}" -auth "${authfile}" -retro $Xflags
	rc=$?

	# Now remove the authorization file
	rm -f -- "${authfile}"

	return ${rc}
}

fall-back-on-failure() {
	case "$1" in
		e*)
			if test 0 -ne "$2"
			then
				 if ! test -e "/tmp/.pcdm-x-started" ; then
					# Looks like X failed to start...
					if test -e "/tmp/.pcdm-x-failed" ; then
						# Wow, we really failed here, lets try running X wizard
						rm -f -- "runxsetup"
						rm -f -- "/tmp/.pcdm-x-failed"
					else
						# Lets move xorg.conf away, and try again
						touch "/tmp/.pcdm-x-failed"
						mv /etc/X11/xorg.conf /etc/X11/xorg.conf.prevFailed 2>/dev/null
					fi
				fi
			else
				:
			fi
			;;
		t*)
			;;
		k*)
			;;
		a*)
			;;
		c*|*)
			;;
	esac
}

session() {
	dbus-launch --exit-with-session PCDM-session -once
}

eval "$@"
