#!/bin/sh -ue
NAME=e2fsprogs
VERSION=1.47.0
RELEASE=1
SOURCE="https://mirrors.edge.kernel.org/pub/linux/kernel/people/tytso/e2fsprogs/v${VERSION}/e2fsprogs-${VERSION}.tar.gz"
BUILD_STYLE=gmake

build() {
	_llseek_fix="$SRC/e2fsprogs-${VERSION}/lib/blkid/llseek.c"
	if [ -f "$_llseek_fix" ] && ! grep -q "_FILE_OFFSET_BITS+0 == 64" "$_llseek_fix"; then
		msg "patching llseek.c for modern libc..."
		sed -i 's/#if SIZEOF_LONG == SIZEOF_LONG_LONG$/#if SIZEOF_LONG == SIZEOF_LONG_LONG || _FILE_OFFSET_BITS+0 == 64/' "$_llseek_fix"
		sed -i 's/^#define llseek lseek$/#define my_llseek lseek/' "$_llseek_fix"
	fi

	msg "configuring e2fsprogs..."
	./configure \
		--prefix="$PREFIX" \
		--enable-static-libuuid \
		--enable-static-libblkid \
		--disable-shared \
		--disable-uuidd \
		--disable-nls \
		CC="${CC:-cc}" \
		CFLAGS="-O2" \
		LDFLAGS="-static" || die "configure failed"

	msg "building e2fsprogs..."
	gmake -j"$NPROC" || die "gmake failed"

	msg "installing e2fsprogs..."
	gmake DESTDIR="$PKG" install || die "install failed"
	ln -sf mke2fs "$PKG/$PREFIX/bin/mkfs.ext2" 2>/dev/null || true
	ln -sf mke2fs "$PKG/$PREFIX/bin/mkfs.ext3" 2>/dev/null || true
	ln -sf mke2fs "$PKG/$PREFIX/bin/mkfs.ext4" 2>/dev/null || true
}
. ${0%/*}/../../libsh/libdmake.sh
