#!/usr/bin/env bash

have() { command -v "$1" >/dev/null 2>&1; }

# Product/model name from DMI, e.g. "OMEN Laptop 15-en0xxx" - falls back to
# vendor+board, then hostname, if the board reports no usable product name.
get_model() {
    local model=""
    if [ -r /sys/class/dmi/id/product_name ]; then
        model=$(cat /sys/class/dmi/id/product_name 2>/dev/null)
    fi
    case "$model" in
        ""|*"O.E.M"*|*"To Be Filled"*|"System Product Name")
            local vendor board
            vendor=$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null)
            board=$(cat /sys/class/dmi/id/board_name 2>/dev/null)
            model="${vendor:+$vendor }${board}"
            ;;
    esac
    model=$(echo "$model" | tr '/' '-' | sed 's/^ *//;s/ *$//')
    [ -z "$model" ] && model="$(hostname)"
    echo "$model"
}

OUTPUT_FILE="./$(get_model)_$(date +%F).txt"

section() { printf '\n===== %s =====\n' "$1"; }
# Prints which package likely provides a missing command, so it can be
# looked up with the distro's package manager (apt/pacman/dnf all index by
# similar names, so searching the hinted package name works across distros).
# Also echoed to the real terminal (fd 3) since the whole block below is
# redirected to the output file - otherwise missing-tool messages would be
# invisible until the script finishes and you open the file.
missing() {
    local cmd="$1" pkg="${2:-$1}"
    local msg="[MISSING] '$cmd' not found - try: apt search $pkg   |   pacman -Ss $pkg   |   dnf search $pkg"
    echo "$msg"
    echo "$msg" >&3
}

