# Template file for 'grub'
pkgname=grub
version=2.02~beta3
revision=1
hostmakedepends="flex"
makedepends="libusb-compat-devel ncurses-devel freetype-devel
 liblzma-devel device-mapper-devel font-unifont-bdf fuse-devel"
depends="os-prober"
conf_files="/etc/default/grub /etc/grub.d/40_custom"
short_desc="GRand Unified Bootloader 2"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="GPL-3"
homepage="http://www.gnu.org/software/grub/"
distfiles="http://alpha.gnu.org/gnu/grub/grub-${version}.tar.xz"
checksum=30ec3d555e52a702c3eef449872ef874eff28b320f40b55ffc47f70db8e5ada1

only_for_archs="i686 i686-musl x86_64 x86_64-musl"
nopie=yes

subpackages="grub-utils grub-i386-efi"
case "$XBPS_TARGET_MACHINE" in
	x86_64*) subpackages+=" grub-x86_64-efi"; _GRUBX64=1;;
esac

post_extract() {
	sed -i -e 's,freetype/ftsynth.h,freetype2/ftsynth.h,' util/grub-mkfont.c
}

do_configure() {
	unset CC AS LD RANLIB CPP
	CFLAGS="$CFLAGS -fno-stack-protector"

	configure_args+=" --enable-device-mapper --enable-cache-stats --enable-nls
		--enable-grub-mkfont --enable-grub-mount --disable-werror
		--sbindir=/usr/bin"

	# fix unifont.bdf location so grub-mkfont can create *.pf2 files
	sed -i 's|/usr/share/fonts/unifont|/usr/share/fonts/misc|' configure

	# build BIOS version
	mkdir $wrksrc/bios_build
	cd $wrksrc/bios_build
	../configure ${configure_args} --with-platform=pc

	# build i386 EFI version
	mkdir $wrksrc/efi_i386_build
	cd $wrksrc/efi_i386_build
	../configure ${configure_args} --with-platform=efi \
		--target=i386 --disable-efiemu --libdir=/usr/lib

	if [ -n "${_GRUBX64}" ]; then
		mkdir $wrksrc/efi_build
		cd $wrksrc/efi_build
		../configure ${configure_args} --with-platform=efi \
			--target="x86_64" --disable-efiemu
	fi
}

do_build() {
	# XXX remove the strip wrapper
	rm ${XBPS_WRAPPERDIR}/strip

	msg_normal "Building BIOS grub...\n"
	cd $wrksrc/bios_build
	make ${makejobs}

	msg_normal "Building i386 EFI grub...\n"
	cd $wrksrc/efi_i386_build
	make ${makejobs}

	if [ -n "${_GRUBX64}" ]; then
		msg_normal "Building EFI grub...\n"
		cd $wrksrc/efi_build
		make ${makejobs}
	fi
}

do_install() {
	# XXX remove the strip wrapper
	rm ${XBPS_WRAPPERDIR}/strip

	if [ -n "${_GRUBX64}" ]; then
		cd $wrksrc/efi_build
		make DESTDIR=$DESTDIR/efi install
		# Remove non-platform specific files
		rm -rf ${DESTDIR}/efi/{boot,etc,usr/{share,bin}}
		rm -f ${DESTDIR}/efi/usr/lib/grub/x86_64-efi/x86_64-*
		rm -f ${DESTDIR}/efi/usr/lib/grub/x86_64-efi/*.{module,image}
		cp -r ${DESTDIR}/efi/* ${DESTDIR}
		rm -rf ${DESTDIR}/efi
	fi

	cd $wrksrc/efi_i386_build
	make DESTDIR=$DESTDIR/efi_i386 install
	# Remove non-platform specific files
	rm -rf ${DESTDIR}/efi_i386/{boot,etc,usr/{share,bin}}
	rm -f ${DESTDIR}/efi_i386/usr/lib/grub/i386-efi/i386-*
	rm -f ${DESTDIR}/efi_i386/usr/lib/grub/i386-efi/*.{module,image}
	cp -r ${DESTDIR}/efi_i386/* ${DESTDIR}
	rm -rf ${DESTDIR}/efi_i386

	cd $wrksrc/bios_build
	make DESTDIR=$DESTDIR install

	# Required to compress info files.
	vmkdir usr/share/info
	touch -f ${DESTDIR}/usr/share/info/dir

	vinstall ${FILESDIR}/grub.default 644 etc/default grub
	# Kernel hooks.
	vinstall ${FILESDIR}/kernel.d/grub.post 750 \
		etc/kernel.d/post-install 50-grub
	vinstall ${FILESDIR}/kernel.d/grub.post 750 \
		etc/kernel.d/post-remove 50-grub

	# update-grub for noobs.
	printf "#!/bin/sh\ngrub-mkconfig -o /boot/grub/grub.cfg\nexit \$?\n" >> \
		${DESTDIR}/usr/bin/update-grub
	chmod 755 ${DESTDIR}/usr/bin/update-grub

	vmkdir usr/share/bash-completion/completions
	mv ${DESTDIR}/etc/bash_completion.d/grub \
		${DESTDIR}/usr/share/bash-completion/completions

	# Remove useless tools
	rm ${DESTDIR}/usr/bin/grub-ofpathname
	rm ${DESTDIR}/usr/bin/grub-sparc64-setup
}

grub-utils_package() {
	short_desc+=" - additional utilities"
	depends="grub>=${version}"
	pkg_install() {
		vmove usr/bin/grub-menulst2cfg
		vmove usr/bin/grub-fstest
		vmove usr/bin/grub-mkfont
		vmove usr/bin/grub-mount
	}
}
grub-x86_64-efi_package() {
	noarch=yes
	depends="grub>=$version dosfstools efibootmgr"
	short_desc+=" - x86_64 EFI support"
	pkg_install() {
		vmove usr/lib/grub/x86_64-efi
	}
}
grub-i386-efi_package() {
	noarch=yes
	depends="grub>=$version dosfstools efibootmgr"
	short_desc+=" - i386 EFI support"
	pkg_install() {
		vmove usr/lib/grub/i386-efi
	}
}
