# Template file for 'e2fsprogs'
pkgname=e2fsprogs
version=1.47.4
revision=0
short_desc="ext2/3/4 filesystem utilities and libraries"
maintainer="finwo <finwo@pm.me>"
license="GPL-2.0-only, LGPL-2.0-only, BSD-3-Clause, MIT"
homepage="https://e2fsprogs.sourceforge.net/"
distfiles="https://www.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v${version}/e2fsprogs-${version}.tar.xz"
checksum=fd5bf388cbdbe006a3d3b318d983b2948382440acc85a87f1e7d108653e8db0b
depends="glibc busybox"

# Notes:
# - Why this exists: without e2fsck a switch cannot repair its own root after
#   an unclean shutdown, which is not a recoverable position. mke2fs is also
#   what installer/install.sh formats the root partition with, so shipping this
#   is what lets a UNOS image install UNOS instead of borrowing host binaries.
# - libuuid/libblkid: upstream defaults these OFF, expecting util-linux to
#   provide them. We do not package util-linux, and gptfdisk needs libuuid, so
#   we build e2fsprogs' own copies and they become the canonical providers.
# - --with-root-prefix=/usr keeps everything under /usr/{sbin,lib} rather than
#   the historical /sbin + /lib split, matching glibc's --libdir=/usr/lib.
# - Busybox already provides a cut-down blkid/findfs; those applets stay as
#   they are. The real blkid here ships as a library, not as a second binary,
#   to avoid an apk file conflict (apk refuses cross-package overwrites).

do_configure() {
	cd "${WRKSRC}"
	# --sysconfdir=/etc is load-bearing: with a bare --prefix=/usr, autotools
	# puts mke2fs.conf in /usr/etc, but mke2fs only ever reads /etc/mke2fs.conf.
	# It would silently fall back to built-in defaults and ignore our config.
	./configure \
		--prefix=/usr \
		--with-root-prefix=/usr \
		--sysconfdir=/etc \
		--enable-elf-shlibs \
		--disable-static \
		--enable-libuuid \
		--enable-libblkid \
		--disable-uuidd \
		--disable-fsck \
		--disable-debugfs \
		--disable-nls \
		--disable-rpath \
		--disable-testio-debug
}

do_build() {
	cd "${WRKSRC}"
	make -j"$(nproc)"
}

do_install() {
	cd "${WRKSRC}"
	make install DESTDIR="${DESTDIR}"
	make install-libs DESTDIR="${DESTDIR}"

	# Sysroot seed: gptfdisk and smartmontools build against libuuid.
	make install DESTDIR="${SYSROOT}"
	make install-libs DESTDIR="${SYSROOT}"

	# Shared-only (packages/README.md): no .a, no libtool turds.
	rm -f "${DESTDIR}"/usr/lib/*.a "${DESTDIR}"/usr/lib/*.la
	rm -f "${SYSROOT}"/usr/lib/*.a "${SYSROOT}"/usr/lib/*.la

	# `blkid` and `findfs` would collide with the busybox applets of the same
	# name; apk refuses cross-package file overwrites, so drop ours.
	rm -f "${DESTDIR}"/usr/sbin/blkid "${DESTDIR}"/usr/sbin/findfs
	rm -f "${DESTDIR}"/usr/share/man/man8/blkid.8 \
	      "${DESTDIR}"/usr/share/man/man8/findfs.8

	# No docs/locale in the image.
	rm -rf "${DESTDIR}/usr/share/info" "${DESTDIR}/usr/share/locale"

	# e2scrub drives online fsck of LVM snapshots via cron and udev. UNOS has
	# none of those three, so the scripts could only ever fail confusingly.
	rm -f  "${DESTDIR}"/usr/sbin/e2scrub "${DESTDIR}"/usr/sbin/e2scrub_all
	rm -f  "${DESTDIR}"/etc/e2scrub.conf
	rm -rf "${DESTDIR}"/etc/cron.d "${DESTDIR}"/usr/libexec
	rm -f  "${DESTDIR}"/usr/share/man/man8/e2scrub.8 \
	       "${DESTDIR}"/usr/share/man/man8/e2scrub_all.8 \
	       "${DESTDIR}"/usr/share/man/man5/e2scrub.conf.5
	# udev rules: no udev (devtmpfs + mdev territory), and upstream hardcodes
	# a lib64 path that does not match our /usr/lib layout either way.
	rm -rf "${DESTDIR}"/usr/lib64 "${DESTDIR}"/usr/lib/udev
	# initrd helper: UNOS boots without an initrd by design (see grub.cfg.in).
	rm -f  "${DESTDIR}"/usr/lib/e2initrd_helper

	# compile_et/mk_cmds are build-time code generators for downstream
	# consumers; they belong in the sysroot, not on a switch.
	rm -f  "${DESTDIR}"/usr/bin/compile_et "${DESTDIR}"/usr/bin/mk_cmds
	rm -rf "${DESTDIR}"/usr/share/et "${DESTDIR}"/usr/share/ss
	rm -f  "${DESTDIR}"/usr/share/man/man1/compile_et.1 \
	       "${DESTDIR}"/usr/share/man/man1/mk_cmds.1

	# The whole point of the package: assert the tools the installer needs.
	for t in mke2fs e2fsck resize2fs tune2fs dumpe2fs; do
		[ -x "${DESTDIR}/usr/sbin/${t}" ] || die "e2fsprogs: missing ${t}"
	done
	# mkfs.ext4 is a symlink to mke2fs; installer/install.sh calls it by name.
	[ -e "${DESTDIR}/usr/sbin/mkfs.ext4" ] || die "e2fsprogs: no mkfs.ext4"
	[ -e "${DESTDIR}/usr/sbin/fsck.ext4" ] || die "e2fsprogs: no fsck.ext4"
	[ -f "${SYSROOT}/usr/include/uuid/uuid.h" ] || die "e2fsprogs: no sysroot uuid.h"
	[ -f "${SYSROOT}/usr/lib/pkgconfig/uuid.pc" ] || die "e2fsprogs: no sysroot uuid.pc"
	# mke2fs silently uses built-in defaults if this is not where it looks.
	[ -f "${DESTDIR}/etc/mke2fs.conf" ] || die "e2fsprogs: mke2fs.conf not in /etc"
	[ ! -d "${DESTDIR}/usr/etc" ] || die "e2fsprogs: stray /usr/etc"
	[ ! -d "${DESTDIR}/usr/lib64" ] || die "e2fsprogs: stray /usr/lib64"
}