exec 3>&1
{
    section "SUMMARY (fastfetch)"
    have fastfetch && fastfetch --logo none --structure-disabled colors || missing fastfetch

    section "inxi full"
    have inxi && inxi -Fzxx || missing inxi

    section "hostnamectl"
    have hostnamectl && hostnamectl || missing hostnamectl systemd

    section "lscpu"
    have lscpu && lscpu || missing lscpu util-linux

    section "CPU virtualization support"
    flags=$(grep -m1 -o -E 'vmx|svm' /proc/cpuinfo | sort -u | tr '\n' ' ')
    [ -n "$flags" ] && echo "Flags found: $flags" || echo "No VT-x/AMD-V flag found in /proc/cpuinfo"
    [ -e /dev/kvm ] && echo "/dev/kvm present (KVM usable)" || echo "/dev/kvm NOT present"
    have systemd-detect-virt && echo "Running inside: $(systemd-detect-virt)" || missing systemd-detect-virt systemd

    section "IOMMU (VT-d / AMD-Vi)"
    if compgen -G "/sys/kernel/iommu_groups/*" > /dev/null 2>&1; then
        echo "IOMMU groups present: $(ls -d /sys/kernel/iommu_groups/*/ 2>/dev/null | wc -l)"
    else
        echo "No IOMMU groups found (VT-d/AMD-Vi likely disabled in BIOS or unsupported - not a package, check BIOS settings)"
    fi

    section "TPM"
    if [ -d /sys/class/tpm ] && [ "$(ls -A /sys/class/tpm 2>/dev/null)" ]; then
        for t in /sys/class/tpm/*; do
            v=""
            [ -f "$t/tpm_version_major" ] && v=" version_major=$(cat "$t/tpm_version_major")"
            [ -f "$t/caps" ] && v="$v caps: $(head -1 "$t/caps" 2>/dev/null)"
            echo "$t$v"
        done
    else
        echo "No TPM device found (not a package - hardware/firmware dependent)"
    fi

    section "SPI / I2C / ACPI peripherals (covers sensors that don't show up on PCI/USB, e.g. some fingerprint readers)"
    if [ -d /sys/bus/spi/devices ] && [ -n "$(ls -A /sys/bus/spi/devices 2>/dev/null)" ]; then
        echo "--- SPI devices ---"
        for d in /sys/bus/spi/devices/*; do
            modalias=$(cat "$d/modalias" 2>/dev/null)
            driver=$(basename "$(readlink -f "$d/driver" 2>/dev/null)" 2>/dev/null)
            echo "$(basename "$d")  modalias=$modalias  driver=${driver:-none}"
        done
    else
        echo "No SPI devices found"
    fi
    if [ -d /sys/bus/i2c/devices ] && [ -n "$(ls -A /sys/bus/i2c/devices 2>/dev/null)" ]; then
        echo "--- I2C devices ---"
        for d in /sys/bus/i2c/devices/*; do
            modalias=$(cat "$d/modalias" 2>/dev/null)
            driver=$(basename "$(readlink -f "$d/driver" 2>/dev/null)" 2>/dev/null)
            echo "$(basename "$d")  modalias=$modalias  driver=${driver:-none}"
        done
    else
        echo "No I2C devices found"
    fi
    if [ -d /sys/bus/acpi/devices ]; then
        echo "--- ACPI device HIDs (fingerprint sensors often show as SYNA*/VFS*/ELAN*/FPC*/GTCH* - search this list) ---"
        for d in /sys/bus/acpi/devices/*; do
            hid=$(cat "$d/hid" 2>/dev/null)
            [ -n "$hid" ] && echo "$(basename "$d"): $hid"
        done
    fi
    echo "--- kernel log hints ---"
    sudo dmesg 2>/dev/null | grep -iE 'fingerprint|fpc1[0-9]{3}|goodix|validity|elan.*fp|synaptics.*(spi|fp)' || echo "No fingerprint-related kernel log hints found"

    section "Memory (dmidecode -t memory)"
    have dmidecode && sudo dmidecode -t memory || missing dmidecode
    have free && free -h || missing free procps

    section "Motherboard / BIOS / Chassis / System (dmidecode)"
    have dmidecode && sudo dmidecode -t baseboard -t bios -t chassis -t system || missing dmidecode

    section "Expansion slots (PCIe/M.2/etc.)"
    have dmidecode && sudo dmidecode -t slot || missing dmidecode

    section "lshw full"
    have lshw && sudo lshw || missing lshw

    section "Block devices"
    have lsblk && lsblk -o NAME,MODEL,SERIAL,SIZE,TYPE,TRAN,ROTA,FSTYPE,MOUNTPOINT || missing lsblk util-linux

    section "ZFS"
    if have zpool; then
        zpool list -v
        zpool status
        zfs list
    else
        missing zpool zfsutils-linux
    fi

    section "Disk health (SMART)"
    if have smartctl; then
        for d in $(lsblk -dno NAME 2>/dev/null); do
            echo "--- /dev/$d ---"
            sudo smartctl -a "/dev/$d" 2>&1
        done
    else
        missing smartctl smartmontools
    fi

    section "Network interfaces"
    if have ip; then
        ip -br link
        ip -br addr
    else
        missing ip iproute2
        have ifconfig && ifconfig -a || missing ifconfig net-tools
    fi

    section "Ethernet link capabilities (ethtool)"
    if have ethtool; then
        for i in $(ls /sys/class/net 2>/dev/null | grep -v lo); do
            echo "--- $i ---"
            sudo ethtool "$i" 2>&1
        done
    else
        missing ethtool
    fi

    section "Wi-Fi (iw)"
    have iw && iw dev || missing iw

    section "PCI devices"
    have lspci && lspci -vvnn || missing lspci pciutils

    section "USB devices"
    have lsusb && lsusb || missing lsusb usbutils

    section "Sensors (temps/fans/voltages)"
    have sensors && sensors || missing sensors lm-sensors

    section "Battery"
    if have upower; then
        for b in $(upower -e 2>/dev/null | grep -i BAT); do
            upower -i "$b"
        done
    else
        missing upower
    fi

} > "$OUTPUT_FILE" 2>&1

echo "Saved to $OUTPUT_FILE"
