#!/usr/bin/env bash

# Bash strict mode
set -euo pipefail

is_parent_volatile() {
	local parent_pid="$PPID"
	if [[ "$parent_pid" -eq 1 ]]; then
		return 0
	fi
	local parent_exe
	parent_exe="$(readlink -f "/proc/$parent_pid/exe" 2>/dev/null || true)"
	if [[ -z "$parent_exe" ]]; then
		return 0
	fi
	case "$parent_exe" in
		/usr/bin/fuzzel|/usr/bin/nwg-drawer|/usr/bin/wofi)
			return 0
			;;
		*)
			return 1
			;;
	esac
}

# Elevate
if [[ $EUID -ne 0 ]]; then
	if is_parent_volatile; then
		pkexec /usr/lib/garuda/pkexec-gui "$@" || { exit "$?"; }
		exit 0
	else
		exec pkexec /usr/lib/garuda/pkexec-gui "$@"
		exit 1
	fi
fi

# Launch dbus session if not already running
if [[ ! -v DBUS_SESSION_BUS_ADDRESS ]]; then
	eval $(dbus-launch --sh-syntax --exit-with-session)
fi

# Restore some variables from parent process
while IFS= read -rd '' var; do export "$var"; done < <(grep --null-data -ae "^\(HOME\|XDG_CURRENT_DESKTOP\|WAYLAND_DISPLAY\|XDG_RUNTIME_DIR\|XDG_CONFIG_DIRS\|XDG_SESSION_TYPE\|XDG_SESSION_PATH\|XCURSOR_SIZE\|KDE_SESSION_VERSION\|LC_.*\|LANG\|LANGUAGE\|QT_WAYLAND_FORCE_DPI\|QT_QPA_PLATFORMTHEME\|QT_STYLE_OVERRIDE\)=.*\$" /proc/$PPID/environ)

# Temporary directory to store overlays
overlays_dir="$(mktemp -d)"
if [[ -z "$overlays_dir" ]]; then
	echo "Failed to create temporary directory" >&2
	exit 1
fi

mkdir "$overlays_dir"/{upper,work,home}
# Overlayfs mount
mount -t overlay overlay -o lowerdir="$HOME",upperdir="$overlays_dir/upper",workdir="$overlays_dir/work" "$overlays_dir/home"

# If not set, the default behavior of the program that is launched will be used automatically
if [[ -v XDG_CONFIG_DIRS ]]; then
	# Replace any mention of the user's home directory with the overlay in XDG_CONFIG_DIRS
	export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS//"$HOME"/"$overlays_dir/home"}"
fi

# Set HOME to the overlay
export HOME="$overlays_dir/home"

# Make sure wayland is configured correctly. This variable is usually not defined expliclty, so it will have to be generated
if [[ -v WAYLAND_DISPLAY ]]; then
	export WAYLAND_DISPLAY="$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"
fi
# We do not want root writing to user's XDG_RUNTIME_DIR
export XDG_RUNTIME_DIR="/run/user/0"

# Run program and store exit code
"$@" || code=$?

# Cleanup
umount "$overlays_dir/home"
rm -rf "$overlays_dir"

exit ${code:-0}
