# Template file for 'gptfdisk'
pkgname=gptfdisk
version=1.0.10
revision=0
short_desc="GPT partitioning tools (sgdisk)"
maintainer="finwo <finwo@pm.me>"
license="GPL-2.0-only"
homepage="https://www.rodsbooks.com/gdisk/"
distfiles="https://downloads.sourceforge.net/project/gptfdisk/gptfdisk/${version}/gptfdisk-${version}.tar.gz"
checksum=2abed61bc6d2b9ec498973c0440b8b804b7a72d7144069b5a9209b2ad693a282
# libstdc++ is a real runtime dep: this is the first C++ package in UNOS.
depends="glibc busybox libstdc++ e2fsprogs popt"
makedepends="e2fsprogs popt libstdc++"

# Notes:
# - Why: installer/install.sh partitions with sgdisk. ONIE provides it on a
#   switch, but without it here a UNOS image cannot install UNOS -- which is
#   what makes the plain qemu image usable as a rescue/installer environment.
# - C++: linked against our packaged libstdc++ (see that template for why we
#   build the runtime from GCC source rather than static-linking it).
# - libuuid comes from e2fsprogs (upstream e2fsprogs defaults it off; our
#   template enables it precisely because this package needs it).
# - Upstream ships a plain Makefile, no configure. It also builds gdisk and
#   cgdisk; cgdisk needs ncurses, which UNOS does not package, so only the
#   non-curses tools are built and only sgdisk is shipped.

do_build() {
	cd "${WRKSRC}"
	# Build against the sysroot so we link our glibc/libuuid/libstdc++ rather
	# than the host's. gdisk/sgdisk/fixparts only; cgdisk needs ncurses.
	make -j"$(nproc)" \
		CXX="g++" \
		CXXFLAGS="-O2 -D_FILE_OFFSET_BITS=64 -I${SYSROOT}/usr/include" \
		LDFLAGS="-L${SYSROOT}/usr/lib -Wl,-rpath-link,${SYSROOT}/usr/lib" \
		sgdisk
}

do_install() {
	cd "${WRKSRC}"
	vmkdir usr/sbin
	vinstall sgdisk 0755 usr/sbin
	vmkdir usr/share/man/man8
	[ -f sgdisk.8 ] && vinstall sgdisk.8 0644 usr/share/man/man8

	[ -x "${DESTDIR}/usr/sbin/sgdisk" ] || die "gptfdisk: no sgdisk"
	# Assert we picked up our own C++ runtime, not a static copy and not the
	# host's: a static libstdc++ would silently violate the shared-only rule.
	readelf -d "${DESTDIR}/usr/sbin/sgdisk" | grep -q 'libstdc++\.so\.6' \
		|| die "gptfdisk: sgdisk does not link libstdc++.so.6 (static C++ runtime?)"
	readelf -d "${DESTDIR}/usr/sbin/sgdisk" | grep -q 'libuuid\.so\.1' \
		|| die "gptfdisk: sgdisk does not link libuuid.so.1"
}
