#!/bin/bash
##################################
#  Author1: Bruno Goncalves (www.biglinux.com.br) 
#  Author2: Barnabé di Kartola
#  Author3: Rafael Ruscher (rruscher@gmail.com)  
#  Date:    2023/10/28 
#  
#  Description: BigLinux Snapshot Detect and Restore 
#  
# Licensed by GPL V2 or greater
##################################

export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=grub-btrfs-timeshift

txtNotDetect=$"Snapshot não detectado, você realmente inicializa no snapshot?"
txtOk=$"Concluído!"
txtRestoration=$"Restauração"
txtYes=$"Se você realmente deseja restaurar o script de chamada com --yes."

if ! grep -q  'timeshift-btrfs/snapshots' /proc/cmdline ; then
    echo "$txtNotDetect"
    exit
fi

if [[ "$1" = "--yes" ]]; then
    killall timeshift-gtk
    # Detect snapshot date
    actualSnapshot="$(sed -E 's|.*timeshift-btrfs/snapshots/([^ @/]*).*|\1|' /proc/cmdline)"

    # Restore using Timeshift
    yes "" |  /usr/bin/timeshift  --restore --snapshot "$actualSnapshot" --scripted --yes
else
    echo "$txtYes"
fi
