SUMMARY="A generic-purpose lossless compression algorithm"
DESCRIPTION="A generic-purpose lossless compression algorithm that \
compresses data using a combination of a modern variant of the LZ77 algorithm, \
Huffman coding and 2nd order context modeling, with a compression ratio \
comparable to the best currently available general-purpose compression \
methods. It is similar in speed with deflate but offers more dense compression."
HOMEPAGE="https://github.com/google/brotli"
COPYRIGHT="2009, 2010, 2013-2018 Brotli Authors"
LICENSE="MIT"
REVISION="2"
SOURCE_URI="https://github.com/google/brotli/archive/v$portVersion.tar.gz"
# Testdata missing in Tarball, see https://github.com/google/brotli/issues/850
# For tests 1/ use the git URL
# 2/ add --worktree-attributes to 'git archive' in HaikuPorter/SourceFetcher.py
#SOURCE_URI="git+${HOMEPAGE}.git#v$portVersion"
CHECKSUM_SHA256="f9e8d81d0405ba66d181529af42a3354f838c939095ff99930da6aa9cdf6fe46"
SOURCE_FILENAME="brotli-$portVersion.tar.gz"
PATCHES="brotli-$portVersion.patchset"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86"

# For now the library will follow $portVersion,
# but upstream said that will change in the future.
libVersion="$portVersion"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

PROVIDES="
	brotli$secondaryArchSuffix = $portVersion
	cmd:brotli$secondaryArchSuffix = $portVersion
	lib:libbrotlicommon$secondaryArchSuffix = $libVersionCompat
	lib:libbrotlidec$secondaryArchSuffix = $libVersionCompat
	lib:libbrotlienc$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	brotli${secondaryArchSuffix}_devel = $portVersion
	devel:libbrotlicommon$secondaryArchSuffix = $libVersionCompat
	devel:libbrotlicommon_static$secondaryArchSuffix = $libVersion
	devel:libbrotlidec$secondaryArchSuffix = $libVersionCompat
	devel:libbrotlidec_static$secondaryArchSuffix = $libVersion
	devel:libbrotlienc$secondaryArchSuffix = $libVersionCompat
	devel:libbrotlienc_static$secondaryArchSuffix = $libVersion
	"
REQUIRES_devel="
	brotli$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:g++$secondaryArchSuffix
	cmd:gcc$secondaryArchSuffix
	cmd:make
	"

PYTHON_PACKAGES=()
PYTHON_VERSIONS=()
PYTHON_LIBSUFFIXES=()
# We don't have python2 for secondaryArch,
if [ -z "$secondaryArchSuffix" ]; then
	PYTHON_PACKAGES+=(python)
	PYTHON_VERSIONS+=(2.7)
	PYTHON_LIBSUFFIXES+=("")
	BUILD_REQUIRES+="
		setuptools_python
		"
	BUILD_PREREQUIRES+="
		cmd:python2
		"
fi
# gcc2 does not support the flags passed by python3
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	PYTHON_PACKAGES+=(python3)
	PYTHON_VERSIONS+=(3.7)
	PYTHON_LIBSUFFIXES+=(m)
	BUILD_REQUIRES+="
		setuptools_python3
		"
	BUILD_PREREQUIRES+="
		cmd:python3
		"
fi
for i in "${!PYTHON_PACKAGES[@]}"; do
	pythonPackage=${PYTHON_PACKAGES[i]}
	pythonVersion=${PYTHON_VERSIONS[$i]}
	pythonLibSuffix=${PYTHON_LIBSUFFIXES[$i]}

	eval "PROVIDES_${pythonPackage}=\"
		${portName}_$pythonPackage = $portVersion
		\""
	eval "REQUIRES_$pythonPackage=\"
		haiku$secondaryArchSuffix
		lib:libpython$pythonVersion$pythonLibSuffix$secondaryArchSuffix
		\""
done

BUILD()
{
	runConfigure --omit-buildspec ./configure-cmake --disable-debug
	make $jobArgs
}

INSTALL()
{
	make install

	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonPackage=${PYTHON_PACKAGES[i]}
		pythonVersion=${PYTHON_VERSIONS[$i]}

		python=python$pythonVersion
		installLocation=$prefix/lib/$python/vendor-packages/
		export PYTHONPATH=$installLocation:$PYTHONPATH
		mkdir -p $installLocation
		rm -rf build
		$python setup.py build install \
			--root=/ --prefix=$prefix

		packageEntries $pythonPackage \
			"$prefix"/lib/$python
	done

	install -d -m 755 "$manDir"/man{1,3}
	install -t "$manDir/man1" docs/brotli.1
	install -t "$manDir/man3" docs/*.3

	prepareInstalledDevelLibs libbrotli{common,dec,enc}{,-static}
	fixPkgconfig

	packageEntries devel \
		"$manDir/man3" \
		"$developDir"
}

TEST()
{
	make test

	for i in "${!PYTHON_PACKAGES[@]}"; do
		pythonVersion=${PYTHON_VERSIONS[$i]}

		python=python$pythonVersion
		rm -rf build
		$python setup.py test
		rm python/_brotli.so
	done
}
