{{- if (eq .host.distro.family "linux") -}}
#!/usr/bin/env bash

{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}

### Fix Qubes issue
if command -v qubesctl > /dev/null && [ -f /boot/grub2/grubenv ] && [ -d /boot/efi/EFI/qubes ]; then
    sudo cp -f /boot/grub2/grubenv /boot/efi/EFI/qubes/grubenv
    logg success 'Copied /boot/grub2/grubenv to /boot/efi/EFI/qubes/grubenv'
fi

### Ensure /boot/grub2/themes is directory
if [ ! -d /boot/grub2/themes ]; then
    sudo mkdir -p /boot/grub2/themes
    logg success 'Created /boot/grub2/themes'
fi

### Copy GRUB theme to /boot/grub2/themes
if [ -d /usr/local/share/grub/themes ]; then
    sudo cp -rf /usr/local/share/grub/themes /boot/grub2/
    logg success 'Copied GRUB themes in /usr/local/share/grub/themes to /boot/grub2/themes'
else
    logg warn '/usr/local/share/grub/themes is missing'
fi

### Set default GRUB screen resolution variables
SCREEN_RATIO_ULTRAWIDE="2100"
GRUB_RESOLUTION_TYPE="1080p"

### Determine screen size ratio (used for picking GRUB2 theme resolution)
if command -v xrandr > /dev/null && command -v uniq > /dev/null; then
    SCREEN_WIDTH="$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)"
    SCREEN_HEIGHT="$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)"
    SCREEN_RATIO="$(awk -v height="$SCREEN_HEIGHT" -v width="$SCREEN_WIDTH" 'BEGIN { print ((height / width) * 1000) }')"
    SCREEN_RATIO="${SCREEN_RATIO%.*}"
    logg success "Screen detected as $SCREEN_WIDTH x $SCREEN_HEIGHT (ratio of $SCREEN_RATIO)"
    if (( $(echo "$SCREEN_RATIO $SCREEN_RATIO_ULTRAWIDE" | awk '{print ($1 > $2)}') )); then
        GRUB_RESOLUTION_TYPE="ultrawide"
        logg info 'GRUB resolution registered as ultrawide'
    fi
else
    logg warn 'Missing either xrandr or uniq (required for calculating screen size ratio)'
fi

### Optimize the GRUB resolution
if [ -f /etc/default/grub ]; then
    ### GRUB_GFXMODE
    logg info 'Setting GRUB_GFXMODE=auto in /etc/default/grub'
    if cat /etc/default/grub | grep GRUB_GFX_MODE > /dev/null; then
        sudo sed -i 's/.*GRUB_GFXMODE.*/GRUB_GFXMODE=auto/' /etc/default/grub
    else
        echo "GRUB_GFXMODE=auto" | sudo tee -a /etc/default/grub
    fi

    ### GRUB_GFXPAYLOAD_LINUX
    logg info 'Setting GRUB_GFXPAYLOAD_LINUX=keep in /etc/default/grub'
    if cat /etc/default/grub | grep GRUB_GFXPAYLOAD_LINUX > /dev/null; then
        sudo sed -i 's/.*GRUB_GFXPAYLOAD_LINUX.*/GRUB_GFXPAYLOAD_LINUX="keep"/' /etc/default/grub
    else
        echo 'GRUB_GFXPAYLOAD_LINUX="keep"' | sudo tee -a /etc/default/grub
    fi

    ### GRUB_THEME
    logg info 'Setting GRUB_THEME={{ .theme }} in /etc/default/grub'
    if cat /etc/default/grub | grep GRUB_THEME > /dev/null; then
        sudo sed -i 's/.*GRUB_THEME.*/GRUB_THEME="{{ .theme }}-'"$GRUB_RESOLUTION_TYPE"'"/' /etc/default/grub
    else
        echo 'GRUB_THEME="{{ .theme }}-'"$GRUB_RESOLUTION_TYPE"'"' | sudo tee -a /etc/default/grub
    fi

    ### GRUB_BACKGROUND
    logg info 'Setting GRUB_BACKGROUND=/usr/local/share/grub/{{ .theme }}-blue.png in /etc/default/grub'
    if cat /etc/default/grub | grep GRUB_BACKGROUND > /dev/null; then
        sudo sed -i 's/.*GRUB_BACKGROUND.*/GRUB_BACKGROUND="\/usr\/local\/share\/grub\/{{ .theme }}-blue.png"/' /etc/default/grub
    else
        echo 'GRUB_BACKGROUND="/usr/local/share/grub/{{ .theme }}-blue.png"' | sudo tee -a /etc/default/grub
    fi

    ### GRUB_TIMEOUT
    logg info 'Setting GRUB_TIMEOUT={{ .grub.timeout }} in /etc/default/grub'
    if cat /etc/default/grub | grep GRUB_TIMEOUT > /dev/null; then
        sudo sed -i 's/.*GRUB_TIMEOUT.*/GRUB_TIMEOUT="{{ .grub.timeout }}"/' /etc/default/grub
    else
        echo 'GRUB_TIMEOUT="{{ .grub.timeout }}"' | sudo tee -a /etc/default/grub
    fi

    ### GRUB_FORCE_HIDDEN_MENU
    logg info 'Setting GRUB_FORCE_HIDDEN_MENU={{ .grub.shiftToSee }} in /etc/default/grub'
    sudo sed -i '/GRUB_FORCE_HIDDEN_MENU/d' /etc/default/grub
    echo "GRUB_FORCE_HIDDEN_MENU={{ .grub.shiftToSee }}" | sudo tee -a /etc/default/grub

    ### Remove duplicate lines in /etc/default/grub
    logg info 'Ensuring there are no duplicate entries in /etc/default/grub'
    cat /etc/default/grub | uniq | sudo tee /etc/default/grub > /dev/null
