#!/bin/bash

Info () {
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
}

wait () {
    local wait=$"&#10;&#9;Wait a moment ..."
    yad --center --width=350 --title="Update $db" --window-icon=kfm --image="dialog-warning" --borders=20 --text="$wait" --no-buttons --close-on-unfocus &
}


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

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

IFS=$'\n'
dbs=($"Bootloader" $"Locations" $"Man Pages" $"MIME Types" $"Splash Screen" $"Upgradable Packages")

ready=$"Done!"
title=$"Update database":
show_msg=true

i=${#dbs[@]}
h=$((104+24*$i))
cancel=$"Cancel"

db=$(yad --center --title="$title" --height=$h --borders=30 --list --no-headers --window-icon=kfm --column=DB ${dbs[@]} --button="$cancel":1 --button="Info":"$0 info" --button="OK":0)
if [ $? -gt 0 ]; then exit 1; fi

db="${db::-1}"

case "$db" in
    $"Bootloader"	   )  xterm -title $"Update Bootloader" -e restore-grub
			      show_msg=false
			      ;;
    $"Locations"	   )  wait
			      updatedb
			      ;;
    $"Man Pages"	   )  wait
			      mandb
			      ;;
    $"MIME Types"	   )  wait
			      update-mime-database /usr/share/mime
			      ;;
    $"Splash Screen"	   )  [ ! -d "/usr/share/plymouth/themes/starbuntu.bak" ] && cp -r /usr/share/plymouth/themes/starbuntu /usr/share/plymouth/themes/starbuntu.bak
			      ths=($(ls --group-directories-first /usr/share/plymouth/themes/ | head -n -3 | grep -v ^starbuntu$))
			      i=${#ths[@]}
			      h=$((150+24*$i))
			      th=$(yad --center --title="$db" --text=$"Select a theme":$'\n' --height=$h --borders=30 --list --no-headers --window-icon=kfm --column=DB ${ths[@]} --button="$cancel":1 --button="Info":"$0 info" --button="OK":0)
			      if [ $? -gt 0 ]; then exit 1; fi
			      wait
			      rm -r /usr/share/plymouth/themes/starbuntu
			      cp -r /usr/share/plymouth/themes/${th::-1} /usr/share/plymouth/themes/starbuntu
			      mv /usr/share/plymouth/themes/starbuntu/*.plymouth /usr/share/plymouth/themes/starbuntu/starbuntu.plymouth
			      mv /usr/share/plymouth/themes/starbuntu/*.script /usr/share/plymouth/themes/starbuntu/starbuntu.script
			      cp /usr/share/plymouth/starbuntu-logo.png /usr/share/plymouth/themes/starbuntu/logo.png
			      sed -i 's/^Name=.*/Name=Starbuntu theme/' /usr/share/plymouth/themes/starbuntu/starbuntu.plymouth
			      sed -i -E 's/^ImageDir=(.*)themes\/.*/ImageDir=\1themes\/starbuntu/' /usr/share/plymouth/themes/starbuntu/starbuntu.plymouth
			      sed -i -E 's~(ScriptFile=.*themes/).*(\.script)~\1starbuntu/starbuntu\2~' /usr/share/plymouth/themes/starbuntu/starbuntu.plymouth
			      sed -i -E 's~(logo.*= Image).*~\1("logo.png");~' /usr/share/plymouth/themes/starbuntu/starbuntu.script
			      . /lib/os-release
			      sed -i "s/^title=.*/title=$NAME $VERSION_ID/" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth
			      ln -sfn /usr/share/plymouth/themes/starbuntu/starbuntu.plymouth /etc/alternatives/default.plymouth
			      cp /usr/local/share/plymouth-Starbuntu/starbuntu-logo.png /usr/share/plymouth
			      ln -sfn /usr/share/plymouth/starbuntu-logo.png /usr/share/plymouth/ubuntu-logo.png
			      update-initramfs -c -k all > /dev/null
			      ;;
    $"Upgradable Packages" )  xterm -title $"Upgradable Packages" -e 'k=1; while pgrep -f "apt" | grep -v "$(pgrep -f pgrep)" > /dev/null; do sleep 1; echo $"Waiting for unblocking ...""     [ $k sec ]"; ((k++)); done
			      apt update
			      echo
			      echo $"Upgradable Packages":
			      echo
			      apt list --upgradable
			      echo
			      msg=$"[ End, please press Return! ]"
			      read -p "$msg"'
			      show_msg=false
			      ;;
esac

$show_msg && yad --center --title="$title" --width=350 --window-icon=kfm --borders=20 --text="<b>Update $db:&#10;$ready</b>" --text-align="center" --button="OK"

