### this file is sourced not run
PKGVER=2.02.2018.08.fed
PKGBUILD=1
PKGARCH=i686

# source: james + fedora patches
TARBALL=grub-20180725.tar.bz2
MD5SUM=470bfda948985dba4543e6d55d4b32f0
SRC_URL=http://distro.ibiblio.org/fatdog/source/800/$TARBALL
BUNDLE=

PKG_NOSTRIP=yes # don't strip to preserve Secure Boot signatures
BINARY_NAME=grubia32-fed.efi

SLACKREQ='' # 
SLACKDESC="grub2-efi32: grub2-efi32 $PKGVER (UEFI Bootloader)  
grub2-efi32:  
grub2-efi32: This contains GRUB2 bootloader for booting 32-bit UEFI systems. 
grub2-efi32: It only contains the $BINARY_NAME package, which you can rename
grub2-efi32: to bootia32.efi to get a fully functional bootloader.
grub2-efi32: 
grub2-efi32: The bootloader is signed with Fatdog64 key.
grub2-efi32: 
grub2-efi32: 
grub2-efi32: 
grub2-efi32: 
"

LINKED_GRUB_MODULES="part_msdos part_gpt part_apple \
iso9660 fat ext2 exfat udf ntfs btrfs xfs f2fs hfsplus lvm loopback \
efi_gop efi_uga gfxterm font gfxmenu \
configfile normal chain boot \
efinet net tftp http \
png jpeg"

### default pkg_download
### prepare
pkg_prepare() {
	pkg_prepare_default &&
	cp -a $SRC_DIR/../keys . &&
	mkdir tmp/build &&
	cd tmp/grub* &&

	# apply patches
	while read -r p; do
		case "$p" in
			\#*|"") continue ;;
		esac
		#p="${p/#* /}"
		p=$(echo $p | awk '{print $2}')
		echo "--- applying $p"
		patch -Np1 -i fedora-patches/"$p" || return 1
	done < fedora-patches/series
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/grub* 

	./autogen.sh &&
	autoreconf -fi &&
	
	# build - use -O1, something in -O2 and -Os kills it
	CFLAGS="-O1" \
	./configure --with-platform=efi --target=i386 --disable-werror --with-utils=host \
		--prefix=/tmp/build  &&
	make $MAKEFLAGS && make install &&
	
	# post-processing to make a standalone image
	cd /tmp/build &&

	# our prefix henceforth shall be /grub
	PREFIX=grub # don't start with slash
	cat > bootstrap.cfg << EOF &&
set prefix=(memdisk)/$PREFIX
normal
EOF
	
	# default contents for memdisk - taken from grub-mkstandalone
	mkdir -p memdisk/$PREFIX/{i386-efi,locale,fonts} &&
	cp -v lib/grub/*/{*.lst,*.mod} memdisk/$PREFIX/i386-efi &&
	# delete the modules we're doing to link together later to save space
	for p in memdisk tar $LINKED_GRUB_MODULES; do rm memdisk/$PREFIX/i386-efi/${p}.mod; done &&
	
	# our config and font
	cp -v /tmp/pkg/euro.pf2 memdisk/$PREFIX/fonts &&
	cp -v /tmp/pkg/grub2-embedded.cfg memdisk/$PREFIX/grub.cfg &&

	# generate grubia32.efi - taken from grub-mkstandalone
	(cd memdisk; tar -c *) > memdisk.tar &&	
	bin/grub-mkimage -O i386-efi -o grubia32.efi -C xz -c bootstrap.cfg \
	    -m memdisk.tar -p $PREFIX memdisk tar $LINKED_GRUB_MODULES &&

	# add SBAT section (needed for newer shims)
	# see https://github.com/rhboot/shim/issues/376#issuecomment-964137621
	objcopy --add-section .sbat=/tmp/pkg/grub2.csv --adjust-section-vma .sbat+10000000 grubia32.efi &&

	# sign it
	mkdir -p /usr/share/grub2-efi &&	
	sbsign --key /keys/fatdog64.key --cert /keys/fatdog64-2041.crt \
	    --output /usr/share/grub2-efi/$BINARY_NAME grubia32.efi &&
	sbverify --cert /keys/fatdog64-2041.crt /usr/share/grub2-efi/$BINARY_NAME &&
	rm -rf /keys &&

	# copy font and grub2-embedded for reference
	install -m644 /tmp/pkg/euro.pf2 /tmp/pkg/grub2-embedded.cfg /usr/share/grub2-efi &&

	pkg_build_slackdesc
}

