#!/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

# Search partitions btrfs ext xfs and vfat
for i in $(blkid | grep -e 'TYPE="btrfs"' -e 'TYPE="ext."' -e 'TYPE="xfs"' -e 'TYPE="vfat"' | cut -f1 -d:)
do
    # Print the partition that is being unmounted
    echo "umounting $i"
    # Unmount the partition and redirect any errors to /dev/null
    umount -l "$i" 2> /dev/null
done

# Save the list of Linux partitions to /tmp/os-prober,excluding partitions labeled "timeshift" and "subvol=@/"
os-prober | grep :linux | grep -v timeshift | grep -v subvol=@/ > /tmp/os-prober  

# add other partitions
for i in $(blkid | grep -e 'TYPE="btrfs"' | cut -f1 -d:)
do
    # Check if the partition is already in the /tmp/os-prober
    if [ "$(grep "$i" /tmp/os-prober)" = "" ];
    then
        # Add any "btrfs" partitions that were not detected by os-prober to /tmp/os-prober
        echo "$i:Not detected name:::btrfs::" >> /tmp/os-prober 
    fi
done

# EFI
# Remove file if exists and create the directory if it doesn't already exist
rm -f /tmp/efi-partitions
mkdir -p /boot/efi

# Save the device name of the live boot
DEVICE_LIVE_BOOT_TMP="$(readlink -f "$(df -h /run/miso/bootmnt/ | awk '{ print $1 }' | grep '/dev/')" | sed 's|/dev/||'g)"
if [ "$(echo "$DEVICE_LIVE_BOOT_TMP" | grep dm)" != "" ];
then
    DEVICE_LIVE_BOOT="$(ls /sys/block/${DEVICE_LIVE_BOOT_TMP}/slaves | sed 's|[0-9]||g')"
else
    DEVICE_LIVE_BOOT="$DEVICE_LIVE_BOOT_TMP"
fi

# Mount and check if there is a directory /boot/efi/EFI
for i in $(LANG=en fdisk -l | grep -e "EFI System" -e "FAT" | cut -f1 -d" " | grep -v "$DEVICE_LIVE_BOOT")
do
    umount -l /boot/efi
    mount "$i" /boot/efi
    if [ -d /boot/efi/EFI ];
    then
        # Save the partition to /tmp/efi-partitions
        echo "$i" >> /tmp/efi-partitions
    fi
    umount -l /boot/efi
done

rm -f /tmp/grub-disks

# Get disk information like model and label type
for i in $(LANG=C lsblk | grep disk | grep -v zram | awk '{ print $1 ":" $4}' | grep -v "$DEVICE_LIVE_BOOT")
do
    DISKNAME="$(LANG=en fdisk -l /dev/${i%:*} | grep -e "Disk model:" | sed 's|Disk model: ||g' | sed -e 's/  */_/g;s|_$||g')"
    DISKLABELTYPE="$(LANG=en fdisk -l /dev/${i%:*} | grep -e "Disklabel type:" | sed 's|Disklabel type: ||g' | sed -e 's/  *//g;s| ||g')"

    # Save the disk information to /tmp/grub-disks
    echo "${i%:*} ${i#*:} ${DISKNAME} ${DISKLABELTYPE}" >> /tmp/grub-disks
    
done