SUMMARY="World Coordinate System library for astronomical FITS images"
DESCRIPTION="WCSTools is a package of programs and a library of utility \
subroutines for setting and using the world coordinate systems (WCS) in the \
headers of the most common astronomical image formats, FITS and IRAF .imh, \
to relate image pixels to sky coordinates. "
HOMEPAGE="https://heasarc.gsfc.nasa.gov/fitsio/CCfits"
COPYRIGHT="1996-2020 Jessica Mink - Smithsonian Astrophysical Observatory
	1995-2020 Mark Calabretta - Australia Telescope National Facility, CSIRO
	1999-2020 Gentoo Authors"
LICENSE="GNU GPL v2
	GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="http://tdc-www.harvard.edu/software/wcstools/wcstools-$portVersion.tar.gz"
CHECKSUM_SHA256="b2f9be55fdec29f0c640028a9986771bfd6ab3d2f633953e4c7cc3b410e5fe9c"

PATCHES="wcstools-$portVersion.patchset"
ADDITIONAL_FILES="
	configure.ac
	Makefile.am
	Makefile.libwcs.am
	wcstools.pc.in
	"

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

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

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

PROVIDES="
	wcstools$secondaryArchSuffix = $portVersionCompat
	cmd:addpix$commandSuffix = $portVersionCompat
	cmd:bincat$commandSuffix = $portVersionCompat
	cmd:char2sp$commandSuffix = $portVersionCompat
	cmd:conpix$commandSuffix = $portVersionCompat
	cmd:cphead$commandSuffix = $portVersionCompat
	cmd:delhead$commandSuffix = $portVersionCompat
	cmd:delwcs$commandSuffix = $portVersionCompat
	cmd:edhead$commandSuffix = $portVersionCompat
	cmd:filename$commandSuffix = $portVersion
	cmd:fileroot$commandSuffix = $portVersionCompat
	cmd:filext$commandSuffix = $portVersionCompat
	cmd:fixpix$commandSuffix = $portVersionCompat
	cmd:getcol$commandSuffix = $portVersionCompat
	cmd:getfits$commandSuffix = $portVersionCompat
	cmd:gethead$commandSuffix = $portVersionCompat
	cmd:getpix$commandSuffix = $portVersionCompat
	cmd:gettab$commandSuffix = $portVersionCompat
	cmd:i2f$commandSuffix = $portVersionCompat
	cmd:imcat$commandSuffix = $portVersionCompat
	cmd:imextract$commandSuffix = $portVersionCompat
	cmd:imfill$commandSuffix = $portVersionCompat
	cmd:imhead$commandSuffix = $portVersionCompat
	cmd:immatch$commandSuffix = $portVersionCompat
	cmd:imresize$commandSuffix = $portVersionCompat
	cmd:imrot$commandSuffix = $portVersionCompat
	cmd:imsize$commandSuffix = $portVersionCompat
	cmd:imsmooth$commandSuffix = $portVersionCompat
	cmd:imstack$commandSuffix = $portVersionCompat
	cmd:imstar$commandSuffix = $portVersionCompat
	cmd:imwcs$commandSuffix = $portVersionCompat
	cmd:isfile$commandSuffix = $portVersionCompat
	cmd:isfits$commandSuffix = $portVersionCompat
	cmd:isnum$commandSuffix = $portVersionCompat
	cmd:isrange$commandSuffix = $portVersionCompat
	cmd:keyhead$commandSuffix = $portVersionCompat
	cmd:newfits$commandSuffix = $portVersionCompat
	cmd:scat$commandSuffix = $portVersionCompat
	cmd:sethead$commandSuffix = $portVersionCompat
	cmd:setpix$commandSuffix = $portVersionCompat
	cmd:simpos$commandSuffix = $portVersionCompat
	cmd:sky2xy$commandSuffix = $portVersionCompat
	cmd:skycoor$commandSuffix = $portVersionCompat
	cmd:sp2char$commandSuffix = $portVersionCompat
	cmd:subpix$commandSuffix = $portVersionCompat
	cmd:sumpix$commandSuffix = $portVersionCompat
	cmd:wcscrlf$commandSuffix = $portVersionCompat
	cmd:wcsgetdate$commandSuffix = $portVersionCompat
	cmd:wcshead$commandSuffix = $portVersionCompat
	cmd:wcsremap$commandSuffix = $portVersionCompat
	cmd:wcstools$commandSuffix = $portVersionCompat
	cmd:xy2sky$commandSuffix = $portVersionCompat
	lib:libwcstools$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	wcstools${secondaryArchSuffix}_devel = $portVersionCompat
	devel:libwcstools$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	wcstools$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:autoheader
	cmd:automake
	cmd:awk
	cmd:gcc$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:sed
	"

