SUMMARY="Zstandard, a fast real-time compression algorithm"
SUMMARY_bin="Command line tools to compress/decompress .zst files"
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-2018 Facebook, Inc."
LICENSE="BSD (2-clause)
	GNU GPL v2"
REVISION="1"
SOURCE_URI="https://github.com/facebook/zstd/archive/v$portVersion.tar.gz"
SOURCE_FILENAME="zstd-$portVersion.tar.gz"
CHECKSUM_SHA256="734d1f565c42f691f8420c8d06783ad818060fc390dee43ae0a89f86d0a4f8c2"
PATCHES="zstd-$portVersion.patchset"
if [ "$effectiveTargetArchitecture" = x86_gcc2 ]; then
PATCHES="$PATCHES
	zstd-$portVersion-gcc2.patchset
	"
fi

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ -n "$secondaryArchSuffix" ]; then
# Comment out this block if you wish to co-install zstd_bin & e.g. zstd_x86_bin.
	commandSuffix=
	commandBinDir=$prefix/bin
fi

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

PROVIDES="
	zstd$secondaryArchSuffix = $portVersion
	lib:libzstd$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_bin="
	zstd${secondaryArchSuffix}_bin = $portVersion
	cmd:unzstd$commandSuffix = $portVersion
	cmd:zstd$commandSuffix = $portVersion
	cmd:zstdcat$commandSuffix = $portVersion
	cmd:zstdgrep$commandSuffix = $portVersion
	cmd:zstdless$commandSuffix = $portVersion
	cmd:zstdmt$commandSuffix = $portVersion
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
PROVIDES_bin="$PROVIDES_bin
	cmd:pzstd$commandSuffix = $portVersion
	"
fi
REQUIRES_bin="
	haiku$secondaryArchSuffix
	zstd$secondaryArchSuffix == $portVersion base
	cmd:grep
	cmd:less
	lib:liblz4$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
REQUIRES_bin="$REQUIRES_bin
	lib:liblzma$secondaryArchSuffix
	"
fi
if [ -n "$secondaryArchSuffix" -a "$commandBinDir" = "$prefix"/bin ]; then
CONFLICTS_bin="
	zstd_bin
	"
fi

PROVIDES_devel="
	zstd${secondaryArchSuffix}_devel = $portVersion
	devel:libzstd$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	zstd$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:liblz4$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
BUILD_REQUIRES="$BUILD_REQUIRES
	devel:liblzma$secondaryArchSuffix
	"
fi
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:sed
	"

TEST_REQUIRES="
	cmd:python3
	"

BUILD()
{
	make PREFIX=$prefix INCLUDEDIR=$includeDir LIBDIR=$libDir $jobArgs
	if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
		make -C contrib/pzstd
	fi
}

INSTALL()
{
	mkdir -p $libDir
	make install PREFIX=$prefix LIBDIR=$libDir INCLUDEDIR=$includeDir \
		BINDIR=$commandBinDir mandir=$manDir
	if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
		make -C contrib/pzstd install PREFIX="$prefix" BINDIR="$commandBinDir"
	fi

	prepareInstalledDevelLib libzstd
	fixPkgconfig

	packageEntries bin \
		$commandBinDir \
		$manDir/man1

	packageEntries devel \
		$developDir

	rmdir $manDir

	install -d $docDir
	install -t $docDir -m 444 LICENSE
}

TEST()
{
	make test
	make -C tests all
if [ "$effectiveTargetArchitecture" != x86_gcc2 ]; then
	make -C contrib/pzstd test
fi
}
