#!/usr/bin/env bash

# Intel GPU Helper Functions for Proxmox LXC / VAAPI passthrough
# Author: CanbiZ
# License: MIT

set -euo pipefail

function exit_script() {
    printf "⚠️  User exited script\n"
    exit 0
}

function msg() {
    local type="$1"
    shift
    case "$type" in
    info) printf " \033[36m➤\033[0m %s\n" "$@" ;;
    ok) printf " \033[32m✔\033[0m %s\n" "$@" ;;
    warn) printf " \033[33m⚠\033[0m %s\n" "$@" >&2 ;;
    err) printf " \033[31m✘\033[0m %s\n" "$@" >&2 ;;
    esac
}

function intel_gpu_available() {
    [[ -e /dev/dri/renderD128 ]] && lspci | grep -qi 'VGA.*Intel'
}

function is_alpine_ct() {
    local ctid="$1"
    pct exec "$ctid" -- sh -c 'grep -qi alpine /etc/os-release' >/dev/null 2>&1
}

function passthrough_intel_to_lxc() {
    local ctid="$1"
    local conf="/etc/pve/lxc/${ctid}.conf"

    if ! intel_gpu_available; then
        msg warn "No Intel iGPU detected on host"
        return 1
    fi

    {
        echo "# Intel iGPU (VAAPI)"
        echo "lxc.cgroup2.devices.allow: c 226:* rwm"
        echo "lxc.cgroup2.devices.allow: c 29:0 rwm"
        echo "lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file"
        echo "lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir"
        echo "lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file"
    } >>"$conf"

    msg ok "Intel VAAPI passthrough applied to CT $ctid"
    return 0
}

function install_intel_tools_in_ct() {
    local ctid="$1"
    local install_nonfree="yes"

    if is_alpine_ct "$ctid"; then
        msg warn "Skipping Intel tool install for Alpine CT $ctid"
        return 0
    fi

    if [[ "$install_nonfree" == "yes" ]]; then
        msg info "Enabling non-free sources in $ctid..."

        pct exec "$ctid" -- bash -c '
      grep -q "non-free" /etc/apt/sources.list && exit 0

      cat <<EOF > /etc/apt/sources.list.d/non-free.list
deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
EOF
    '
    fi

    msg info "Installing Intel tools in CT $ctid..."

    pct exec "$ctid" -- bash -c "
      apt-get update -qq
      DEBIAN_FRONTEND=noninteractive apt-get install -y \
        va-driver-all vainfo intel-gpu-tools ocl-icd-libopencl1 intel-opencl-icd intel-media-va-driver-non-free >/dev/null 2>&1
      adduser root video >/dev/null 2>&1 || true
      adduser root render >/dev/null 2>&1 || true
    "

    msg ok "Installed Intel VAAPI tools in $ctid"
}
