#!/bin/sh
# Clawdie operator USB live GPU setup.
# Detect display hardware and load a conservative KMS module before SDDM.
# Prefer integrated/open drivers so hybrid laptops boot the broadest path first;
# keep NVIDIA proprietary loading opt-in by requiring an installed nvidia.ko.

# PROVIDE: clawdie_live_gpu
# REQUIRE: FILESYSTEMS devfs
# BEFORE: sddm
# KEYWORD: nojail

. /etc/rc.subr

name="clawdie_live_gpu"
rcvar="clawdie_live_gpu_enable"
start_cmd="clawdie_live_gpu_start"

LOG_FILE="/var/log/clawdie-live-gpu.log"

clawdie_live_gpu_log()
{
    echo "$(date '+%H:%M:%S') $*" >> "$LOG_FILE" 2>/dev/null || true
}

clawdie_live_gpu_display_blocks()
{
    pciconf -lv 2>/dev/null | awk '
        BEGIN { RS = ""; IGNORECASE = 1 }
        /class=0x03/ { print }
    '
}

clawdie_live_gpu_has_pci_vendor()
{
    _blocks="$1"
    _vendor_id="$2"

    # Match the numeric PCI vendor on display-class blocks only. Do not use
    # broad text matches like "Intel" here: unrelated vendor strings or
    # subdevices can make an AMD system select i915kms and fail before SDDM.
    echo "$_blocks" | awk -v vendor="$_vendor_id" '
        BEGIN { RS = ""; IGNORECASE = 1; found = 0 }
        $0 ~ ("(^|[[:space:]])vendor=" vendor "([[:space:]]|$)") { found = 1 }
        END { exit(found ? 0 : 1) }
    '
}

clawdie_live_gpu_select_modules()
{
    _blocks="$1"

    # Hybrid laptops often expose Intel or AMD integrated graphics plus a
    # discrete NVIDIA GPU. Prefer the integrated/open KMS path for live boot.
    if clawdie_live_gpu_has_pci_vendor "$_blocks" "0x1002"; then
        # amdgpu covers modern AMD; radeonkms is a best-effort fallback for
        # older Radeon hardware only if amdgpu does not load.
        echo "amdgpu radeonkms"
        return 0
    fi

    if clawdie_live_gpu_has_pci_vendor "$_blocks" "0x8086"; then
        echo "i915kms"
        return 0
    fi

    if clawdie_live_gpu_has_pci_vendor "$_blocks" "0x15ad"; then
        echo "vmwgfx"
        return 0
    fi

    if clawdie_live_gpu_has_pci_vendor "$_blocks" "0x10de"; then
        # NVIDIA packages are bundled in the offline repo, but the live image
        # does not install a branch-specific NVIDIA kmod by default because the
        # 390/470/current branches conflict. Only load NVIDIA if an operator or
        # future build profile has installed a concrete branch into the rootfs.
        if [ -e /boot/modules/nvidia.ko ]; then
            echo "nvidia-modeset nvidia"
        fi
        return 0
    fi

    return 0
}

clawdie_live_gpu_load_module()
{
    _module="$1"

    if [ "$_module" = "radeonkms" ] && kldstat -n amdgpu.ko >/dev/null 2>&1; then
        clawdie_live_gpu_log "skipping radeonkms because amdgpu is already loaded"
        return 0
    fi

    if kldstat -n "${_module}.ko" >/dev/null 2>&1; then
        clawdie_live_gpu_log "module already loaded: ${_module}"
        return 0
    fi

    if kldload "$_module" >/dev/null 2>&1; then
        clawdie_live_gpu_log "loaded module: ${_module}"
        return 0
    fi

    clawdie_live_gpu_log "WARN: failed to load module: ${_module}"
    return 1
}

clawdie_live_gpu_start()
{
    _blocks=$(clawdie_live_gpu_display_blocks)
    if [ -z "$_blocks" ]; then
        clawdie_live_gpu_log "no display-class PCI device detected; using scfb/vesa fallback"
        return 0
    fi

    clawdie_live_gpu_log "detected display devices: $(echo "$_blocks" | grep -i '^vgapci' | tr '\n' ' ')"
    _modules=$(clawdie_live_gpu_select_modules "$_blocks")
    if [ -z "$_modules" ]; then
        clawdie_live_gpu_log "no live KMS module selected; using scfb/vesa fallback"
        return 0
    fi

    clawdie_live_gpu_log "selected live KMS modules: ${_modules}"
    for _module in $_modules; do
        clawdie_live_gpu_load_module "$_module" || true
    done

    return 0
}

load_rc_config "$name"
: ${clawdie_live_gpu_enable:="NO"}

run_rc_command "$1"
