#!/bin/env fish
begin
    set -l dir (status dirname)
    source "$dir/../../lib/software.fish"

    function installSW -V dir
        yayinst secureboot-grub
    end

    function configureSW -V dir
        source "$dir/../../../lib/settings.fish"
        set -l label (getOSConfig boot.label)
        set -l efiDir (getOSConfig boot.efiMountPoint)
        set -l bootNums (efibootmgr | sed "/$label/{ s/^.*Boot\([[:digit:]]\+\)\*.*\$/\1/; p; }; d")

        for bootNum in $bootNums
            sudo efibootmgr --delete-bootnum --bootnum $bootNum
        end

        sudo sed -i \
            -e "/esp=/{" \
                -e "a esp=$(echo "$efiDir" | string escape)" \
                -e d \
            -e "}" \
            -e "/bootloader_id=/{" \
                -e "a bootloader_id=$(echo "$label" | string escape)" \
                -e d \
            -e "}" \
            /etc/secureboot.conf

        sudo secure-grub-install
    end

    runInstaller --force $argv
end