# Contributor: Edd Salkield <edd@salkield.uk>
# Maintainer: Edd Salkield <edd@salkield.uk>
pkgname=hdf4
pkgver=4.2.15
pkgrel=2
pkgdesc="Data model, library, and file format for storing and managing data"
url="https://www.hdfgroup.org/solutions/hdf4/"
arch="all !riscv64" # ftbfs
license="custom"
options="!check" # Upstream tests appear broken
depends_dev="zlib-dev jpeg-dev libtirpc-dev"
makedepends="$depends_dev bison flex libaec-dev chrpath"
checkdepends="diffutils" # Flags not supported by busybox diff are used
subpackages="$pkgname-dev $pkgname-doc $pkgname-tools"
# Patches from Fedora:  https://src.fedoraproject.org/rpms/hdf/tree/rawhide
# NB: Some of these patches depend on each other in the given order
source="https://support.hdfgroup.org/ftp/HDF/releases/HDF$pkgver/src/hdf-$pkgver.tar.bz2
	10-hdf-ppc.patch
	20-hdf-4.2.4-sparc.patch
	30-hdf-s390.patch
	40-hdf-arm.patch
	50-hdf-aarch64.patch
	60-hdf-ppc64le.patch
	70-hdf-loongarch64.patch
	gcc14.patch"
builddir="$srcdir"/hdf-"$pkgver"

prepare() {
	default_prepare
	update_config_sub
	update_config_guess
}

build() {
	# This flag would required to treat Fortran argument mismatch as a warning,
	# not an error.
	# This got fixed in master https://github.com/HDFGroup/hdf4/issues/1
	# but isn't fixed yet in the latest release.
	# > export FFLAGS="$FFLAGS -fallow-argument-mismatch"
	# However, --enable-shared currently requires that fortran is disabled, so
	# it doesn't matter anyway
	export CFLAGS="$CFLAGS -Wno-implicit-function-declaration -Wno-implicit-int -Wno-incompatible-pointer-types"
	export CPPFLAGS="$CPPFLAGS -I/usr/include/tirpc"
	export LIBS="$LIBS -ltirpc"

	# We disable netcdf since it's provided by the netcdf-* packages
	./configure \
		--enable-shared \
		--disable-static \
		--disable-fortran \
		--disable-netcdf \
		--with-szlib \
		--prefix=/usr \
		--build=$CBUILD \
		--host=$CHOST
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install-recursive
	install -Dm644 COPYING "$pkgdir"/usr/share/licenses/"$pkgname"/COPYING
	chrpath -d \
		"$pkgdir"/usr/lib/libmfhdf.so.0.0.0 \
		"$pkgdir"/usr/lib/libdf.so.0.0.0 \
		"$pkgdir"/usr/bin/gif2hdf \
		"$pkgdir"/usr/bin/hdf24to8 \
		"$pkgdir"/usr/bin/hdf8to24 \
		"$pkgdir"/usr/bin/hdfimport \
		"$pkgdir"/usr/bin/hdftopal \
		"$pkgdir"/usr/bin/hdiff \
		"$pkgdir"/usr/bin/jpeg2hdf \
		"$pkgdir"/usr/bin/paltohdf \
		"$pkgdir"/usr/bin/vmake \
		"$pkgdir"/usr/bin/hdf2gif \
		"$pkgdir"/usr/bin/hdfcomp \
		"$pkgdir"/usr/bin/hdfls \
		"$pkgdir"/usr/bin/hdftor8 \
		"$pkgdir"/usr/bin/hdp \
		"$pkgdir"/usr/bin/ncdump \
		"$pkgdir"/usr/bin/r8tohdf \
		"$pkgdir"/usr/bin/vshow \
		"$pkgdir"/usr/bin/hdf2jpeg \
		"$pkgdir"/usr/bin/hdfed \
		"$pkgdir"/usr/bin/hdfpack \
		"$pkgdir"/usr/bin/hdfunpac \
		"$pkgdir"/usr/bin/hrepack \
		"$pkgdir"/usr/bin/ncgen \
		"$pkgdir"/usr/bin/ristosds
}

dev() {
	default_dev
	amove usr/lib/libhdf4.settings
}

doc() {
	default_doc
	# Remove conflicts with netcdf-doc
	rm "$subpkgdir"/usr/share/man/man1/ncdump.1.gz "$subpkgdir"/usr/share/man/man1/ncgen.1.gz
}

tools() {
	mkdir -p "$subpkgdir"/usr
	amove usr/bin
	# Remove conflicts with netcdf-utils
	rm "$subpkgdir"/usr/bin/ncgen "$subpkgdir"/usr/bin/ncdump
}

sha512sums="
7d4037800ef8950ed74f225355ef5458444bed26fc189fbbd2ce7d36009998013ac1f6ba5d4e60834acf8f6f73550357238745acc35e162679919400a48191e9  hdf-4.2.15.tar.bz2
c3098bf0c937c406ed133ea94c7b4bb7fb7b78aa37e1fae0402f74e3aed8db79a13a5d0baf6b0cb78835984794e795a51d54b690115a390baf9b89292fd587ad  10-hdf-ppc.patch
751ef4c537f4a7d50dae95eb3c9879848eada5c1ae80b9906b62b26b77496b42dfe7dd6ffb8359a986e2c0630b46ca59d797864fe93645ddb2923d65f6ca9bcc  20-hdf-4.2.4-sparc.patch
2b3c04bfa20f1067852538a6593de2659ee480b3f40dc51931989a175b99394ae67f1d3445f697486801d7a9d19a4e10cf10413fb6ee28126cebf0e1379f1447  30-hdf-s390.patch
b9ea7c626fcf1bfbf75e05459794fc577a28602749464f2eea029fc4d729ceeb96f93273cc9ea8988878444632532888970047370b787b9c4bdea0b0077ff1b3  40-hdf-arm.patch
fac62b5ab9ceaf9ddd9cc4831994cdf5cc4e0b2f85ee9f943f7311b0acc2b78d18f49e194a31b7ddd0c17dc87a14ec55d6af73258fbc2f0f2e2b86b7f9048f25  50-hdf-aarch64.patch
824be68ce54c43ea8e556cd9836b8d18162e3a214d532ce40ca7de600693da89dcbc364aae5995c2cd455126550d88606889c5e1c956c4cb9f1928566c705121  60-hdf-ppc64le.patch
9e3c9f726c2ee9bd29a70211c91a5b6afa6bcc26708cfa2fe844ba2f3a2e69587cff028cca73febe83dd8fea62ad9abee52c301de301e2d9f4d77dbfdee8f293  70-hdf-loongarch64.patch
8c07bb9c0a185720879d24037f252686686133ebd4fed1e36caddc36e4be40f894b2a56c41fc4ffb09dae14f013a66bf1bbbae1980ab2c9aef86e7a53ec5f0e3  gcc14.patch
"
