#!/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
}

make_timeshift_json () {
    IFS=$'\n'
    for l in $(cat /etc/timeshift/default.json); do
	[[ $l =~ ]$ ]] && l="}" && echo -n "$l" >> $target && break
	[[ $l =~ "snapshot_" ]] && continue
	[[ $l =~ "backup_device_uuid" ]] && l="  \"backup_device_uuid\" : \"$uuid\","
	[[ $l =~ "do_first_run" ]] && l="  \"do_first_run\" : \"false\","
	[[ $l =~ "include_btrfs_home" ]] && l="  \"include_btrfs_home_for_backup\" : \"false\"," && echo "$l" >> $target && l="  \"include_btrfs_home_for_restore\" : \"false\"," # && echo "$l" >> $target
	[[ $l =~ "count_boot" ]] && echo "$l" >> $target && l="  \"date_format\" : \"%Y-%m-%d %H:%M:%S\"," # && echo "$l" >> $target
	[[ $l =~ "  ]," ]] && n=$(cat /etc/passwd | grep -E "0:0|100.:100." | cut -d: -f1 | wc -l) && k=1 && for u in $(cat /etc/passwd | grep -E "0:0|100.:100." | cut -d: -f1); do l="    \"+ /home/$u/**\","; l="${l/\/home\/root/\/root}"; [ $k -lt $n ] && echo "$l" >> $target || echo "${l::-1}" >> $target; ((k++)); done && l="  ],"
	[[ $l =~ "exclude-apps" ]] && l="  \"exclude-apps\" : []"
	echo "$l" >> $target
    done
    unset IFS
}

[ "$1" = "info" ] && Info && exit 0
param="$@"
[ -n "$param" ] && BACKUP_DEVICE="$param"  # or define BACKUP_DEVICE in $HOME/.config/openbox/environment
target="/etc/timeshift/timeshift.json"
title=$"Backup"
text=$'\n'"    "$"Please launch 'backup' again with syntax&#10;    <i>'backup &lt;Backup device or Label of backup device&gt;'</i>"
button=$"Help"
lang_dir=$"C"
icon=$HOME/.icons/mime-edit.png
if [ -z "$BACKUP_DEVICE" ]; then
    yad --center --borders=10 --image="dialog-warning" --window-icon=$icon --title="$title" --text="$text" --button=$button:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#backup" --button="Info":"$0 info" --button="OK":0
    exit 1
fi
while [ -z "$dev" ]; do
    BACKUP_DEVICE="${BACKUP_DEVICE/\/dev\/}"
    dev="$(lsblk -o KNAME,LABEL | grep $BACKUP_DEVICE | cut -d\  -f1)"
    uuid="$(lsblk -o KNAME,UUID | grep $dev | cut -d\  -f2)"
    uuid=$(echo $uuid)
    if [ -z "$dev" ]; then
	text=$'\n'"    "$"Please plugin $BACKUP_DEVICE!"$'\n'
	cancel=$"Cancel"
	yad --center --borders=10 --image="dialog-warning" --window-icon=$icon --title="$title" --text="$text" --button=$cancel:1 --button=$button:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#backup" --button="Info":"$0 info" --button="OK":0
	[ $? -gt 0 ] && exit 1
    fi
done

user=$(whoami)

if [ -n "$user" ] && [[ "$user" != "root" ]]; then
    text="    "$"To backup your system requires root privileges.&#10;&#10;    Proceed anyway?"
    b0=$"Yes"; b1=$"No"
    yad --center --borders=10 --image=info --window-icon=$icon --title="$title" --text="$text" --button="$b1":1 --button=$button:"yelp /usr/share/help/$lang_dir/Starbuntu/starbuntu-apps-details.page#backup" --button="Info":"$0 info" --button="$b0":0
    err=$?
    [ $err -eq 0 ] && gksu "$0 $BACKUP_DEVICE" & exit 0 || exit 1
fi

dev="/dev/$dev"
while true; do sleep 500; xdotool keydown Shift_L keyup Shift_L; done &

wait=$"&#10;&#9;Wait until backup reports:&#10;&#9;"\'$"Done!"\'$'\n'
yad --center --fixed --title="$title" --window-icon="$icon" --image="dialog-warning" --borders=20 --text="$wait" --no-buttons --close-on-unfocus &

udisksctl unmount -b $dev
mp="$(udisksctl mount -b $dev | cut -d\  -f4)"
act_dev="$(lsblk -o KNAME,LABEL,MOUNTPOINT | grep '/$' | cut -d\  -f1)"
back="$(lsblk -o KNAME,LABEL,MOUNTPOINT | grep $act_dev | cut -d\  -f2)"
rm -r $mp/timeshift/snapshots/$back.old
mv $target $target.bak
make_timeshift_json
timeshift --snapshot-device $dev --create
mv $mp/timeshift/snapshots/$back $mp/timeshift/snapshots/$back.old
mv $mp/timeshift/snapshots/2* $mp/timeshift/snapshots/$back
ln -sr $mp/timeshift/snapshots/$back $mp/timeshift/snapshots-ondemand
ln -sr $mp/timeshift/snapshots/$back.old $mp/timeshift/snapshots-ondemand
rm $mp/timeshift/snapshots-ondemand/2*
mv $target.bak $target
udisksctl unmount -b $dev

ready=$"Done!"
yad --center --title="$title" --width=350 --window-icon=$icon --borders=20 --text="<b>$ready</b>" --text-align="center" --button="OK"
