#!/bin/bash

parse() {
	sudo_user="root"
	sudo_command=""
	sudo_login_shell=""
	local args=("$@")
	local i=0
	local arg_count=${#args[@]}

	expand_flags() {
		local expanded=()
		for arg in "$@"; do
			if [[ $arg =~ ^-[a-zA-Z]{2,}$ ]]; then
				local chars="${arg:1}"
				for ((j=0; j<${#chars}; j++)); do
					expanded+=("-${chars:$j:1}")
				done
			else
				expanded+=("$arg")
			fi
		done
		args=("${expanded[@]}")
		arg_count=${#args[@]}
	}

	expand_flags "$@"

	while [[ $i -lt $arg_count ]]; do
		case "${args[$i]}" in
			-u|--user)
				((i++))
				if [[ $i -lt $arg_count ]]; then
					sudo_user="${args[$i]}"
				fi
				((i++));;
			-i|--login)
				sudo_login_shell=true
				((i++));;
			-s|--shell)
				((i++));;
			--)
				((i++))
				sudo_command="${args[*]:$i}"
				break;;
			-*)
				((i++));;
			su)
				((i++))
				local su_user=""
				local su_args=()
				while [[ $i -lt $arg_count ]]; do
					su_args+=("${args[$i]}")
					((i++))
				done
				local su_expanded=()
				for arg in "${su_args[@]}"; do
					if [[ $arg =~ ^-[a-zA-Z]{2,}$ ]]; then
						local chars="${arg:1}"
						for ((j=0; j<${#chars}; j++)); do
							su_expanded+=("-${chars:$j:1}")
						done
					else
						su_expanded+=("$arg")
					fi
				done
				local k=0
				local su_count=${#su_expanded[@]}
				while [[ $k -lt $su_count ]]; do
					case "${su_expanded[$k]}" in
						-|-l|--login)
							sudo_login_shell=true
							((k++));;
						-c|--command)
							((k++))
							if [[ $k -lt $su_count ]]; then
								sudo_command="${su_expanded[$k]}"
								((k++))
							fi;;
						--session-command=*)
							sudo_command="${su_expanded[$k]#*=}"
							((k++));;
						-m|-p|--preserve-environment)
							((k++));;
						-s|--shell)
							((k++))
							if [[ $k -lt $su_count && "${su_expanded[$k]}" != -* ]]; then
								((k++))
							fi;;
						-*)
							((k++));;
						*)
							if [[ -z "$su_user" ]]; then
								su_user="${su_expanded[$k]}"
								((k++))
							else
								((k++))
							fi;;
					esac
				done
				if [[ -n "$su_user" ]]; then
					sudo_user="$su_user"
				fi
				break;;
			*)
				sudo_command="${args[*]:$i}"
				break;;
		esac
	done
}
parse "$@"

[ -z "$WSCOMPAT_MASTER" ] && WSCOMPAT_MASTER="$(whoami)"
read -r -d '' init_cmd << WSCOMPAT_EOF_ahkiem3K || true
	export WS_IS_COMPAT=1
	export WSCOMPAT_MASTER="$WSCOMPAT_MASTER"
	export WSCOMPAT_DIR="/tmp/wscompat_${WSCOMPAT_MASTER}_\$(whoami)"
	rm -rf "\$WSCOMPAT_DIR" && mkdir -p "\$WSCOMPAT_DIR" && chmod 700 "\$WSCOMPAT_DIR"
	cat << 'WSCOMPAT_EOF_daePe0Ph' > "\$WSCOMPAT_DIR/profile"
$(
	sed 's/WSCOMPAT_EOF_/WSCOMPAT_EOF__/g' "$WSCOMPAT_DIR/profile"
	for file in "$WSCOMPAT_DIR/profile"*.sh; do
		if [ -f "$file" ]; then
			sed 's/WSCOMPAT_EOF_/WSCOMPAT_EOF__/g' "$file"
		fi
	done
)
WSCOMPAT_EOF_daePe0Ph
	. \$WSCOMPAT_DIR/profile
WSCOMPAT_EOF_ahkiem3K

if [ -z "$sudo_command" ]; then
	sudo_command="exec /bin/bash --rcfile \"\$WSCOMPAT_DIR/bashrc\""
fi
if [ "$sudo_login_shell" ]; then
	exec /bin/sudo -u "$sudo_user" \
		/bin/bash --login --noprofile --norc -c "$init_cmd; cd; $sudo_command"
else
	exec /bin/sudo -u "$sudo_user" \
		/bin/bash --noprofile --norc -c "$init_cmd; $sudo_command"
fi
