#!/bin/bash
##################################
#  Author: Stefano Capitani <www.manjaro.org> <stefanoatmanjarodotorg>
#  Original authors:
#
#  www.biglinux.com.br
#  By Bruno Gonçalves
#  03/01/2020
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
##################################

#Translation
export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=manjaro-rescue


DISK_SELECTED="$(cat /tmp/grub-disks-selected)"
DISK_NAME="$(grep "${DISK_SELECTED}" /tmp/grub-disks | cut -f3 -d" ")"
DISK_TABLE="$(grep "${DISK_SELECTED}" /tmp/grub-disks | cut -f4 -d" ")"
DISK_SIZE="$(grep "${DISK_SELECTED}" /tmp/grub-disks | cut -f2 -d" ")"
SELECTED_PARTITION="$(awk -F: '{ print $1}' /tmp/os-prober-selected)"
SELECTED_OS="$(awk -F: '{ print $2 " - " $3 }' /tmp/os-prober-selected)"
PARTITION_FORMAT="$(awk -F: '{ print $5 }' /tmp/os-prober-selected)"
UUID_PARTITION="$(awk -F: '{ print $6 }' /tmp/os-prober-selected | sed 's|UUID=||g')"

umount -l $SELECTED_PARTITION 2> /dev/null
umount -l /mnt 2> /dev/null

if [ "$PARTITION_FORMAT" = "btrfs" ];
then
    mount $SELECTED_PARTITION -o subvol=@ /mnt
else
    mount $SELECTED_PARTITION /mnt
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "1" ];
then
    grub-install --root-directory=/mnt /dev/${DISK_SELECTED}
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "2" ];
then
    manjaro-chroot /mnt rm /var/lib/pacman/db.lck
    manjaro-chroot /mnt pacman --noconfirm -Sy grub
    manjaro-chroot /mnt grub-install --force --target=i386-pc --recheck --boot-directory=/boot /dev/${DISK_SELECTED}
    manjaro-chroot /mnt mkinitcpio -P
    manjaro-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
fi


if [ "$(cat /tmp/grub-restore-apply-mode)" = "3" ];
then
    manjaro-chroot /mnt rm /var/lib/pacman/db.lck
    manjaro-chroot /mnt pamac update --force-refresh --no-confirm
    manjaro-chroot /mnt pamac install linux grub --no-confirm
    manjaro-chroot /mnt pacman --noconfirm -Syyuu grub
    manjaro-chroot /mnt pacman --noconfirm -Syyuu linux
    manjaro-chroot /mnt grub-install --force --target=i386-pc --recheck --boot-directory=/boot /dev/${DISK_SELECTED}
    manjaro-chroot /mnt mkinitcpio -P
    manjaro-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "4" ];
then
    manjaro-chroot /mnt bash
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "5" ];
then
    manjaro-chroot /mnt bmenu
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "6" ];
then
    manjaro-chroot /mnt mlh
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "7" ];
then
    manjaro-chroot /mnt rm /var/lib/pacman/db.lck >/dev/null 2>&1
    echo 'Running ...'
    manjaro-chroot /mnt pamac-manager >/dev/null 2>&1
    clear
    echo 'Exiting ...'
fi

if [ "$(cat /tmp/grub-restore-apply-mode)" = "8" ];
then
    if [ $(command -v timeshift-gtk) ];
        then manjaro-chroot /mnt timeshift-gtk >/dev/null 2>&1
    else echo 'Exiting ...'
        zenity --warning --text $"Timeshift not installed!"  --window-icon=/usr/share/manjaro/grub-restore/grub-icon.svg 
    fi
fi

echo "sync disk"
sleep 1
echo "umount /mnt/boot"
umount /mnt/boot 2>/dev/null
sleep 1
echo "umount /mnt"
umount /mnt 2>/dev/null
sleep 1
