#!/bin/sh

set -eu

panic() {
  echo "error: $1" >&2
  exit 1
}

destination_directory='/run/puavo/secure-boot-keys'
source_directory="${1:-/images/boot/addons}"

# Hosts without UKI command-line signing, such as the diskinstaller,
# have no certificate on disk. Neither does a fully installed encrypted
# device, where the Boot Trust Manager publishes it from the boot vault.
[ -f "${source_directory}/secure-boot.pem" ] || exit 0

install -d -m 0700 "$destination_directory" \
  || panic "failed to create ${destination_directory}"
install -m 0644 "${source_directory}/secure-boot.pem" \
  "${destination_directory}/secure-boot.pem" \
  || panic "failed to install device Secure Boot certificate"
