#!/bin/bash
# Rescapp Grub-install run script
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Adrian Gibanel Lopez
#
# Rescapp 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, either version 3 of the License, or
# (at your option) any later version.
#
# Rescapp 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.
#
# You should have received a copy of the GNU General Public License
# along with Rescapp.  If not, see <http://www.gnu.org/licenses/>.

source ${RESCAPP_LIB_FILE}

set -x
set -v


# TODO: Program check runtime (Maybe to be stolen from bootinfoscript)

# MAIN PROGRAM

GRUB_INSTALLED_OK_STR="Grub was installed OK! :)"
GRUB_NOT_INSTALLED_STR="Grub was not installed. Something went wrong! :("
WHICH_HARD_DISK_INSTALL_GRUB_STR="Which hard drive do you wish to install Grub on?"
GRUB_INSTALLED_RUNNING_STR="Installing Grub."
GRUB_INSTALL_GNU_LINUX_PARTITIONS_NOT_FOUND="No GNU/Linux partitions were found or chosen."
GRUB_INSTALL_HARD_DISK_NOT_FOUND="No drives were found or chosen."
GRUB_INSTALL_HARD_DISKS_ORDER_NOT_FOUND="The hard drives order was not set properly."

SELECTED_PARTITION=$(rtux_Choose_Linux_partition);

if [ "x${SELECTED_PARTITION}" != "x" ] ; then
  SELECTED_HARD_DISK=$(rtux_Choose_Hard_Disk ${WHICH_HARD_DISK_INSTALL_GRUB_STR});
  if [ "x${SELECTED_HARD_DISK}" != "x" ] ; then
    SELECTED_HARD_DISKS_ORDER=$(rtux_Order_Hard_Disks)
    if [ "x${SELECTED_HARD_DISKS_ORDER}" != "x" ] ; then
      if rtux_Run_Show_Progress "${GRUB_INSTALLED_RUNNING_STR}" rtux_Grub_Install ${SELECTED_HARD_DISK} ${SELECTED_PARTITION} ${SELECTED_HARD_DISKS_ORDER} ; then
        rtux_Message_Success ${GRUB_INSTALLED_OK_STR}
      else
        rtux_Message_Failure ${GRUB_NOT_INSTALLED_STR}
      fi
    else
      rtux_Message_Warning ${GRUB_INSTALL_HARD_DISKS_ORDER_NOT_FOUND}
      rtux_Message_Failure ${GRUB_NOT_INSTALLED_STR}
    fi
  else
    rtux_Message_Warning ${GRUB_INSTALL_HARD_DISK_NOT_FOUND}
    rtux_Message_Failure ${GRUB_NOT_INSTALLED_STR}
  fi
else
  rtux_Message_Warning ${GRUB_INSTALL_GNU_LINUX_PARTITIONS_NOT_FOUND}
  rtux_Message_Failure ${GRUB_NOT_INSTALLED_STR}
fi


