#!/usr/bin/bash
set -euo pipefail

CERT="/var/lib/dkms/mok.pub"

if ! command -v mokutil >/dev/null 2>&1; then
    echo "ERROR: mokutil is not installed."
    exit 1
fi

if [ ! -f "$CERT" ]; then
    echo "ERROR: DKMS MOK certificate not found: $CERT"
    echo "Check your DKMS signing configuration."
    exit 1
fi

if mokutil --test-key "$CERT" >/dev/null 2>&1; then
    echo "DKMS signing certificate is already enrolled."
    exit 0
fi

echo "Requesting DKMS MOK certificate enrollment..."
mokutil --import "$CERT"

echo "Enrollment requested."
echo "Reboot and complete enrollment in MokManager."
