#!/bin/sh
#
# Salt's .xinitrc
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
#
# Distributed under terms of the MIT license.
#

die() {
	echo "Failed to start session" >&2
	exit 1
}

step_setup_target() {
	# Set up our session target
	which i3 > /dev/null 2>&1 && export STARTUP="i3"
	which bspwm > /dev/null 2>&1 && export STARTUP="bspwm"
	[ -x "$HOME/.concession/concession" ] && export STARTUP="$HOME/.concession/concession"
	[ -z ${STARTUP+x} ] && die
}

step_setup_env() {
	# Ideally, you'd hook into a bunch of different DE startup stuff here
	# But I only use Plasma, so there's only that here
	if which kdeinit5 > /dev/null 2>&1; then
		kdeinit5 &
		kded5 &
		export STARTUP="ksmserver \"$STARTUP\" --no-lockscreen"
	fi
}

step_start_xsession() {
	# Execute Xsession stuff
	[ -r /etc/X11/Xsession ] && . /etc/X11/Xsession
}

step_start_manual() {
	# Execute our target directly
	[ -z "${STARTUP+x}" ] || exec "$STARTUP"
}

main() {
	# Set up the environment
	step_setup_target
	#step_setup_env
	# Execute our target
	step_start_xsession # Through Xsession scripts, or...
	step_start_manual # ...manually, if need be
	# Or maybe just die, I guess
	die
}

main $@