SUMMARY="Zstandard, a fast real-time compression algorithm"
DESCRIPTION="Zstd, short for Zstandard, is a fast lossless compression \
algorithm, targeting real-time compression scenarios at zlib-level and better \
compression ratios. The zstd compression library provides in-memory \
compression and decompression functions. It offers a very wide range of \
compression / speed trade-off, while being backed by a very fast decoder. It \
also offers a special mode for small data, called dictionary compression, and \
can create dictionaries from any sample set."
HOMEPAGE="https://facebook.github.io/zstd/"
COPYRIGHT="2016-2017 Facebook, Inc."
LICENSE="BSD (2-clause)"
REVISION="3"
SOURCE_URI="https://github.com/facebook/zstd/archive/v$portVersion.tar.gz"
CHECKSUM_SHA256="980b8febb0118e22f6ed70d23b5b3e600995dbf7489c1f6d6122c1411cdda8d8"
SOURCE_FILENAME="zstd-$portVersion.tar.gz"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="!x86_gcc2 x86"

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

PROVIDES="
	zstd$secondaryArchSuffix = $portVersion
	lib:libzstd$secondaryArchSuffix = $portVersion compat >= 1
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

SUMMARY_bin="Command line tools to compress/decompress .zst files"
PROVIDES_bin="
	zstd${secondaryArchSuffix}_bin = $portVersion
	cmd:gzstd$commandSuffix = $portVersion
	cmd:pzstd$commandSuffix = $portVersion
	cmd:unzstd$commandSuffix = $portVersion
	cmd:zstd$commandSuffix = $portVersion
	cmd:zstdcat$commandSuffix = $portVersion
	cmd:zstdgrep$commandSuffix = $portVersion
	cmd:zstdless$commandSuffix = $portVersion
	"
REQUIRES_bin="
	haiku$secondaryArchSuffix
	zstd$secondaryArchSuffix == $portVersion base
	cmd:grep
	cmd:less
	"

PROVIDES_devel="
	zstd${secondaryArchSuffix}_devel = $portVersion
	devel:libzstd$secondaryArchSuffix = $portVersion compat >= 1
	"
REQUIRES_devel="
	zstd$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:sed
	"

PATCH()
{
	# Fixes to make "make install" work on Haiku.
	sed -i -e '/^ifneq (,$(filter $(shell uname),/ s/))$/ Haiku&/' \
		Makefile \
		lib/Makefile \
		programs/Makefile

	# Insert missing shebang and use double quotes around $@
	sed -i -e 's/ $@ / "$@" /;1i #!/bin/sh' programs/zstdless
}

BUILD()
{
	make PREFIX=$prefix INCLUDEDIR=$includeDir LIBDIR=$libDir
	make -C contrib/pzstd
	make -C contrib/gen_html
	contrib/gen_html/gen_html $portVersion lib/zstd.h zstd_manual.html
}

INSTALL()
{
	make install PREFIX=$prefix LIBDIR=$libDir INCLUDEDIR=$includeDir \
		BINDIR=$commandBinDir MANDIR=$manDir/man1
	make -C contrib/pzstd install PREFIX=$prefix BINDIR=$commandBinDir

	prepareInstalledDevelLib libzstd
	fixPkgconfig

	packageEntries bin \
		$commandBinDir \
		$manDir/man1

	packageEntries devel \
		$developDir

	rmdir $manDir

	install -d $docDir
	install -t $docDir -m 444 LICENSE PATENTS zstd_manual.html
}

TEST()
{
	make test
}
