#!/bin/bash

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RED='\e[1;31m'
YELLOW='\e[0;33m'
RESET_COLOR='\033[0m'

if [ "$EUID" -ne 0 ]; then
	echo -e $RED "Run this script as root or with the sudo command."$RESET_COLOR
	exit 1
fi

if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
	echo -e $RED "Running in chroot, ignore the snapshot creation request."$RESET_COLOR
	exit 1
fi

if ! command -v timeshift > /dev/null; then
	exit 1
fi

cat /proc/cmdline | grep kaisen-grub-btrfs > /dev/null

if [ $? -ne 0 ]; then
	echo -e $RED "You have not booted from a snapshot. Aborting."$RESET_COLOR
	exit 1
fi

SNAPSHOT=`awk -F 'subvol=timeshift-btrfs/snapshots/' '{ print $2 }' /proc/cmdline | cut -d '/' -f1`
DEVICE=`grep -v '#' /etc/fstab | grep -w / | grep -w subvol=@ | cut -d ' ' -f1 | cut -d '=' -f2`

echo -e $YELLOW "The snapshot $SNAPSHOT will be restored."$RESET_COLOR
timeshift --yes --scripted --btrfs --restore --snapshot "$SNAPSHOT" --snapshot-device "$DEVICE" --target "$DEVICE"