defineDebugInfoPackage wcstools$secondaryArchSuffix \
	"$commandBinDir"/addpix$commandSuffix \
	"$commandBinDir"/bincat$commandSuffix \
	"$commandBinDir"/char2sp$commandSuffix \
	"$commandBinDir"/conpix$commandSuffix \
	"$commandBinDir"/cphead$commandSuffix \
	"$commandBinDir"/delhead$commandSuffix \
	"$commandBinDir"/delwcs$commandSuffix \
	"$commandBinDir"/edhead$commandSuffix \
	"$commandBinDir"/filename$commandSuffix \
	"$commandBinDir"/fileroot$commandSuffix \
	"$commandBinDir"/filext$commandSuffix \
	"$commandBinDir"/fixpix$commandSuffix \
	"$commandBinDir"/getcol$commandSuffix \
	"$commandBinDir"/getfits$commandSuffix \
	"$commandBinDir"/gethead$commandSuffix \
	"$commandBinDir"/getpix$commandSuffix \
	"$commandBinDir"/gettab$commandSuffix \
	"$commandBinDir"/i2f$commandSuffix \
	"$commandBinDir"/imcat$commandSuffix \
	"$commandBinDir"/imextract$commandSuffix \
	"$commandBinDir"/imfill$commandSuffix \
	"$commandBinDir"/imhead$commandSuffix \
	"$commandBinDir"/immatch$commandSuffix \
	"$commandBinDir"/imresize$commandSuffix \
	"$commandBinDir"/imrot$commandSuffix \
	"$commandBinDir"/imsize$commandSuffix \
	"$commandBinDir"/imsmooth$commandSuffix \
	"$commandBinDir"/imstack$commandSuffix \
	"$commandBinDir"/imstar$commandSuffix \
	"$commandBinDir"/imwcs$commandSuffix \
	"$commandBinDir"/isfile$commandSuffix \
	"$commandBinDir"/isfits$commandSuffix \
	"$commandBinDir"/isnum$commandSuffix \
	"$commandBinDir"/isrange$commandSuffix \
	"$commandBinDir"/keyhead$commandSuffix \
	"$commandBinDir"/newfits$commandSuffix \
	"$commandBinDir"/scat$commandSuffix \
	"$commandBinDir"/sethead$commandSuffix \
	"$commandBinDir"/setpix$commandSuffix \
	"$commandBinDir"/simpos$commandSuffix \
	"$commandBinDir"/sky2xy$commandSuffix \
	"$commandBinDir"/skycoor$commandSuffix \
	"$commandBinDir"/sp2char$commandSuffix \
	"$commandBinDir"/subpix$commandSuffix \
	"$commandBinDir"/sumpix$commandSuffix \
	"$commandBinDir"/wcscrlf$commandSuffix \
	"$commandBinDir"/wcsgetdate$commandSuffix \
	"$commandBinDir"/wcshead$commandSuffix \
	"$commandBinDir"/wcsremap$commandSuffix \
	"$commandBinDir"/xy2sky$commandSuffix \
	"$libDir"/libwcstools.so.$libVersion

PATCH()
{
	sed -i \
		-e 's/getdate/wcsgetdate/' \
		-e 's/crlf/wcscrlf/' \
		-e 's/remap/wcsremap/' \
		-e "s/3.... Programs/${portVersion} Programs/" \
		wcstools
}

BUILD()
{
	for f in "$portDir"/additional-files/{configure.ac,wcstools.pc.in,Makefile.am}; do
		cp ${f} .
	done
	cp "$portDir"/additional-files/Makefile.libwcs.am libwcs/Makefile.am

	mkdir -p m4

	libtoolize --force --copy --install
	aclocal --force --install
	autoheader --force
	autoconf --force
	automake --add-missing --force-missing

	runConfigure --omit-dirs binDir ./configure --bindir="$commandBinDir"
	make $jobArgs
}

INSTALL()
{
	make install

	rm -f $libDir/libwcstools.la*

	prepareInstalledDevelLib libwcstools
	fixPkgconfig

	# copy man documentation
	mkdir -p $manDir/man1
	cp -af man/man1/*.1 $manDir/man1

	# devel package
	packageEntries devel $developDir
}

TEST()
{
	# WIP
	newfits -a 10 -j 248 41 -p 0.15 test.fits
	sethead test.fits A=1 B=1
	[[ "$(./gethead test.fits RA)" == "16:32:00.0000" ]] || exit 1
	rm test.fits
}
