#!/bin/bash

#   generate GRUB ELF files (coreboot payload) and configuration files
#
#	Copyright (C) 2014, 2015, 2020 Leah Rowe <info@minifree.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

# This is where GRUB is expected to be (outside of the grub-assemble, instead in main checkout)
source "resources/grub/modules.list"

printf "Creating GRUB payloads and configuration files\n"

if [ ! -d "grub/" ]; then
    ./download grub
fi

if [ ! -f "grub/grub-mkstandalone" ]; then
    ./build module grub
fi

[ ! -d "payload/" ] && mkdir -p payload/
[ ! -d "payload/grub" ] && mkdir -p payload/grub/

rm -f payload/grub/*

# This creates a grub.elf, which will be used with both
# text mode and vesa startup in coreboot
# This will reduce memdisk usage, important to mitigate bug with memdisk too big

# Make GRUB payload executables for each supported keyboard layout.
# Each payload will be applicable to text mode or vesafb configuration in coreboot

for keylayout in resources/grub/keymap/original/*; do
    if [ ! -f "${keylayout}" ]; then
        continue
    fi
    keymap="${keylayout##*/}"

    grub/grub-mkstandalone \
      --grub-mkimage="grub/grub-mkimage" \
      -O i386-coreboot \
      -o payload/grub/grub_${keymap}.elf \
      -d grub/grub-core/ \
      --fonts= --themes= --locales=  \
      --modules="${grub_modules}" \
      --install-modules="${grub_install_modules}" \
      /boot/grub/grub.cfg=resources/grub/config/grub_memdisk.cfg \
      /boot/grub/layouts/${keymap}.gkb=resources/grub/keymap/${keymap}.gkb
done

# Now generate the configs. One pair of configs for each keyboard layout.
# In each pair there is a configuration for text mode, and one for vesafb

for romtype in txtmode vesafb; do
	# GRUB configuration files
	for keylayout in resources/grub/keymap/original/*; do
        if [ ! -f "${keylayout}" ]; then
            continue
        fi
		keymap="${keylayout##*/}"
		cat resources/grub/config/extra/{common,"${romtype}"}.cfg > payload/grub/grub_${keymap}_${romtype}.cfg
		printf "keymap %s\n" "${keymap}" >> payload/grub/grub_${keymap}_${romtype}.cfg
		cat resources/grub/config/menuentries/{common,${romtype}}.cfg >> payload/grub/grub_${keymap}_${romtype}.cfg
		# grubtest.cfg should be able to switch back to grub.cfg
		sed "s/grubtest.cfg/grub.cfg/" < payload/grub/grub_${keymap}_${romtype}.cfg > payload/grub/grub_${keymap}_${romtype}_test.cfg
	done
done

printf "Done! Check payload/grub/ to see the files.\n\n"
