# Co-Maintainer: Clayton Craft <clayton@craftyguy.net>
maintainer="Oliver Smith <ollieparanoid@postmarketos.org>"
pkgname=postmarketos-mkinitfs
pkgver=2.11.1
pkgrel=0
pkgdesc="Tool to generate initramfs images for postmarketOS"
url="https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs"
depends="
	boot-deploy>=0.23.0
"
makedepends_build="go scdoc"
options="pmb:cross-native2"
replaces="mkinitfs"

triggers="$pkgname.trigger=\
/lib/firmware/*:\
/usr/lib/systemd/boot:\
/usr/lib/udev:\
/usr/libexec/pmos-tests-initramfs:\
/usr/share/deviceinfo:\
/usr/share/kernel/*:\
/usr/share/mkinitfs-triggers:\
/usr/share/mkinitfs/*:\
/usr/share/plymouth:\
"

# mkinitfs-vendor-$pkgver.tar.gz: vendored Go deps, is part of the release:
# https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/releases
source="
	https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/archive/$pkgver/postmarketos-mkinitfs-$pkgver.tar.gz
	https://gitlab.postmarketos.org/api/v4/projects/postmarketOS%2Fpostmarketos-mkinitfs/packages/generic/mkinitfs-vendor-$pkgver/$pkgver/mkinitfs-vendor-$pkgver.tar.gz
	"

install="$pkgname.post-upgrade"
arch="all"
license="GPL-2.0-or-later"
provider_priority=999  # higher priority than Alpine's mkinitfs
provides="initramfs-generator"
subpackages="$pkgname-doc"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOPATH="$srcdir"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"

prepare() {
	default_prepare
	ln -s "$srcdir"/vendor "$builddir"/vendor
}

build() {
	unset LDFLAGS  # passed to Go as linker flags, which are invalid

	# * Set DISABLE_GOGC=1 for e.g. aarch64 -> x86_64 for:
	#   https://gitlab.com/qemu-project/qemu/-/issues/2560
	# * Set CGO_ENABLED=1 to fix "-buildmode=pie requires external (cgo)
	#   linking, but cgo is not enabled" when cross compiling with
	#   cross-native2 from e.g. x86_64 to (armv7,riscv64,armhf). See also:
	#   https://gitlab.alpinelinux.org/alpine/aports/-/issues/15809
	case "$CARCH" in
	x86_64)
		make VERSION="$pkgver" DISABLE_GOGC=1 CGO_ENABLED=1;;
	*)
		make VERSION="$pkgver" CGO_ENABLED=1;;
	esac
}

package() {
	make PREFIX=/usr DESTDIR="$pkgdir" install
}

check() {
	go test ./...
}

sha512sums="
a57360095b71e5e215606ade0174ee30f13a7df9190de55f42b96c44b0f08069fa33d2f64767fa859282e8e05c11f1bd571625a231f27ee7b85d3089c85db1fe  postmarketos-mkinitfs-2.11.1.tar.gz
f0b82ceef43f8f5a706e9a2cdc17be4a553cb043c1ee35fa49799b4d0ed85f4c2f4d80af31519c10d1ec4273546622ba5daedf989c38d5a11dc49c09c033afc1  mkinitfs-vendor-2.11.1.tar.gz
"
