SUMMARY="A TLS 1.0 and SSL 3.0 implementation for the GNU project"
DESCRIPTION="GnuTLS is a secure communications library implementing the SSL, \
TLS and DTLS protocols and technologies around them. It provides a simple C \
language application programming interface (API) to access the secure \
communications protocols as well as APIs to parse and write X.509, PKCS #12, \
OpenPGP and other required structures. It is aimed to be portable and \
efficient with focus on security and interoperability."
HOMEPAGE="https://www.gnutls.org/"
COPYRIGHT="2000-2018 Free Software Fundation Inc.
	2004-2008 Simon Josefsson
	2000-2004 Nikos Mavrogiannopoulos"
LICENSE="GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="https://www.gnupg.org/ftp/gcrypt/gnutls/v${portVersion%\.*}/gnutls-$portVersion.tar.xz"
CHECKSUM_SHA256="1b79b381ac283d8b054368b335c408fedcb9b7144e0c07f531e3537d4328f3b3"
SOURCE_DIR="gnutls-$portVersion"
PATCHES="gnutls-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2 ?arm ?ppc"
SECONDARY_ARCHITECTURES="x86"

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

libgnutls_opensslVersion="27.0.2"
libgnutlsVersion="30.28.2"
libgnutlsxxVersion="28.1.0"

libgnutls_opensslVersionCompat="$libgnutls_opensslVersion compat >= ${libgnutls_opensslVersion%%.*}"
libgnutlsVersionCompat="$libgnutlsVersion compat >= ${libgnutlsVersion%%.*}"
libgnutlsxxVersionCompat="$libgnutlsxxVersion compat >= ${libgnutlsxxVersion%%.*}"

PROVIDES="
	gnutls$secondaryArchSuffix = $portVersion
	lib:libgnutls_openssl$secondaryArchSuffix = $libgnutls_opensslVersionCompat
	lib:libgnutls$secondaryArchSuffix = $libgnutlsVersionCompat
	lib:libgnutlsxx$secondaryArchSuffix = $libgnutlsxxVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libgmp$secondaryArchSuffix
	lib:libgpg_error$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libidn2$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libnettle$secondaryArchSuffix
	lib:libp11_kit$secondaryArchSuffix
	lib:libtasn1$secondaryArchSuffix
	lib:libunistring$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"
CONFLICTS="
	gnutls34$secondaryArchSuffix
	gnutls35$secondaryArchSuffix
	gnutls36$secondaryArchSuffix
	"
REPLACES="
	gnutls36$secondaryArchSuffix
	"

if [ -z "$commandSuffix" ]; then
PROVIDES_bin="
	gnutls${secondaryArchSuffix}_bin = $portVersion
	cmd:certtool
	cmd:gnutls_cli
	cmd:gnutls_cli_debug
	cmd:gnutls_serv
	cmd:ocsptool
	cmd:p11tool
	cmd:psktool
	cmd:srptool
	"
REQUIRES_bin="
	gnutls$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libgpg_error$secondaryArchSuffix
	lib:libgmp$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libidn2$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libnettle$secondaryArchSuffix
	lib:libp11_kit$secondaryArchSuffix
	lib:libtasn1$secondaryArchSuffix
	lib:libunistring$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"
CONFLICTS_bin="
	gnutls34${secondaryArchSuffix}_bin
	gnutls35${secondaryArchSuffix}_bin
	gnutls36${secondaryArchSuffix}_bin
	"
REPLACES_bin="
	gnutls36${secondaryArchSuffix}_bin
	"
fi

PROVIDES_devel="
	gnutls${secondaryArchSuffix}_devel = $portVersion
	devel:libgnutls_openssl$secondaryArchSuffix = $libgnutls_opensslVersionCompat
	devel:libgnutls$secondaryArchSuffix = $libgnutlsVersionCompat
	devel:libgnutlsxx$secondaryArchSuffix = $libgnutlsxxVersionCompat
	"
REQUIRES_devel="
	gnutls$secondaryArchSuffix == $portVersion base
	devel:libgcrypt$secondaryArchSuffix
	devel:libgpg_error$secondaryArchSuffix
	devel:libidn2$secondaryArchSuffix
	devel:libnettle$secondaryArchSuffix >= 8
	devel:libp11_kit$secondaryArchSuffix
	devel:libtasn1$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
CONFLICTS_devel="
	gnutls34${secondaryArchSuffix}_devel
	gnutls35${secondaryArchSuffix}_devel
	gnutls36${secondaryArchSuffix}_devel
	"
REPLACES_devel="
	gnutls36${secondaryArchSuffix}_devel
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libgcrypt$secondaryArchSuffix
	devel:libgmp$secondaryArchSuffix
	devel:libgpg_error$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libidn2$secondaryArchSuffix
	devel:libnettle$secondaryArchSuffix >= 8
	devel:libp11_kit$secondaryArchSuffix
	devel:libtasn1$secondaryArchSuffix
	devel:libunistring$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:autoheader
	cmd:automake
	cmd:autopoint
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	"

TEST_REQUIRES="
	cmd:cvs
	cmd:git
	"

debugList=(
	"$libDir"/libgnutls-openssl.so.$libgnutls_opensslVersion
	"$libDir"/libgnutls.so.$libgnutlsVersion
	"$libDir"/libgnutlsxx.so.$libgnutlsxxVersion
	)
if [ -z "$commandSuffix" ]; then
debugList+=(
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/certtool
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/gnutls-cli
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/gnutls-cli-debug
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/gnutls-serv
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/ocsptool
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/psktool
	"${commandBinDir/gnutls$secondaryArchSuffix/gnutls${secondaryArchSuffix}_bin}"/srptool
	)
fi
defineDebugInfoPackage gnutls$secondaryArchSuffix "${debugList[@]}"

BUILD()
{
	runConfigure --omit-dirs binDir ./configure --bindir="$commandBinDir" \
		--enable-openssl-compatibility \
		--disable-hardware-acceleration \
		--disable-nls \
		--with-default-trust-store-file=/system/data/ssl/CARootCertificates.pem
	make $jobArgs AUTOCONF=: AUTOMAKE=: AUTOHEADER=: ACLOCAL=:
}

INSTALL()
{
	make install AUTOCONF=: AUTOMAKE=: AUTOHEADER=: ACLOCAL=:

	rm -f "$infoDir"/dir
	rm -f "$libDir"/libgnutls*.la

	prepareInstalledDevelLibs libgnutls-openssl libgnutls libgnutlsxx
	fixPkgconfig

	if [ -n "$commandSuffix" ]; then
		rm -rf "$commandBinDir" "$documentationDir"
		maybe_manDir_man3=
	else
		maybe_manDir_man3="$manDir"/man3
	fi

	packageEntries devel \
		"$developDir" \
		${maybe_manDir_man3:+"$maybe_manDir_man3"}

	if [ -z "$commandSuffix" ]; then
		packageEntries bin \
			"$commandBinDir" \
			"$manDir"/man1
		rmdir "$manDir"
	fi
}

TEST()
{
	make check LIBS="-lgnu"
}
