SUMMARY="A high-performance asynchronous HTTP client library"
DESCRIPTION="The serf library is a C-based HTTP client library built upon the \
Apache Portable Runtime (APR) library. It multiplexes connections, running \
the read/write communication asynchronously. Memory copies and \
transformations are kept to a minimum to provide high performance operation."
HOMEPAGE="https://serf.apache.org/"
COPYRIGHT="2002-2019 Justin Erenkrantz and Greg Stein"
LICENSE="Apache v2"
REVISION="9"
SOURCE_URI="https://archive.apache.org/dist/serf/serf-$portVersion.tar.bz2"
CHECKSUM_SHA256="549c2d21c577a8a9c0450facb5cca809f26591f048e466552240947bdf7a87cc"
PATCHES="
	serf-$portVersion.patchset
	0001-Fix-syntax-of-a-print-in-the-scons-file-to-unbreak-b.patch
	0002-SConstruct-Fix-path-quoting-for-.def-generator.patch
	0003-gen_def.patch
	0004-Follow-up-to-r1811083-fix-building-with-scons-3.0.0-.patch
	"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

PROVIDES="
	serf$secondaryArchSuffix = $portVersion
	lib:libserf_1$secondaryArchSuffix = 1.3.0 compat >= 1
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libapr_1$secondaryArchSuffix
	lib:libaprutil_1$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix # aprutil brings us
	lib:libiconv$secondaryArchSuffix # aprutil brings us
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	serf${secondaryArchSuffix}_devel = $portVersion
	devel:libserf_1$secondaryArchSuffix = 1.3.0 compat >= 1
	"
REQUIRES_devel="
	serf$secondaryArchSuffix == $portVersion base
	devel:libapr_1$secondaryArchSuffix
	devel:libaprutil_1$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libapr_1$secondaryArchSuffix
	devel:libaprutil_1$secondaryArchSuffix
	devel:libcrypto$secondaryArchSuffix
	devel:libexpat$secondaryArchSuffix # aprutil brings us
	devel:libiconv$secondaryArchSuffix # aprutil brings us
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:apr_1_config$secondaryArchSuffix
	cmd:apu_1_config$secondaryArchSuffix
	cmd:gcc$secondaryArchSuffix
	#cmd:make
	cmd:scons
	"

BUILD()
{
	archDir=
	if [ -n "$secondaryArchSuffix" ]; then
		archDir=/$effectiveTargetArchitecture
	fi
	apr=$portPackageLinksDir/cmd~apr_1_config$secondaryArchSuffix/bin$archDir/apr-1-config
	apu=$portPackageLinksDir/cmd~apu_1_config$secondaryArchSuffix/bin$archDir/apu-1-config
	gcc=$portPackageLinksDir/cmd~gcc$secondaryArchSuffix/bin$archDir/gcc
	scons OPENSSL=$portPackageLinksDir/devel~libssl$secondaryArchSuffix \
		ZLIB=/system APR=$apr APU=$apu PREFIX=$prefix CC=$gcc
}

INSTALL()
{
	configDir=
	if [ -n "$secondaryArchSuffix" ]; then
		configDir=/$effectiveTargetArchitecture
	fi
	apr=$portPackageLinksDir/cmd~apr_1_config$secondaryArchSuffix/bin$configDir/apr-1-config
	apu=$portPackageLinksDir/cmd~apu_1_config$secondaryArchSuffix/bin$configDir/apu-1-config
	scons install OPENSSL=$portPackageLinksDir/devel~libssl$secondaryArchSuffix \
		ZLIB=/system APR=$apr APU=$apu PREFIX=$prefix

	mkdir -p $includeDir $developLibDir
	mv $prefix/include/serf-1/* $includeDir/
	rm -rf $prefix/include

	if [ -n "$secondaryArchSuffix" ]; then
		mkdir -p $libDir
		mv $prefix/lib/libserf* $prefix/lib/pkgconfig $libDir/
	fi

	# prepare development lib links
	prepareInstalledDevelLib libserf-1

	fixPkgconfig

	# The pkgconfig file reference other libraries using the wrong paths, which
	# creates a lot of confusion. Fix them so correct paths are used.
	local develPackageName="${portName}_devel-$portFullVersion"
	local packageLinksDir=$(dirname $portPackageLinksDir)
	local linksDir="$packageLinksDir/${develPackageName}/devel~libaprutil_1$secondaryArchSuffix/$relativeDevelopLibDir"
	sed -i -e "s,^\(Libs.private.*\)-L.* \(-laprutil.*\)$,\1-L$linksDir \2," \
		$developLibDir/pkgconfig/serf-1.pc
	local linksDir="$packageLinksDir/${develPackageName}/devel~libapr_1$secondaryArchSuffix/$relativeDevelopLibDir"
	sed -i -e "s,\-L/packages/apr-1.* \(-lapr-1 .*\)$,-L$linksDir \1," \
		$developLibDir/pkgconfig/serf-1.pc


	# devel package
	packageEntries devel $developDir
}
