#!/bin/dash

if test -x "$1"; then
	unset OPTIONS
	unset LD_LIBRARY_PATH

	# Some additional options for root
	if test $(id -u) -eq 0; then
	
		# Some AppImages may need it (Kiwix)
		export QTWEBENGINE_DISABLE_SANDBOX=1
		
		case "$1" in
			*'lmms-'*)
				# LMMS requires this custom option
				OPTIONS='--allowroot'
				;;
			*)
				# Electron apps need '--no-sandbox' option for root
				OFFSET=$(grep -abo -m2 'hsqs' "$1" | awk -F: 'NR==2 {print $1}' ) # 2nd 'hsqs' is "the one"
				[ "$OFFSET" ] && unsquashfs -o $OFFSET -l "$1" 2> /dev/null | grep -qw -m1 'chrome-sandbox' && OPTIONS='--no-sandbox'
				;;
		esac
		
	fi

	# check if there is a config file
	[ -r /etc/run-appimage.shinit ] && . /etc/run-appimage.shinit
	exec "$@" $OPTIONS
else
	Xdialog --title "Warning" --msgbox "Please enable executable bit to run this AppImage." 0 0 10000
fi
