#!/bin/bash
###################################################################
#  REMASTERSYS                                                    #
#    Under the GNU GPL2 License                                   #
#    Copyright (C) 2013-2022 Daniel "Nerun" Rodrigues             #
#    Copyright (C) 2007-2012 Tony "Fragadelic" Brijeski           #
#    Full copyright notice: /usr/share/doc/remastersys/copyright  #
###################################################################

# The remastersys-grub-restore was completely rewritten in September 2022.

# SYSTEM LOCALIZATION
# Target to file:
# /usr/share/locale/(language code)/LC_MESSAGES/remastersys-grub-restore.mo
TEXTDOMAIN=remastersys-grub-restore

Pick=$"Options"
Size=$"Size"
Info=$"Info"
Yes=$"Yes"
No=$"No"
Cancel=$"Cancel"
Continue=$"\n\nDo you want to continue?"

DIALOG_BASE="$(which yad) --window-icon=/usr/share/icons/hicolor/16x16/apps/remastersys.png --center"
DIALOGMENU="$DIALOG_BASE --width=500 --height=370"
DIALOG="$DIALOG_BASE --fixed"
TITLE="--always-print-result --dialog-sep --image=/usr/share/icons/hicolor/48x48/apps/remastersys.png --title="
TEXT="--text="
ENTRY="--entry "
ENTRYTEXT="--entry-text "
MENU="--list --column=$Pick --column=$Size --column=$Info"
YESNO=" --button=$Yes:0 --button=$No:1 "
MSGBOX=" --button=Ok:0 "
CANCELOK=" --button=$Cancel:1 --button=Ok:0 "
TITLETEXT=$"Remastersys Grub Restore"

QuitNow(){
    if [ $? != 0 ]; then
        $DIALOG_BASE --width=310 $TITLE"$TITLETEXT" $MSGBOX $TEXT$"Quiting now."
        exit 0
    fi
}

# check if running with root privileges
if [ $(whoami) != "root" ]; then
    $DIALOG $TITLE"$TITLETEXT" $MSGBOX $TEXT$"ERROR: This script must run as superuser. Try with \"sudo\"."
    exit 1
fi

# inform them what this is and ask if they want to continue
$DIALOG_BASE --width=310 $TITLE"$TITLETEXT" $YESNO $TEXT$"This is a GRUB restore utility.$Continue"

if [ $? != 0 ]; then
    exit 0
fi

# START ########################################################################

MyRoot=$(mount | grep " / " | cut -d' ' -f1)

if [ "$MyRoot" != "overlay" ]; then
    # Running from HD
    partition_info=$"The_partition_this_install_is_on"
    partitions_list="$MyRoot $(sudo fdisk -l | grep "$MyRoot" | sed 's/\*//' | tr -s " " | cut -d' ' -f5) $partition_info"
else
    # Running from a LiveCD
    # list mounted partitions
    fdisk -l | grep "^/dev" | sed 's/\*//' | tr -s " " | cut -d' ' -f1,5- > tmp.sudo_fdisk

    while read line; do
        PART=$(echo $line | cut -d' ' -f1)
        SIZE=$(echo $line | cut -d' ' -f2)
        INFO=$(echo $line | cut -d' ' -f3- | sed 's/ /_/g')
        partitions_list="$partitions_list $PART $SIZE $INFO"
    done < tmp.sudo_fdisk
    
    rm tmp.sudo_fdisk
fi

# SELECT PARTITION #############################################################

SelectPartition=$"Please select a partition to install or restore grub to.\n\nIf you see no option then no valid partitions were found. If that's the case, first try to mount the partition you want to install or restore GRUB to and then come back here."

TARGETPART=$($DIALOGMENU $TITLE"$TITLETEXT" $CANCELOK $MENU $TEXT"$SelectPartition" $partitions_list)
QuitNow

# equal to: /dev/sda1 or /dev/nvme0n1p5
PartitionRoot=$(echo $TARGETPART | cut -d"|" -f1)

# equal to: /dev/sda or /dev/nvme0n1
PartitionMBR=$(echo $TARGETPART | cut -d"|" -f1 | sed -e 's/p[1-9]$\|[1-9]$//g')

# SELECT ROOT OR ROOTMBR #######################################################

WhereGrub=$"Where do you want to install GRUB to?"
root=$"root partition ($PartitionRoot)"
rootmbr=$"Master Boot Record (MBR) of $PartitionMBR"

# root|root partition (/dev/sda1)|
# rootmbr|Master Boot Record (MBR) of /dev/sda|
WHEREGRUB=$($DIALOG_BASE --width=400 $TITLE"$TITLETEXT" $CANCELOK --list --column=$Pick --column=$Info "rootmbr" "$rootmbr" "root" "$root" $TEXT"$WhereGrub")
QuitNow

# root
# rootmbr
WHEREGRUBa=$(echo $WHEREGRUB | cut -d"|" -f1)
# root partition (/dev/sda1)
# Master Boot Record (MBR) of /dev/sda
WHEREGRUBb=$(echo $WHEREGRUB | cut -d"|" -f2)

# ADVISE - PROCEED TO INSTALL OR CANCEL ########################################

proceed=$"GRUB will be restored to:\n\n$WHEREGRUBb$Continue"

$DIALOG_BASE --width=400 $TITLE"$TITLETEXT" $YESNO $TEXT"$proceed"
QuitNow

if [ "$PartitionRoot" = "$MyRoot" ]; then
    PartitionRoot="/"
fi

xterm -fn 9x15 -e "sudo remastersys-grubconfig $PartitionRoot $WHEREGRUBa"

