SUMMARY="Open source, portable, easy to use, readable and flexible SSL library"
DESCRIPTION="mbed TLS (formerly known as PolarSSL) makes it trivially easy for \
developers to include cryptographic and SSL/TLS capabilities in their (embedded) \
products, facilitating this functionality with a minimal coding footprint."
HOMEPAGE="https://tls.mbed.org/"
COPYRIGHT="2006-2018 ARM Limited"
LICENSE="Apache v2"
REVISION="1"
SOURCE_URI="https://github.com/ARMmbed/mbedtls/archive/mbedtls-$portVersion.tar.gz"
CHECKSUM_SHA256="ea2049c2dd4868693998d5a9780e198194be5aea1706ff4a9d4f882f18c0a101"
SOURCE_DIR="mbedtls-mbedtls-$portVersion"
PATCHES="mbedtls-$portVersion.patchset"

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

libVersionmbedCrypto="6.25.0"
libVersionembedTls="13.25.0"
libVersionembedX509="1.25.0"
libVersionembedCryptoCompat="$libVersionmbedCrypto compat >= ${libVersionmbedCrypto%%.*}"
libVersionembedTlsCompat="$libVersionembedTls compat >= ${libVersionembedTls%%.*}"
libVersionembedX509Compat="$libVersionembedX509 compat >= ${libVersionembedX509%%.*}"

PROVIDES="
	mbedtls$secondaryArchSuffix = $portVersion
	lib:libmbedcrypto$secondaryArchSuffix = $libVersionembedCryptoCompat
	lib:libmbedtls$secondaryArchSuffix = $libVersionembedTlsCompat
	lib:libmbedx509$secondaryArchSuffix = $libVersionembedX509Compat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_tools="
	mbedtls${secondaryArchSuffix}_tools
	cmd:aescrypt2 = $portVersion
	cmd:benchmark = $portVersion
	cmd:cert_app = $portVersion
	cmd:cert_req = $portVersion
	cmd:cert_write = $portVersion
	cmd:crl_app = $portVersion
	cmd:crypt_and_hash = $portVersion
	cmd:crypto_examples = $portVersion
	cmd:dh_client = $portVersion
	cmd:dh_genprime = $portVersion
	cmd:dh_server = $portVersion
	cmd:dtls_client = $portVersion
	cmd:dtls_server = $portVersion
	cmd:ecdh_curve25519 = $portVersion
	cmd:ecdsa = $portVersion
	cmd:gen_entropy = $portVersion
	cmd:gen_key = $portVersion
	cmd:gen_random_ctr_drbg = $portVersion
	cmd:gen_random_havege = $portVersion
	cmd:generic_sum = $portVersion
#	cmd:hello = $portVersion
	cmd:key_app = $portVersion
	cmd:key_app_writer = $portVersion
	cmd:key_ladder_demo = $portVersion
	cmd:key_ladder_demo.sh = $portVersion
	cmd:mini_client = $portVersion
	cmd:mpi_demo = $portVersion
	cmd:pem2der = $portVersion
	cmd:pk_decrypt = $portVersion
	cmd:pk_encrypt = $portVersion
	cmd:pk_sign = $portVersion
	cmd:pk_verify = $portVersion
	cmd:psa_constant_names = $portVersion
	cmd:query_compile_time_config = $portVersion
	cmd:req_app = $portVersion
	cmd:rsa_decrypt = $portVersion
	cmd:rsa_encrypt = $portVersion
	cmd:rsa_genkey = $portVersion
	cmd:rsa_sign = $portVersion
	cmd:rsa_sign_pss = $portVersion
	cmd:rsa_verify = $portVersion
	cmd:rsa_verify_pss = $portVersion
	cmd:selftest = $portVersion
	cmd:ssl_cert_test = $portVersion
	cmd:ssl_client1 = $portVersion
	cmd:ssl_client2 = $portVersion
	cmd:ssl_context_info = $portVersion
	cmd:ssl_fork_server = $portVersion
	cmd:ssl_mail_client = $portVersion
	cmd:ssl_pthread_server = $portVersion
	cmd:ssl_server = $portVersion
	cmd:ssl_server2 = $portVersion
	cmd:strerror = $portVersion
	cmd:udp_proxy = $portVersion
	cmd:udp_proxy_wrapper.sh = $portVersion
	cmd:zeroize = $portVersion
	"
REQUIRES_tools="
	mbedtls$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	mbedtls${secondaryArchSuffix}_devel = $portVersion
	devel:libmbedcrypto$secondaryArchSuffix = $libVersionembedCryptoCompat
	devel:libmbedtls$secondaryArchSuffix = $libVersionembedTlsCompat
	devel:libmbedx509$secondaryArchSuffix = $libVersionembedX509Compat
	"
REQUIRES_devel="
	mbedtls$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:ccache
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:perl
	cmd:python3
	cmd:sed
	"

BUILD()
{
	mkdir -p build && cd build

	export LDFLAGS="-lnetwork"

	cmake .. -DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=$prefix \
		-DLIB_INSTALL_DIR=$libDir \
		-DINCLUDE_INSTALL_DIR=$includeDir \
		-DUSE_SHARED_MBEDTLS_LIBRARY=ON \
		-DENABLE_ZLIB_SUPPORT=ON \
		-DMBEDTLS_FATAL_WARNINGS=OFF

	make $jobArgs
}

INSTALL()
{
	make -C build install

	# remove useless binary
	rm -r $prefix/bin/hello

	prepareInstalledDevelLibs libmbedcrypto libmbedtls libmbedx509

	packageEntries devel \
		"$developDir"

	packageEntries tools \
		$prefix/bin
}

TEST()
{
	LIBRARY_PATH="$sourceDir/library${LIBRARY_PATH:+:$LIBRARY_PATH}" make test
}
