#!/bin/bash

version="0.1.0"
reldate="2026/09/17"

scriptname=$(basename ${0})

if (( EUID != 0 )); then
    echo "${scriptname} must be run as root"
    exit 1
fi

refind_base=$(find /boot -type d -name 'refind')
if [ ! -d "${refind_base}" ]; then
    echo "no folder 'refind' found anywhere in /boot"
    exit 1
fi

refind_apt_conf=/usr/share/nanolx/apt.d/99-keep-refind
refindconf=${refind_base}/refind.conf

_default_bootmanager () {
    efibootmgr | awk '/BootOrder:/ {split($2, order, ","); target="^Boot"order[1]} target && $0 ~ target {sub(/^Boot[0-9A-Fa-f]+\*?[[:space:]]*/, ""); split($0, name, "\t"); print name[1]}'
}

case ${1} in
    list-themes )
        ls -1 -I "*.old" "${refind_base}/themes/"
    ;;

    set-theme )
        if [ -d "${refind_base}/themes/${2}" ]; then
            echo "setting default refind theme: ${2}"
            sed -e '/include.*themes/d' -i "${refindconf}"
            echo "include themes/${2}/theme.conf" >> "${refindconf}"
        else
            echo "skip: theme not found: ${2}"
            exit 1
        fi
    ;;

    get-theme )
        echo "currently active refind theme:"
        refind_theme="$(gawk -F'/' '/include.*themes/{print $2}' "${refindconf}")"
        [ -z "${refind_theme}" ] && echo "default (none)" || echo "${refind_theme}"
    ;;

    default-theme )
        echo "reverting to default refind theme"
        sed -e '/include.*themes/d' -i "${refindconf}"
    ;;

    enable-keep-refind )
        if [ -f /etc/apt/apt.conf.d/99-keep-refind ]; then
            echo "Refind protection configuration already enabled"
        else
            cp ${refind_apt_conf} /etc/apt/apt.conf.d/
            echo "installed /etc/apt/apt.conf.d/99-keep-refind"
        fi
    ;;

    disable-keep-refind )
        if [ -f /etc/apt/apt.conf.d/99-keep-refind ]; then
            rm /etc/apt/apt.conf.d/99-keep-refind
            echo "removed /etc/apt/apt.conf.d/99-keep-refind"
        else
            echo "Refind protection configuration not enabled"
        fi
    ;;

    liquorix-default )
        if grep -q "^default_selection \"liquorix\"" ${refindconf}; then
            echo "liquorix already set as prefered kernel"
        else
            echo "set latest liquorix kernel as default boot choice"
            echo "fold_linux_kernels false" >> ${refindconf}
            echo "default_selection \"liquorix\"" >> ${refindconf}
        fi
    ;;

    debian-default )
        if grep -q "^default_selection \"liquorix\"" ${refindconf}; then
            echo "set latest Debian kernel as default boot choice"
            sed "/^fold_linux_kernels false/d" -i ${refindconf}
            sed "/^default_selection \"liquorix\"/d" -i ${refindconf}
        else
            echo "Debian already set as prefered kernel"
        fi
    ;;

    status )
        echo "${scriptname} status report"
        echo "default bootmanager               $(_default_bootmanager)"
        echo "current refind theme              $(gawk -F'/' '/include.*themes/{print $2}' "${refindconf}")"
        if [ -f /etc/apt/apt.conf.d/99-keep-refind ]; then
            echo "keep refind after grub update     enabled"
        else
            echo "keep refind after grub update     disabled"
        fi
        if grep -q "^default_selection \"liquorix\"" ${refindconf}; then
            echo "prefered linux kernel             liquorix"
        else
            echo "prefered linux kernel             debian"
        fi
    ;;

    version)
        echo "${scriptname} ${version} (${reldate})"
    ;;

    * )
        echo -e "${scriptname} v${version} (${reldate})
© 2026 Christopher Roy Bratusek <nano@jpberlin.de>

licensed under the GNU General Public License v3 (or newer)

usage:
    ${scriptname} list-themes           list installed themes
    ${scriptname} set-theme     [theme] set theme to use
    ${scriptname} get-theme             get active theme
    ${scriptname} default-theme         revert to default theme
    ${scriptname} enable-keep-refind    keep refind, when updating grub/refind/shim
    ${scriptname} disable-keep-refind   disable keeping refind as default bootloader
    ${scriptname} liquorix-default      set latest liquorix kernel as prefered boot choice
    ${scriptname} debian-default        set latest upstream kernel as prefered boot choice
    ${scriptname} status                print some information
    ${scriptname} version               print version
"
    ;;
esac
