# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=e2fsprogs
pkgver=1.47.1
pkgrel=0
pkgdesc="Standard Ext2/3/4 filesystem utilities"
url="https://e2fsprogs.sourceforge.net/"
arch="all"
license="GPL-2.0-or-later AND LGPL-2.0-or-later AND BSD-3-Clause AND MIT"
depends_dev="util-linux-dev gawk"
# XXX: build hack to upgrade live on builders
makedepends="$depends_dev musl>1.2.3_git20230322-r2 linux-headers fuse3-dev"
checkdepends="diffutils perl coreutils"
subpackages="
	$pkgname-static
	$pkgname-dev
	libcom_err
	fuse2fs
	fuse2fs-doc:fuse2fs_doc:noarch
	$pkgname-doc
	$pkgname-libs
	$pkgname-extra
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/tytso/e2fsprogs/archive/v$pkgver.tar.gz
	"

# secfixes:
#   1.45.5-r0:
#     - CVE-2019-5188
#   1.45.4-r0:
#     - CVE-2019-5094

build() {
	CFLAGS="$CFLAGS -flto=auto" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--mandir=/usr/share/man \
		--enable-elf-shlibs \
		--enable-symlink-install \
		--disable-fsck \
		--disable-uuidd \
		--disable-libuuid \
		--disable-libblkid \
		--disable-nls
	make
}

check() {
	# Disable failing test
	rm -rf tests/m_offset*

	# Failing on CI
	rm -rf tests/f_bigalloc_orphan_list*

	# Fails on armhf on CI
	rm -rf tests/f_extent_htree*

	# Fails on builders with less than 4TB free space
	rm -rf tests/m_hugefile

	make check
}

package() {
	make -j1 DESTDIR="$pkgdir" install install-libs
	mv "$pkgdir"/sbin/* "$pkgdir"/usr/sbin/
	local i; for i in e2fsck mke2fs mkfs.* fsck.*; do
		mv "$pkgdir"/usr/sbin/$i "$pkgdir"/sbin/
	done
}

dev() {
	default_dev

	amove \
		usr/bin/compile_et \
		usr/bin/mk_cmds \
		usr/share/et \
		usr/share/ss
}

libcom_err() {
	pkgdesc="Common error description library"

	amove lib/libcom_err*
}

fuse2fs() {
	pkgdesc="Ext2/3/4 filesystem driver for FUSE"
	amove usr/bin/fuse2fs
}

fuse2fs_doc() {
	pkgdesc="Ext2/3/4 filesystem driver for FUSE (documentation)"
	install_if="fuse2fs=$pkgver-r$pkgrel docs"

	amove usr/share/man/man1/fuse2fs.1
	gzip -9 "$subpkgdir"/usr/share/man/man1/fuse2fs.1
}

extra() {
	depends="$pkgname=$pkgver-r$pkgrel"
	pkgdesc="Ext2/3/4 filesystem extra utilities"

	mkdir -p "$subpkgdir"
	amove usr
	rmdir "$pkgdir"/lib || true
}

sha512sums="
7e59746e14df9e29034693f415dc4bcbd405217712e2d9e594dcc755bbc1c9f3c4f66ba37e4289b022c337c5aae3d96561c0cd88fa21082ed1017d2a000d3b82  e2fsprogs-1.47.1.tar.gz
"
