#!/bin/bash
# shellcheck disable=SC1091

function set_MODPROBE () {
    # Get the config paths
    source "$SCRIPTDIR/lib/paths.sh"

    # Assign the GPU device ids to a variable
    GPU_DEVID="$1"

    # If VGA is disabled
    if grep -q "vfio_pci.disable_vga=1" "$SCRIPTDIR/config/kernel_args" ;
    then
        # Modify our GPU_DEVID line to containe disable_vga=1
        GPU_DEVID="${GPU_DEVID} disable_vga=1"
    fi

    # Write the vfio modprobe config
    printf "## This is an autogenerated file that stubs your graphic card for use with vfio
## This file should be placed inside /etc/modprobe.d/
# Uncomment the line below to \"hardcode\" your graphic card to be bound to the vfio-pci driver.
# In most cases this should not be neccessary, it will also prevent you from turning off vfio in the bootloader.
#options vfio_pci ids=%s

# Make sure vfio_pci is loaded before these modules: nvidia, nouveau, amdgpu and radeon
softdep nvidia pre: vfio vfio_pci
softdep nouveau pre: vfio vfio_pci
softdep amdgpu pre: vfio vfio_pci
softdep radeon pre: vfio vfio_pci
"  "${GPU_DEVID}"  > "$SCRIPTDIR/$MODPROBE/vfio.conf"

    exec "$SCRIPTDIR/lib/get_USB_CTL.sh"
}

function main () {
    SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")

    set_MODPROBE "$1"
}

main "$1"