#!/bin/bash
# Filename:      ${GRML_FAI_CONFIG}/scripts/GRMLBASE/45-grub-images
# Purpose:       create grub images for use in ISO
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2 or any later version.
################################################################################

set -e
set -u

# shellcheck source=/dev/null
. "$GRML_LIVE_CONFIG"

# FAI sets $target, but shellcheck does not know that.
target=${target:?}

TMP_CONFIG="/tmp/grub_config_efi"

mkdir -p "${target}"/boot/grub

# BOOT_FILE is set by grml-live.
cat > "${target}/${TMP_CONFIG}" <<EOF
search.file ${BOOT_FILE} root
set prefix=(\$root)/boot/grub
insmod normal
normal
echo "E: Could not find root device (for ${BOOT_FILE})!"
EOF

ARCHS=()
declare -A ADDITIONAL_MODULES

if ifclass ARM64 ; then
  if [ -r "${target}"/usr/lib/grub/arm64-efi/moddep.lst ] ; then
    ARCHS=(arm64-efi)
    ADDITIONAL_MODULES[arm64-efi]="efi_gop"
  else
    echo "/usr/lib/grub/arm64-efi/moddep.lst could not be found, skipping."
    echo "NOTE: grub-efi-arm64-bin not installed?"
  fi
fi

if ifclass AMD64 ; then
  if [ -r "${target}"/usr/lib/grub/x86_64-efi/moddep.lst ] ; then
    ARCHS+=(x86_64-efi)
    ADDITIONAL_MODULES[x86_64-efi]="efi_gop"
  else
    echo "/usr/lib/grub/x86_64-efi/moddep.lst could not be found, skipping."
    echo "NOTE: grub-efi-amd64-bin not installed?"
  fi
fi

# note: enabled also on AMD64 for UEFI 32bit boot support
if ifclass I386 || ifclass AMD64 ; then
  if [ -r "${target}"/usr/lib/grub/i386-efi/moddep.lst ] ; then
    ARCHS+=(i386-efi)
    ADDITIONAL_MODULES[i386-efi]="efi_gop"
  else
    echo "/usr/lib/grub/i386-efi/moddep.lst could not be found, skipping."
    echo "NOTE: grub-efi-ia32 not installed?"
  fi

  # i386-pc for BIOS boot
  if [ -r "${target}"/usr/lib/grub/i386-pc/moddep.lst ] ; then
    ARCHS+=(i386-pc)
    ADDITIONAL_MODULES[i386-pc]="biosdisk"
  else
    echo "ERROR: /usr/lib/grub/i386-pc/moddep.lst could not be found. grub-pc-bin not installed?" >&2
    exit 1
  fi
fi

for arch in "${ARCHS[@]}" ; do
  filename=''
  case "$arch" in
    x86_64-efi) filename=/boot/bootx64.efi   ;;
    i386-efi)   filename=/boot/bootia32.efi  ;;
    arm64-efi)  filename=/boot/bootaa64.efi  ;;
    i386-pc)    filename=/boot/grub/i386-pc/core.img  ;;
  esac

  # Create directory for i386-pc if needed
  if [ "$arch" = "i386-pc" ] ; then
    mkdir -p "${target}/boot/grub/i386-pc"
  fi

  read -r -a modules <<< "${ADDITIONAL_MODULES[$arch]}"
  $ROOTCMD grub-mkimage -O "$arch" -o "$filename" --prefix=/boot/grub/ --config="$TMP_CONFIG" \
    echo iso9660 part_msdos search_fs_file test \
    fat ext2 xfs btrfs zfs zfscrypt squash4 part_gpt lvm \
    "${modules[@]}"
done

rm -f "${target}/${TMP_CONFIG}"
echo "Generated Grub images"

## END OF FILE #################################################################
# vim:ft=sh expandtab ai tw=80 tabstop=4 shiftwidth=2
