#!/bin/bash

Info () {
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2020"
app="$(basename $0)"
msgid="The Starbuntu application
<i>\$app</i>
was written by"
info="$(gettext "$msgid" | (export PATH `envsubst --variables "$msgid"`; envsubst "$msgid"))"

yad --center --fixed --title="$title" --window-icon="info" --image="info" --borders=20 --text="$info"$'\n'$'\n'"$author"$'\n'"$mail"$'\n'$'\n'"$year"$'\n' --text-align=center --selectable-labels --button="OK" &
wunwa "$title" > /dev/null; xdotool key Home key Down
}

pw_test () {
    if [ $err -eq 1 ] || [ $err -eq 127 ]; then
	[ $err -eq 1 ] && warning=$"Wrong password!&#10;Repeat input?&#10;"
	[ $err -eq 127 ] && warning=$"Command&#10;${c[3]}&#10;wrong!&#10;Repeat input?&#10;"
	yad --center --title="$title" --text="$warning" --window-icon=$HOME/.icons/Golden_key.svg --borders=20 --button=$re1:0 --button=$re2:1
    else
	pass=true
    fi
}

param="$1"
[ "$param" = "info" ] && Info && exit 0

params="$@"; pass=false
[[ "$params" =~ "--no-error-msg" ]] && error_msg=false && params="${params/--no-error-msg/}" || error_msg=true
if [ -z "$params" ] || [ "$params" = '%f' ]; then
    params="xterm"
else
    params="$(quote-files-in-string $params)"
fi
xhost + > /dev/null

lang_dir=$"C"
title=$"Authentication"
tab1=$"Password:"
tab2=$"User:"
tab3=$"Command&#58; "
button1=$"Cancel"
button2=$"Help"
re1=$"Yes"; re2=$"No"

usr=($(ls /home))
usr+=(root)
n_users=${#usr[@]}
height=$(( 90+$n_users*25 ))

n=$RANDOM   # $((1 + $RANDOM % 255))
while ! $pass; do
	warning=""
	if ! $pass; then
	    sh -c 'wunwa "$title" > /dev/null; xdotool key Tab' &	
	    if [ -n "${c[2]}" ]; then
		checked="${c[2]::-1}"
		checked="${checked#*|}"
	    else
		checked="$(whoami)"
	    fi
	    for ((k=0; k<$n_users; k++)); do 
		if [ "${usr[$k]}" = "$checked" ]; then bool[$k]=true; else bool[$k]=false; fi
	    done
	    for ((k=0; k<$n_users; k++)); do bool_usr[$k]="${bool[$k]} ${usr[$k]}"; done
	    f[1]=$(mktemp); f[2]=$(mktemp); f[3]=$(mktemp)
	    yad --plug=$n --tabnum=1 --height=$height --entry --entry-text="${c[1]}" --hide-text 1>${f[1]} &
	    yad --plug=$n --tabnum=2 --height=$height --radiolist --list --no-headers --column=Tick --column=User ${bool_usr[@]} 1>${f[2]} &
	    yad --plug=$n --tabnum=3 --height=$height --entry --entry-text="$params" 1>${f[3]} &
	    sleep 0.3
	    yad --center --notebook --key=$n --title=$title --width=300 --height=$height --window-icon=$HOME/.icons/Golden_key.svg --tab="$tab1       " --tab="$tab2       " --tab="$tab3      " --button="$button1":1 --button="$button2":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#gsu" --button="Info":"$0 info" --button="OK":0
	    exitstatus=$?

	    params="$(cat ${f[3]})"
	    for ((k=1; k<4; k++)); do c[k]="$(cat ${f[k]})"; rm ${f[k]}; sleep 0.03; done
	    [ -z "${c[3]}" ] && c[3]="xterm"

	    if [ $exitstatus -eq 0 ]; then
		user=${c[2]::-1}
		user=${user#*|}
		[ $user = "root" ] && home="/root" && USER="root" || home="/home/$user"
#		own_pass="$(su "$(whoami)" -c true <<< "${c[1]}" 2> /dev/null && echo true || echo false)"
		own_pass="$(su "$(whoami)" <<< "${c[1]}" 2> /dev/null && echo true || echo false)"
		cmd="${c[3]%% *}"; cmd="${cmd//\"}"
		[ -z "$(which "$cmd")" ] && ([ -d "$cmd" ] || [ -f "$cmd" ]) && c[3]="all-open ${c[3]}"
		if [[ "$user" != "$(whoami)" ]] && ! $own_pass; then
		    su $user -c ". $home/.profile; exec ${c[3]}; exit $?" <<< "${c[1]}" 2> /dev/null
		    err=$?
		    [ $err -gt 0 ] && $error_msg && pw_test || pass=true
		else
		    user="-u $user"
		    [ "$user" = "-u $(whoami)" ] && user=""
#		    sudo -SE $user bash -c "exec ${c[3]}; exit $?" <<< ${c[1]} 2> /dev/null
		    sudo -S $user bash -c "exec ${c[3]}; exit $?" <<< ${c[1]} 2> /dev/null
		    err=$?
		    [ $err -gt 0 ] && $error_msg && pw_test || pass=true
		fi
	    else
		pass=true
	    fi
	fi
done

exit $exitstatus
