#!/bin/sh

set -- /usr/bin/sway "$@"

for spec in \
	XDG_CACHE_HOME="$HOME/.cache"      \
	XDG_CONFIG_HOME="$HOME/.config"    \
	XDG_DATA_HOME="$HOME/.local/share" \
	XDG_RUNTIME_DIR="$TMP/runtime"     \
;
do
	env_name="${spec%%=*}"
	env_value="${spec#*=}"

	eval '[ -z "$'$env_name'" ] || continue'

	[ -d "$env_value" ] ||
		mkdir -p -- "$env_value"

	export "$spec"
done

[ ! -e "$XDG_CONFIG_HOME/user-dirs.dirs" ] ||
	. "$XDG_CONFIG_HOME/user-dirs.dirs"

if [ -z "$XDG_VTNR" ]; then
	XDG_VTNR="$(tty)"
	XDG_VTNR="${XDG_VTNR##/dev/tty}"
	export XDG_VTNR
fi

for config in \
	"$HOME/.sway/config"           \
	"$XDG_CONFIG_HOME/sway/config" \
	"/etc/sway/config"             \
;
do
	if [ -f "$config" ]; then
		set -- "$@" --config "$config"
		break
	fi
done

###
### Additional Environment Variables
###

# WLR_BACKENDS: comma-separated list of backends to use (available backends:
# libinput, drm, wayland, x11, headless)
#export WLR_BACKENDS=

# WLR_EGL_NO_MODIFIERS: set to 1 to disable format modifiers in EGL, this can
# be used to understand and work around driver bugs.
#export WLR_EGL_NO_MODIFIERS=

# WLR_RENDERER: forces the creation of a specified renderer (available
# renderers: gles2, pixman, vulkan
#export WLR_RENDERER=

# gles2 renderer-specific variables
# WLR_RENDERER_ALLOW_SOFTWARE: allows the gles2 renderer to use software
# rendering
#export WLR_RENDERER_ALLOW_SOFTWARE=1

# Launch DBus if needed
[ -n "$DBUS_SESSION_BUS_ADDRESS" ] || ! which dbus-launch >/dev/null 2>&1 ||
	set -- dbus-launch --exit-with-session "$@"

exec "$@"
