#!/bin/bash

Info () {
local app
title="Info"
author="Peter Starfinger"
mail="E-Mail: info@die-starfingers.de"
year="2025"
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
}

user=$(whoami)
if [[ "$user" != "root" ]]; then
    gsu --no-error-msg $0 $@ & exit 0
fi

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

lang_dir=$"C"
if [[ ! "$param" =~ \-d|\-a ]]; then
    title=$"Manage Users"
    ask=$"&#10;&#9;<b>Shall a user be deleted or added?</b>&#10;&#10;"
    yad --fixed --title="$title" --window-icon="config-users" --image=dialog-question --borders=20 --text="$ask" --button=$"Cancel":2 --button=$"Help":"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#del-add-user" --button="Info":"$0 info" --button=$"Delete":1 --button=$"Add":0
    e_code=$?
    [ $e_code -eq 2 ] && exit 2
    [ $e_code -eq 1 ] && param="-d" || param="-a"
fi

IFS=$'\n'
users=("<u>USER-ID"$'\t'$"Username""</u>" $(getent passwd | grep bash | sort | grep -v root | cut -d: -f3,5 | tr ':' $'\t' | cut -d, -f1))
max=${#users[@]}; max=${users[$((max-1))]%$'\t'*}
button=$"Cancel"
button2=$"Help"

if [ $param = "-d" ]; then
    title=$"Delete User"
    ask=$"<b>Type number 1000 - $max:</b>"
    err=0; id=0
    while ([ $id \< 1000 ] || [ $id \> $max ]) && [ $err -eq 0 ]; do
	id=$(yad --center --title="$title" --window-icon="config-users" --image="config-users" --borders=20 --text="${users[*]}&#10;&#10;$ask" --entry --button="$button":1 --button="OK":0)
	err=$?
	[ $err -eq 1 ] && exit 1
    done
    if [ $id -eq 1000 ]; then
	reply=$"<b>&#10;&#9;The main user with the User_ID 1000&#10;&#9;cannot be deleted!&#10;</b>"
    else
	name="$(grep $id <<< ${users[*]} | cut -d$'\t' -f2)"
	remark=$"&#10;&#9;<b>Really delete the user $name?</b>&#10;"
	yad --center --title="$title" --window-icon="config-users" --image="config-users" --borders=20 --text="$remark" --button=$"No":1 --button=$"Yes":0
	[[ $? -eq 1 ]] && exit 1
	del_user=$(getent passwd | grep $name | cut -d: -f1)
        [ -f "/usr/share/sddm/faces/$del_user.face.icon" ] && rm /usr/share/sddm/faces/$del_user.face.icon
        rm -r /media/$del_user /mnt/$del_user
	reply="$(userdel -fr $del_user 2>&1)"
	[[ $? -eq 0 ]] && reply=$"&#10;&#9;<b>User $name deleted!</b>"
    fi
else
    title=$"Add User"
    text=$"<b>New User:</b>&#10;"
    field1=$"Name&#58; "
    field2=$"Login Name&#58; "
    field3=$"Password&#58; "
    entry=`yad --center --title="$title" --width=600 --height=200 --window-icon="config-users" --borders=10 --text="$text" --text-align=center --form --separator="¦" --field="$field1" $"e.g. ""Maria II" --field="$field2" $"e.g. ""maria_2 (a-z;0-9;-_.)" --field="$field3" $"e.g. ""m@=D0nNa ("$"only ""ASCII 33-126)" --button="$button":1 --button="$button2":"yelp /usr/share/help/$([ $LANGUAGE != "en" ] && echo "$LANGUAGE" || echo "C")/Starbuntu/starbuntu-apps-details.page#del-add-user" --button="Info":"$0 info" --button="OK":0`
    exit_code=$?
    [ $exit_code -eq 1 ] && exit $exit_code
    name="${entry%%¦*}"; entry="${entry#*¦}"
    login_name="${entry%%¦*}"; entry="${entry#*¦}"
    passwd="${entry::-1}"
    reply="$(useradd -c "$name" -p "$(perl -e "print crypt($passwd,'sa');")" -s /bin/bash -m "$login_name" 2>&1)"
    [[ $? -eq 0 ]] && reply=$"&#10;&#9;<b>User $name added!</b>" && rm /home/$login_name/.face* && echo $login_name
fi
yad --fixed --title="$title" --window-icon="config-users" --image="config-users" --borders=20 --text="$reply" --button="OK":0
