#!/bin/sh

set -eu

read puavo_hosttype < /etc/puavo/hosttype

test "$puavo_hosttype" = 'exam' || exit 0

if ! boot_disk_list=$(/usr/lib/puavo-core/puavo-get-boot-disks); then
  echo 'could not lookup boot disks' >&2
  exit 1
fi

for boot_disk in $boot_disk_list; do
  efi_partition_dev=$(
    lsblk --properties-by blkid -n -l -o PATH,PARTTYPE "$boot_disk" \
      | awk '$2 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" {
               print $1; exit(0)
             }')
  test -n "efi_partition_dev" && break
done

if [ -z "$efi_partition_dev" ]; then
  echo 'did not find EFI partition' >&2
  exit 1
fi

mkdir -p /boot/efi
if ! mount "$efi_partition_dev" /boot/efi; then
  echo 'could not mount the EFI partition to /boot/efi' >&2
  exit 1
fi

exit 0
