SUMMARY="A SSH2 library"
DESCRIPTION="libssh is a multiplatform C library implementing the SSHv2 \
protocol on client and server side. With libssh, you can remotely execute \
programs, transfer files, use a secure and transparent tunnel, manage public \
keys and much more..."
HOMEPAGE="https://www.libssh.org/"
COPYRIGHT="1995 Tatu Ylonen
	2000 Markus Friedl
	2003-2014 Aris Adamantiadis
	2006 Alexander Neundorf
	2007 Daniel Gollub
	2007-2018 Andreas Schneider
	2012 Dmitriy Kuznetsov
	2017 Sartura d.o.o.
	2017 Jan-Lukas Wynen
	2018-2019 Anderson Toshiyuki Sasaki
	2018-2019 Red Hat, Inc.
	2019 Simo Sorce"
LICENSE="GNU LGPL v2.1
	BSD (2-clause)"
REVISION="2"
SOURCE_URI="https://www.libssh.org/files/${portVersion%.*}/libssh-$portVersion.tar.xz"
CHECKSUM_SHA256="86bcf885bd9b80466fe0e05453c58b877df61afa8ba947a58c356d7f0fab829b"
PATCHES="libssh-$portVersion.patchset"

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

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

PROVIDES="
	libssh$secondaryArchSuffix = $portVersion
	lib:libssh$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

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

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libcmocka$secondaryArchSuffix
	devel:libcrypto$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:sshd
	"

if [ -z "$secondaryArchSuffix" ]; then
	ARCHITECTURES_doc="any"

	PROVIDES_doc="
		libssh_doc = $portVersion
		"

	BUILD_PREREQUIRES+="
		cmd:doxygen
		"
fi

defineDebugInfoPackage libssh$secondaryArchSuffix \
	"$libDir"/libssh.so.$libVersion

PATCH()
{
	# Requires: https://review.haiku-os.org/c/haiku/+/632
	sed -i \
		-e '/torture_threads_pki_rsa/d' \
		-e '/torture_rand/d' \
		-e '/torture_threads_init/d' \
		-e '/torture_threads_buffer/d' \
		-e '/torture_threads_crypto/d' \
		tests/unittests/CMakeLists.txt

	# Doesn't work in chroot, probably due to the lack of the passwd database
	sed -i 's/cmocka_unit_test(torture_path_expand_tilde_unix),//' \
		tests/unittests/torture_misc.c
}

BUILD()
{
	_maybe_pie=()
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		_maybe_pie=("-DCMAKE_EXE_LINKER_FLAGS=-pie -fstack-protector")
	fi

	mkdir -p build; cd build
	cmake .. $cmakeDirArgs \
		-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
		-DUNIT_TESTING=ON \
		"${_maybe_pie[@]}"
	make $jobArgs

	if [ -z "$secondaryArchSuffix" ]; then
		make docs
	fi
}

INSTALL()
{
	cd build
	make install

	prepareInstalledDevelLibs libssh
	fixPkgconfig

	ln -sr $libDir/libssh.so.$libVersion $libDir/libssh.so

	# devel package
	packageEntries devel \
		"$developDir" \
		"$libDir"/cmake

	if [ -z "$secondaryArchSuffix" ]; then
		install -d -m 755 "$developDocDir"
		cp -rT doc/html "$developDocDir"
		packageEntries doc "$developDir"
	fi
}

TEST()
{
	cd build
	make test
}
