#!/bin/bash

# Usage: setup_kcare_certs.sh [--force | --update | --info]
#
# Puts the KernelCare certwrapper next to shim.  --force installs it even next to a shim that
# would stop trusting the current KernelCare key after a reboot.
#
# --update only replaces a KernelCare certwrapper that is already there, and never next to a
# shim older than 16.0.  The package runs it on every install and upgrade, so it says nothing
# and exits 0 on a host that has none.  It exits 3 when it leaves the certwrapper next to such a
# shim, which only a shim upgrade changes.  Any other failure prints a WARNING that the
# certwrapper may be out of date: the package goes on.
#
# --info changes nothing.  It prints the state of the KernelCare certificates and what to do, if
# anything, to keep kcare.ko loading across the 2027-01-13 key switch.  Like the other modes, it
# needs root: it reads the ESP and /proc/keys.
#
# kcarectl does the work, in kcarectl/certwrapper.py, so that its rules are the ones kcarectl's
# Secure Boot checks use.  This script stays as the path the package and the docs name.

set -eu -o pipefail

case "${1:-}" in
    "") mode=install ;;
    --force) mode=force ;;
    --update) mode=update ;;
    --info) mode=info ;;
    *)
        >&2 echo "Usage: $0 [--force | --update | --info]"
        exit 2
        ;;
esac

exec /usr/bin/kcarectl --setup-kcare-certs "$mode"
