#!/bin/ash
# generic wrapper to run as ${XUSER} (when currently running as root)
# (C) James Budiono 2012, 2017
# License: GPL version 3 or later
#

#set -x

XUSER=spot

case $0 in *run-as|*run-as-user)
	if ! [ "$1" ] ; then
		echo "$0: Specify user"
		exit 1
	fi
	XUSER=$1
	shift
esac

PROG="$1"

case $0 in
run-as-spot|*/run-as-spot) ;;
*) PROG="$0" ;;
esac

case "$PROG" in
*/*) EXEC="$PROG" ;;
*)
	EXEC=`command -v "$PROG"`
	[ -z "$EXEC" ] && EXEC="$PROG"
	;;
esac

# if spot-sandbox is present, run the shell inside a sandbox
SANDBOX=
case "$EXEC" in
*.AppImage|flatpak|*/flatpak|/var/lib/flatpak/*) ;;
*/dbus-daemon) ;; # Landlock breaks xdg-desktop-portal, which fails to open /proc/%u/root
*) SANDBOX=`command -v spot-sandbox` ;;
esac

CWD=$PWD
CMD=''
while [ "$1" ]; do
	CMD="$CMD \"$1\""
	shift
done

[ "$CMD" ] || exit

#test for chromium-based browsers (if true, launch with dbus-launch rather than exec)
CBB=$(echo "$CMD" | grep -Ei -- 'chrome|chromium|brave|iron|opera|slimjet|vivaldi|microsoft-edge')
[ -n "${CBB}" ] && EXE=dbus-launch || EXE=exec
export EXE

USER_HOME=$(awk -F: '$1=="'"${XUSER}"'" {print $6}' /etc/passwd)
if ! [ "${USER_HOME}" ] ; then
	echo "$0 ERROR: could not HOME dir for user $XUSER"
	exit 1
fi
CURDIR=$PWD

if [ $(id -u) -eq 0 ]; then
	[ $XAUTHORITY ] && cp $XAUTHORITY ${USER_HOME}/.Xauthority 2>/dev/null
	touch ${USER_HOME}/.Xauthority
	export XAUTHORITY=${USER_HOME}/.Xauthority

	# replace all occurences of /root in XDG_* with /home/spot, because we don't
	# run a login shell and source /etc/profile.d/*
	OLD_HOME="$HOME"
	while IFS='=' read NAME VAL; do
		case "$NAME" in
		XDG_*) export $NAME="`echo "$VAL" | sed -e s~^$OLD_HOME~$USER_HOME~ -e s~:$OLD_HOME~:$USER_HOME~g`" ;;
		esac
	done << EOF # hack for old busybox, which doesn't understand <() and <<<
`env`
EOF

	export XDG_CONFIG_HOME=${USER_HOME}/.config
	export XDG_CACHE_HOME=${USER_HOME}/.cache
	export XDG_DATA_HOME=${USER_HOME}/.local/share
	export XDG_STATE_HOME=${USER_HOME}/.local/state

	for i in ${XDG_CONFIG_HOME} ${XDG_CACHE_HOME} ${XDG_DATA_HOME} ${XDG_STATE_HOME}
	do
		if ! [ -d $i ] ; then
			mkdir -p $i
			chown ${XUSER} $i
		fi
	done

	export XDG_RUNTIME_DIR=/tmp/runtime-${XUSER}
	if [ ! -d ${XDG_RUNTIME_DIR} ] ; then
		mkdir -p ${XDG_RUNTIME_DIR}
		chmod 0700 ${XDG_RUNTIME_DIR}
		chown ${XUSER} ${XDG_RUNTIME_DIR}
	fi

	if [ -s /tmp/.spot-session-bus ]; then
		. /tmp/.spot-session-bus
		export DBUS_SESSION_BUS_ADDRESS
		export DBUS_SESSION_BUS_PID
	fi

	# close all file descriptors except std{in,out,err}, in case one of
	# them points to a file under /root
	if [ "$PROG" != "/usr/bin/Xwayland" ]; then
		for FD in /proc/self/fd/*; do
			FD="${FD##*/}"
			[ $FD -gt 2 ] && eval "exec ${FD}<&-"
		done
	fi

	exec su ${XUSER} -s /bin/ash -c '
# try to switch to original directory, unless it is under /root
case "'"$CURDIR"'" in
/root|/root/*)
	cd "'"$USER_HOME"'"
	;;
*)
	cd "'"$CURDIR"'"
	;;
esac
$EXE '"${SANDBOX}"' '"$CMD"'
'
else
	exec ash -c "exec $CMD"
fi

### END ###
