# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=alpine-ipxe
pkgver=1.20.1
pkgrel=1
pkgdesc="iPXE build for Alpine Linux netboot"
url="https://boot.alpinelinux.org"
arch="x86 aarch64"
license="GPL-2.0-or-later"
makedepends="xz-dev perl coreutils bash syslinux xorriso"
options="!strip !check !archcheck !tracedeps" # no tests
source="ipxe-$pkgver.tar.gz::https://github.com/ipxe/ipxe/archive/v$pkgver.tar.gz
	gcc-10.patch
	gcc-11.patch
	DST_Root_CA_X3.pem
	alpine-netboot-ca.crt
	boot.ipxe
	"
builddir="$srcdir/ipxe-$pkgver/src"

# default build targets for x86 systems
_targets="bin/ipxe.iso bin/ipxe.lkrn bin/ipxe.pxe bin/undionly.kpxe bin/ipxe.usb bin/ipxe.dsk"

case $CARCH in
	x86)
		_targets="$_targets bin-i386-efi/ipxe.efi";;
	x86_64)
		_targets="$_targets bin-x86_64-efi/ipxe.efi";;
	aarch64)
		_targets="bin-arm64-efi/snp.efi"
		makedepends="xz-dev perl coreutils bash";;
esac

for _target in $_targets; do
	_target=${_target##*/}
	_target=${_target/./_}
	subpackages="$subpackages $pkgname-$_target:_split"
done

prepare() {
	default_prepare

	local general="DIGEST_CMD NET_PROTO_IPV6 DOWNLOAD_PROTO_HTTPS IMAGE_TRUST_CMD
		NSLOOKUP_CMD PING_CMD REBOOT_CMD NTP_CMD PCI_CMD TIME_CMD VLAN_CMD"
	mkdir -p config/local
	local opt; for opt in $general; do
		echo "#define $opt" >> config/local/general.h
	done
	# need to disable OCSP else letsencrypt will fail.
	sed -i '/OCSP_CHECK/d' config/crypto.h
}

build() {
	# DST_Root_CA_X3.pem is need for https (letsencrypt)
	make EMBED="$srcdir/boot.ipxe" \
		CERT="$srcdir"/alpine-netboot-ca.crt,"$srcdir"/DST_Root_CA_X3.pem \
		TRUST="$srcdir"/alpine-netboot-ca.crt,"$srcdir"/DST_Root_CA_X3.pem \
		EXTRA_CFLAGS="-fcommon" \
		NO_WERROR=1 \
		$_targets
}

package() {
	local target; for target in $_targets; do
		install -Dm644 "$builddir"/$target \
			"$pkgdir"/usr/share/$pkgname/${target##*/}
	done
}

_split() {
	local name=${subpkgname##*-}
	name=${name/_/.}
	pkgdesc="$pkgname $name binary"
	install_if="$pkgname=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"/usr/share/$pkgname
	mv "$pkgdir"/usr/share/$pkgname/$name \
		"$subpkgdir"/usr/share/$pkgname
}

sha512sums="
39142187c7b84e81ed95865b2c6ca6624fd10ee49350741bcf4beff47cd4ebdce91eca9d7b4d3d91e45b96bd50fa554fa6b71e8e3ea6bcc474b0a71f5d35e5cd  ipxe-1.20.1.tar.gz
99d6dfbfe3a1a5c3792246c18976d7aab708301a4a1163978a22ae2c3052ca59c10fa5a418d131b783f2c230d78c05aad8f295e87cf82e48479dfb345d8793be  gcc-10.patch
83469bf56d9bd344eb686dbd1cae85c731794c506ac7bbaecdd00d15f7a2497f512ef1364ecccf0f58099d6c4b9ed86eb8614fa0373c262f7a8b5a5bd3460d5a  gcc-11.patch
1f52b62aa9e3ec0656ea3cb5c8e52eda924d58728e64bb03ca6c112858b91b13cf0a9328b105bb232973e240406b240ed14e72b5bd325e82e6fddf3126dbcc58  DST_Root_CA_X3.pem
68030e3f30983ee45c7f740445a246ea0df225becdd0644e06ffdbd84682ac576934dab79fecceb31b70929d81ce85b40301a977862bf7beb042270be21e6d34  alpine-netboot-ca.crt
5a4f1edd0178e696e919e93b90689091f669bffe77ebb9da10d4872d208205fe42b285551f541a07b2067054a2344639052e2b297a582a1f71d907797a77d719  boot.ipxe
"
