SUMMARY="A FITS File Subroutine Library"
DESCRIPTION="CFITSIO is a library of ANSI C routines for reading and writing \
FITS format data files. A set of Fortran-callable wrapper routines are also \
included for the convenience of Fortran programmers."
HOMEPAGE="https://heasarc.gsfc.nasa.gov/docs/software/fitsio/fitsio.html"
COPYRIGHT="1996-2019 William D. Pence - HEASARC, NASA/GSFC"
LICENSE="CFITSIO"
REVISION="1"
SOURCE_URI="http://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/cfitsio-$portVersion.tar.gz"
CHECKSUM_SHA256="5b65a20d5c53494ec8f638267fca4a629836b7ac8dd0ef0266834eab270ed4b3"
PATCHES="cfitsio-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2 ?arm ?ppc ?sparc"
SECONDARY_ARCHITECTURES="x86"

commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

portVersionCompat="$portVersion compat >= ${portVersion%%.*}"
libVersion="9.3.49"
libVersionCompat="$libVersion compat >= ${libVersion%.*.*}"

PROVIDES="
	cfitsio$secondaryArchSuffix = $portVersionCompat
	lib:libcfitsio$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libgfortran$secondaryArchSuffix
	lib:libnghttp2$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	cfitsio${secondaryArchSuffix}_devel = $portVersionCompat
	devel:libcfitsio$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	cfitsio$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libgfortran$secondaryArchSuffix
	lib:libnghttp2$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_utils="
	cfitsio${secondaryArchSuffix}_utils = $portVersionCompat
	cmd:fpack$commandSuffix = $portVersionCompat
	cmd:funpack$commandSuffix = $portVersionCompat
	"
REQUIRES_utils="
	cfitsio$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libbz2$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoreconf
	cmd:awk
	cmd:cmp
	cmd:gcc$secondaryArchSuffix
	cmd:gfortran$secondaryArchSuffix
	cmd:make
	"

TEST_REQUIRES="
	cmd:diff
	"

defineDebugInfoPackage cfitsio$secondaryArchSuffix \
	"$libDir"/libcfitsio.so.$libVersion

BUILD()
{
	WITHSSE=""
	if [ $effectiveTargetArchitecture == x86_64 ]; then
		GCC5FLAGS="--enable-x86-64"
		WITHSSE="--enable-ssse3"
	fi

	sed -e 's|LDFLAGS=.*|LDFLAGS="$LDFLAGS"|g' -i configure.in

	autoreconf -vfi
	runConfigure --omit-dirs binDir ./configure \
		--bindir="$commandBinDir" \
		--enable-reentrant \
		--with-bzip2 $WITHSSE

	make $jobArgs
	make utils $jobArgs
}

INSTALL()
{
	make install

	rm -f $libDir/libcfitsio.a

	prepareInstalledDevelLib libcfitsio
	fixPkgconfig

	# copy pdf documentation
	mkdir -p $developDocDir
	cp -rd docs/*.pdf $developDocDir/

	# devel package
	packageEntries devel $developDir

	# util package
	packageEntries utils $commandBinDir
}

TEST()
{
	export LIBRARY_PATH=".:$LIBRARY_PATH"
	make testprog
	testprog > testprog.lis
	diff testprog.lis testprog.out
	cmp testprog.fit testprog.std
}
