#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
#el_make_environment
#. gettext.sh
#TEXTDOMAIN=""
#export TEXTDOMAIN

main(){
    if [[ "$UID" != 0 ]] ; then
        el_error "root please"
        exit 1
    fi
    if [[ ! -s /var/lib/dpkg/info/refind.list ]] || [[ ! -x "$(which refind-install || true )" ]] ; then
        el_error "refind must be installed first, aborting..."
    fi

    # vars
    if [[ -e /tmp/.secureboot-enabled ]] || mokutil --sb-state 2>&1 | grep -qsi "SecureBoot Enabled" ; then
        refind_boot_file="shimx64.efi"
    else
        refind_boot_file="grubx64.efi"
    fi

    EL_DEBUG=0 el_info 'Configuring Refind'
    # install a new refind conf
    rm -rf "/boot/efi/EFI/refind"

    # install it again
    echo -e "refind\trefind/install_to_esp\tboolean\ttrue" | debconf-set-selections
    dpkg-reconfigure -fnoninteractive -pcritical --no-reload refind

    if [[ -s "/boot/efi/EFI/refind/refind.conf" ]] ; then
        # do not show /boot entries, just their correctly installed grubs instead
        sed -i -e 's|^#scan_all_linux_kernels.*$|scan_all_linux_kernels false|g' "/boot/efi/EFI/refind/refind.conf"

        if [[ -d "/usr/share/refind-theme-regular" ]] ; then
            rm -rf "/boot/efi/EFI/refind/themes/refind-theme-regular"
            mkdir -p "/boot/efi/EFI/refind/themes"
            cp -a "/usr/share/refind-theme-regular" "/boot/efi/EFI/refind/themes/"
            #cp -a "/usr/share/refind-theme-regular/icons/128-48/os_elive.png" "/boot/efi/EFI/refind/themes/"
            # replace default elive entry
            if ! grep -qs "menuentry Elive" /boot/efi/EFI/refind/refind.conf ; then
                echo -e "\nmenuentry Elive {\n    loader /EFI/elive/${refind_boot_file}\n    icon /EFI/refind/themes/refind-theme-regular/icons/128-48/os_elive.png\n}\n" >> /boot/efi/EFI/refind/refind.conf
            fi
            # enable theme
            echo -e "\ninclude /EFI/refind/themes/refind-theme-regular/theme.conf\n" >> /boot/efi/EFI/refind/refind.conf
        else
            if ! grep -qs "menuentry Elive" /boot/efi/EFI/refind/refind.conf ; then
                echo -e "\nmenuentry Elive {\n    loader /EFI/elive/${refind_boot_file}\n}\n" >> /boot/efi/EFI/refind/refind.conf
            fi
        fi
        # improve menu listings:
        sed -i -e '/^dont_scan_dirs /d' /boot/efi/EFI/refind/refind.conf
        sed -i -e '/^dont_scan_files /d' /boot/efi/EFI/refind/refind.conf
        sed -i -e '/^dont_scan_volumes /d' /boot/efi/EFI/refind/refind.conf
        sed -i -e 's|^#dont_scan_dirs .*$|&1\ndont_scan_dirs +,EFI/elive,EFI/OC|' /boot/efi/EFI/refind/refind.conf
        sed -i -e 's|^#dont_scan_files .*$|&1\ndont_scan_files +,pcmemtestx64.efi,memtest.*efi,memtest+{arch}.efi,grub{arch}_real.efi,/EFI/BOOT/grubx64_real.efi,/EFI/BOOT/BOOTX64.EFI|' /boot/efi/EFI/refind/refind.conf
        #sed -i -e 's|^#dont_scan_files .*$|&1\ndont_scan_files +,pcmemtestx64.efi,System/Library/CoreServices/boot.efi|' /boot/efi/EFI/refind/refind.conf
        sed -i -e 's|^#dont_scan_volumes .*$|&1\ndont_scan_volumes "LRS_ESP","Recovery HD","Ventoy","Elive_OS","Elive_boot","Elive_home","Recovery"|' /boot/efi/EFI/refind/refind.conf

        # TODO: if is_ventoy, do not scan the dir BOOT also

        # there's a bug in refind that doesn't boot due to the "drivers_x64/ext4_x64.efi" file, so just copy the wanted files which also keeps all more clean
        rm -rf /tmp/refind 1>/dev/null 2>&1 || true
        mv -f /boot/efi/EFI/refind /tmp/
        dpkg-reconfigure -fnoninteractive -pcritical --no-reload refind
        cp -f /tmp/refind/refind.conf /boot/efi/EFI/refind/refind.conf
        if [[ -d "/tmp/refind/themes" ]] ; then
            cp -r /tmp/refind/themes /boot/efi/EFI/refind/
        fi
        if [[ -d "/usr/share/refind-theme-regular" ]] ; then
            mkdir -p /boot/efi/EFI/refind/themes
            rm -rf /boot/efi/EFI/refind/themes/refind-theme-regular 2>/dev/null || true
            cp -a /usr/share/refind-theme-regular /boot/efi/EFI/refind/themes/
        fi

        # fix bug that makes it unable to boot:
        rm -rf /boot/efi/EFI/refind/drivers*/ext4_*efi  2>/dev/null || true

    else
        el_error "file refind.conf is not created in '/boot/efi/EFI/refind/refind.conf':\n$(find /boot -type f -iname 'refind.conf') "
    fi

    # never reinstall refind on upgrades because seems like there's a bug which makes it unable to boot. Probably the ext4_x64.efi driver file is put back
    echo -e "refind\trefind/install_to_esp\tboolean\tfalse" | debconf-set-selections
}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