else
    logg warn '/etc/default/grub is missing'
fi

### Determine platform-specific icon to use
if command -v qubesctl > /dev/null; then
    GRUB_ICON='qubes'
elif [ -f "/boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/{{ .host.distro.id }}.png" ]; then
    GRUB_ICON='{{ .host.distro.id }}'
elif [ -f "/boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/{{ .host.distro.family }}.png" ]; then
    GRUB_ICON='{{ .host.distro.family }}'
else
    GRUB_ICON='linux'
fi

### Copy icon to GRUB2 theme folder
if [ -f "/boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png" ]; then
    sudo cp -f /boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png /boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icon.png
    logg success 'Copied platform-specific icon to GRUB2 theme folder'
else
    logg warn "/boot/grub2/themes/{{ .theme }}-$GRUB_RESOLUTION_TYPE/icons/$GRUB_ICON.png is missing"
fi

### Ensure grub2-mkconfig is available
if ! command -v grub2-mkconfig > /dev/null; then
    if command -v grub-mkconfig > /dev/null; then
        sudo ln -s "$(which grub-mkconfig)" /usr/bin/grub2-mkconfig
    else
        logg warn 'Neither grub2-mkconfig or grub-mkconfig are available'
    fi
fi

### Apply GRUB2 theme
# Set export DEBUG_MODE=true to bypass GRUB2 / Plymouth application
if [ "$DEBUG_MODE" != 'true' ]; then
    if [ -d /sys/firmware/efi ]; then
        logg info 'Assuming system is UEFI-enabled since /sys/firmware/efi is present'
        if [ -f /boot/efi/EFI/qubes/grub.cfg ]; then
            logg info 'Running sudo grub2-mkconfig -o /boot/efi/EFI/qubes/grub.cfg'
            sudo grub2-mkconfig -o /boot/efi/EFI/qubes/grub.cfg
            logg success 'Applied GRUB2 theme'
        elif [ -f /boot/efi/EFI/grub.cfg ]; then
            logg info 'Running sudo grub2-mkconfig -o /boot/efi/EFI/grub.cfg'
            sudo grub2-mkconfig -o /boot/efi/EFI/grub.cfg
            logg success 'Applied GRUB2 theme'
        else
            logg warn 'Unknown GRUB2 configuration - not applying GRUB2 theme'
        fi
    else
        logg info 'Assuming system is non-UEFI since /sys/firmware/efi is not present'
        logg info 'Running sudo grub2-mkconfig -o /boot/grub2/grub.cfg'
        sudo grub2-mkconfig -o /boot/grub2/grub.cfg
        logg success 'Applied GRUB2 theme'
    fi
else
    logg info 'Skipping GRUB2 theme application because DEBUG_MODE is set to true'
fi

{{ end -}}
