#!/bin/bash

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

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

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

title=$"Date & Time"
tz=$"Time Zone"
dt=$"Date"
tm=$"Time"
text_sel=$"&#10;<b>Please change:</b>"
title_tz=$"Time Zones"
title_dt=$"Date"
title_tm=$"Time"
text_tz=$"For a quicker selection just&#10;type your state capital&#10;or a big city nearby:&#10;"
text_dt=$"&#10;<b>Please type new date yyyy-mm-dd:</b>&#10;"
text_tm=$"&#10;<b>Please type new time hh:mm:ss:</b>&#10;"
cancel=$"Cancel"
hint=$"&#9;<b>In order to have the change resetproof&#10;&#9;open a terminal and execute the command&#10;&#9;'sudo hwclock -w'!</b>&#10;"
alarm=$"&#10;&#9;<b>Oops! Something went wrong!</b>&#10;"

date_select=`yad --center --title="$title" --window-icon=$HOME/.icons/clock.png --height=200 --width=300 --text="$text_sel" --text-align="center" --list --radiolist --expand-column=0 --column='' --column='' true "$tz" false "$dt" false "$tm" --button="$cancel":1 --button="Info":"$0 info" --button="OK":0`

button="$?"
[ $button -gt 0 ] && exit 0 || success=false

case "$date_select" in

    *"$tz"* )  IFS=$'\n'
		tz=($(timedatectl list-timezones))
		n=${#tz[@]}
		h=$((140+20*$n))
		[ $h -gt 800 ] && h=800
		time_zone=$(yad --center --title="$title_tz" --height=$h --borders=20 --window-icon=$HOME/.icons/clock.png --text="$text_tz" --list --search-column=1 --regex-search --no-headers --column="$title" ${tz[@]} --button="$cancel":1 --button="Info":"$0 info" --button="OK":0)
		button=$?
		if [ -n "$time_zone" ]; then
		    time_zone=${time_zone::-1}
		    timedatectl set-timezone "$time_zone"
		    [ $? -eq 0 ] && success=true
		fi ;;

    *"$dt"* )  date=`yad --center --title="$title_dt" --width=350 --borders=10 --window-icon=$HOME/.icons/clock.png --text="$text_dt" --entry --button="$cancel":1 --button="Info":"$0 info" --button="OK":0`
		button=$?
		if [ -n "$date" ]; then
		    at=$(date +%T)
		    date "+%Y-%m-%d %T" -s "$date $at"
		    [ $? -eq 0 ] && success=true
		fi ;;

    *"$tm"* )  time=`yad --center --title="$title_tm" --width=350 --borders=10 --window-icon=$HOME/.icons/clock.png --text="$text_tm" --entry --button="$cancel":1 --button="Info":"$0 info" --button="OK":0`
		button=$?
		if [ -n "$time" ]; then
		    timedatectl set-ntp 0
		    date "+%T" -s "$time"
		    [ $? -eq 0 ] && success=true
		fi ;;

esac

if [ $button -eq 0 ]; then
    if $success; then msg="$hint"; else msg="$alarm"; fi
    yad --center --title="$title" --fixed --window-icon=$HOME/.icons/clock.png --borders=20 --image="dialog-warning" --text="$msg" --button="OK"
fi

exit 0
