#!/usr/bin/bash
# SPDX-FileCopyrightText: 2025 Nicolas Chauvet <kwizart@gmail.com>
# SPDX-License-Identifier: AGPL-3.0
#
# nvidia-kmod-noopen-checks - Checks for Nvidia cards not supporting kernel-open
#

# File containing the target PCI IDs
PCIIDS_FILE="${1:-nvidia-kmod-noopen-pciids.txt}"

# Read the target PCI IDs from the file
TARGET_PCIIDS=($(cat "$PCIIDS_FILE"))

# Get the PCI IDs of the graphics controllers present on the system
SYSTEM_PCIIDS=($(lspci -nn | grep -E 'VGA compatible controller|3D controller' | grep -oP '\[\K[0-9a-fA-F:]+\b'))

# Check for the presence of target PCI IDs in the system PCI IDs
for PCIID in "${TARGET_PCIIDS[@]}"; do
    if [[ " ${SYSTEM_PCIIDS[@]} " =~ " ${PCIID} " ]]; then
        echo "Graphics card found with PCI ID: $PCIID"
        echo "Not using nvidia-open"
        exit
    fi
done

echo "No matching graphics card found."
echo "Using nvidia-open"
mv kernel kernel-closed
mv kernel-open kernel

