#!/bin/bash
# Rescapp Winmbr 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

# Install Windows MBR so that it boots chosen Windows partition
# From the chosen hard drive
# 1 parametre = Selected hard drive
# 2 parametre = Selected partition
# 3 parametre = Hard Drive position
# While it is being run the user is shown a list of the hard drives
# and is asked to order them
function rtux_Winmbr_Install () {

  local EXIT_VALUE=1 # Error by default
  local SELECTED_HARD_DISK="$1"
  local SELECTED_PARTITION="$2"
  local HARD_DISK_POSITION="$3"

  local BIOS_HARD_DISK=0

  local SELECTED_PARTITION_NUMBER="$(echo ${SELECTED_PARTITION} | sed 's/[A-Za-z]*//g')"
  let BIOS_HARD_DISK=${HARD_DISK_POSITION}+80-1
  if [[ ${BIOS_HARD_DISK} -eq 80 ]] ; then
    local OTHER_HARD_DISK_PARAMETRE=""
  else
    local OTHER_HARD_DISK_PARAMETRE="-d 0x${BIOS_HARD_DISK}"
  fi
  # Activate partition
  lilo -A /dev/${SELECTED_HARD_DISK} ${SELECTED_PARTITION_NUMBER}
  # Install MBR
  lilo -M /dev/${SELECTED_HARD_DISK} mbr

  EXIT_VALUE=$?
  return ${EXIT_VALUE}

} # function rtux_Grub_Install ()

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

# MAIN PROGRAM


WINMBR_INSTALLED_OK_STR="The Windows mbr was installed OK! :)"
WINMBR_NOT_INSTALLED_STR="The Windows mbr was not installed. Something went wrong! :("
WHICH_HARD_DISK_INSTALL_WINMBR_STR="Which hard drive, do you wish to install a Windows mbr on?"
WINMBR_HARDDISK_POSITION_NOT_FOUND="Hard disk position was not set."
WINMBR_HARDDISK_NOT_FOUND="Hard disk was not set or found."
WINMBR_PARTITION_NOT_FOUND="Partition was not set or found."

SELECTED_PARTITION=$(rtux_Choose_Primary_Partition "Which Windows partition?");
if [ "x${SELECTED_PARTITION}" != "x" ] ; then
  SELECTED_HARD_DISK=$(rtux_Choose_Hard_Disk "${WHICH_HARD_DISK_INSTALL_WINMBR_STR}");
  if [ "x${SELECTED_HARD_DISK}" != "x" ] ; then
    SELECTED_PARTITION_HARD_DISK=$(echo "${SELECTED_PARTITION}" | sed 's/[0-9]*//g' )

    HARD_DISK_POSITION=$(rtux_Choose_Hard_Disk_Position ${SELECTED_PARTITION_HARD_DISK})

    if [ "x${HARD_DISK_POSITION}" != "x" ] ; then
      if rtux_Winmbr_Install ${SELECTED_HARD_DISK} ${SELECTED_PARTITION} ${HARD_DISK_POSITION}; then
        rtux_Message_Success ${WINMBR_INSTALLED_OK_STR}
      else
        rtux_Message_Failure ${WINMBR_NOT_INSTALLED_STR}
      fi
    else
      rtux_Message_Warning ${WINMBR_HARDDISK_POSITION_NOT_FOUND}
      rtux_Message_Failure ${WINMBR_NOT_INSTALLED_STR}
    fi
  else
    rtux_Message_Warning ${WINMBR_HARDDISK_NOT_FOUND}
    rtux_Message_Failure ${WINMBR_NOT_INSTALLED_STR}
  fi
else
  rtux_Message_Warning ${WINMBR_PARTITION_NOT_FOUND}
  rtux_Message_Failure ${WINMBR_NOT_INSTALLED_STR}
fi
