#!/bin/bash

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


[ "$1" = "info" ] && Info && exit 0
param="$@"
[ -n "$param" ] && BACKUP_DEVICE="$param"  # or define BACKUP_DEVICE in $HOME/.config/openbox/environment
title=$"Starbuntu Backup"
text=$'\n'"    "$"Please launch 'backup' again with syntax&#10;    <i>'backup &lt;Backup device or Label of backup device&gt;'</i>"$'\n'
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 "$part" ]; do
    BACKUP_DEVICE="${BACKUP_DEVICE/\/dev\/}"
    part="$(lsblk -o KNAME,LABEL | grep $BACKUP_DEVICE | cut -d\  -f1)"
    if [ -z "$part" ]; 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?""&#10;"
    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 --no-error-msg "$0 $BACKUP_DEVICE" & exit 0 || exit 1
fi

part="/dev/$part"
xset s off

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 $part
mp="$(udisksctl mount -b $part | cut -d\  -f4)"
rest_size=$(df $mp | tail -1 | awk '{print $4}')
act_dev="$(lsblk -o KNAME,LABEL,MOUNTPOINT | grep '/$' | cut -d\  -f1)"
back="$(lsblk -o KNAME,LABEL,MOUNTPOINT | grep $act_dev | cut -d\  -f2)"
[ ! -d "$mp/Starbuntu-Backup/$back" ] && [ $rest_size -lt 150000000 ] && entry=$"Not enough disk space on $part!" && yad --fixed --title="$title" --window-icon="$icon" --image="dialog-warning" --borders=10 --text="$entry" --button="OK":0 && exit 1
if [ -d "$mp/Starbuntu-Backup/$back" ]; then
    [ $rest_size -lt 75000000 ] && entry=$"Not enough disk space on $part!" && yad --fixed --title="$title" --window-icon="$icon" --image="dialog-warning" --borders=10 --text="$entry" --button="OK":0 && exit 1
    rsync -aX $mp/Starbuntu-Backup/$back/ $mp/Starbuntu-Backup/$back.old/
else
    mkdir -p "$mp/Starbuntu-Backup/$back"
fi
rsync -aX --delete --inplace / --exclude 'dev/*' --exclude 'media/*' --exclude 'mnt/*' --exclude 'proc/*' --exclude 'run/*' --exclude 'sys/*' --exclude 'tmp/*' $mp/Starbuntu-Backup/$back
udisksctl unmount -b $part
xset s on

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