#!/bin/bash
# Rescapp Fsck 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:According to the wikipedia: The globally unique identifier (GUID) for the EFI system partition in the GUID Partition Table (GPT) scheme is C12A7328-F81F-11D2-BA4B-00A0C93EC93B, while its ID in the master boot record (MBR) partition-table scheme is 0xEF. I wanted to search for both of them but I don't know how to use pyparted to tell me partition types. So I am asumming that getting the esp flag is the same thing as having either the C12A7328-F81F-11D2-BA4B-00A0C93EC93B guid type on GPT partition table or the 0xEF type in msdos partition table.

# fsck Main program

UEFIPARTCHECK_OK_STR="The EFI partition status was OK (Every test passed successfully.) ! :)"
UEFIPARTCHECK_KO_STR="This might not be a valid EFI partition. At least one test failed. :("
UEFIPARTCHECK_RUNNING_STR="Performing EFI partition status checks"

UEFIPARTCHECK_EFI_SYSTEM_DETECTED_OK_STR="The EFI partition, has been detected, as an EFI partition by fdisk :)"
UEFIPARTCHECK_EFI_SYSTEM_DETECTED_KO_STR="The EFI partition, has NOT been detected, as an EFI partition by fdisk. :("
UEFIPARTCHECK_EFI_SYSTEM_DETECTED_RUNNING_STR="Performing EFI partition detection by fdisk"

UEFIPARTCHECK_ESP_FLAG_OK_STR="The EFI partition, has a esp flag :)"
UEFIPARTCHECK_ESP_FLAG_KO_STR="The EFI partition, does not have a esp flag. :("
UEFIPARTCHECK_ESP_FLAG_RUNNING_STR="Performing EFI partition esp flag check"

UEFIPARTCHECK_BOOT_FLAG_OK_STR="The EFI partition, has a boot flag :)"
UEFIPARTCHECK_BOOT_FLAG_KO_STR="The EFI partition, does not have a boot flag. :("
UEFIPARTCHECK_BOOT_FLAG_RUNNING_STR="Performing EFI partition boot flag check"

UEFIPARTCHECK_UEFI_FILESYSTEM_OK_STR="The EFI partition, has a valid uefi filesystem :)"
UEFIPARTCHECK_UEFI_FILESYSTEM_KO_STR="The EFI partition, does not have a valid uefi filesystem. :("
UEFIPARTCHECK_UEFI_FILESYSTEM_RUNNING_STR="Performing EFI partition valid uefi filesystem check"

UEFIPARTCHECK_PARTITION_MOUNT_OK_STR="The EFI partition, can be mounted :)"
UEFIPARTCHECK_PARTITION_MOUNT_KO_STR="The EFI partition, cannot be mounted. :("
UEFIPARTCHECK_PARTITION_MOUNT_RUNNING_STR="Performing EFI partition mount check"

UEFIPARTCHECK_DISK_TYPE_IS_GPT_OK_STR="The EFI partition, disk type is either gpt or msdos :)"
UEFIPARTCHECK_DISK_TYPE_IS_GPT_KO_STR="The EFI partition, disk type is neither gpt nor msdos. :("
UEFIPARTCHECK_DISK_TYPE_IS_GPT_RUNNING_STR="Performing EFI partition disk type check"

UEFIPARTCHECK_PARTITIONS_NOT_FOUND="No EFI partitions were found or chosen."

SELECTED_PARTITION=$(rtux_Choose_Partition "Which EFI partition?")




if [ "x${SELECTED_PARTITION}" != "x" ] ; then

  EXIT_SUM=0


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_EFI_SYSTEM_DETECTED_RUNNING_STR}" rtux_UEFI_Check_Is_EFI_System_Partition ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_EFI_SYSTEM_DETECTED_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_EFI_SYSTEM_DETECTED_KO_STR}
    false
  fi

  UEFIPARTCHECK_EFI_SYSTEM_DETECTED_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_EFI_SYSTEM_DETECTED_EXIT_VALUE))


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_ESP_FLAG_RUNNING_STR}" rtux_UEFI_Part_Check_esp_Flag ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_ESP_FLAG_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_ESP_FLAG_KO_STR}
    false
  fi

  UEFIPARTCHECK_ESP_FLAG_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_ESP_FLAG_EXIT_VALUE))


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_BOOT_FLAG_RUNNING_STR}" rtux_UEFI_Part_Check_boot_Flag ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_BOOT_FLAG_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_BOOT_FLAG_KO_STR}
    false
  fi

  UEFIPARTCHECK_BOOT_FLAG_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_BOOT_FLAG_EXIT_VALUE))


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_UEFI_FILESYSTEM_RUNNING_STR}" rtux_UEFI_Part_Check_uefi_filesystem ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_UEFI_FILESYSTEM_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_UEFI_FILESYSTEM_KO_STR}
    false
  fi

  UEFIPARTCHECK_UEFI_FILESYSTEM_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_UEFI_FILESYSTEM_EXIT_VALUE))


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_PARTITION_MOUNT_RUNNING_STR}" rtux_Partition_Can_Be_Mount ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_PARTITION_MOUNT_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_PARTITION_MOUNT_KO_STR}
    false
  fi

  UEFIPARTCHECK_PARTITION_MOUNT_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_PARTITION_MOUNT_EXIT_VALUE))


  if ( rtux_Run_Show_Progress "${UEFIPARTCHECK_DISK_TYPE_IS_GPT_RUNNING_STR}" rtux_UEFI_Part_Check_disk_type_is_gpt_or_msdos ${SELECTED_PARTITION} ) ; then
    rtux_Message_Success ${UEFIPARTCHECK_DISK_TYPE_IS_GPT_OK_STR}
  else
    rtux_Message_Failure ${UEFIPARTCHECK_DISK_TYPE_IS_GPT_KO_STR}
    false
  fi

  UEFIPARTCHECK_DISK_TYPE_IS_GPT_EXIT_VALUE=$?
  EXIT_SUM=$((EXIT_SUM+UEFIPARTCHECK_DISK_TYPE_IS_GPT_EXIT_VALUE))

else
  rtux_Message_Warning ${UEFIPARTCHECK_PARTITIONS_NOT_FOUND}
  EXIT_SUM=1
  false
fi


if [ ${EXIT_SUM} -eq 0 ] ; then
  rtux_Message_Success ${UEFIPARTCHECK_OK_STR}
else 
  rtux_Message_Failure ${UEFIPARTCHECK_KO_STR}
fi
