SUMMARY="Mozilla's library that implements PKI support"
DESCRIPTION="Network Security Services (NSS) is a set of libraries designed \
to support cross-platform development of security-enabled client and server \
applications. Applications built with NSS can support SSL v2 and v3, TLS, \
PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME, X.509 v3 certificates, and \
other security standards."
HOMEPAGE="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS"
COPYRIGHT="Mozilla Foundation"
LICENSE="GNU GPL v2
	GNU LGPL v2.1
	MPL v2.0"
REVISION="1"
NSS_VERSION=`echo $portVersion | sed 's/\./_/g'`
SOURCE_URI="https://ftp.mozilla.org/pub/mozilla.org/security/nss/releases/NSS_${NSS_VERSION}_RTM/src/nss-$portVersion.tar.gz"
CHECKSUM_SHA256="182d2fef629102ae9423aabf2c192242b565cf5098e82c5a26cf70c5e4ea2221"
SOURCE_DIR="nss-$portVersion/nss"
PATCHES="nss-$portVersion.patchset"

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

PROVIDES="
	nss$secondaryArchSuffix = $portVersion compat >= 3
	cmd:addbuiltin$secondaryArchSuffix = $portVersion compat >= 3
	cmd:atob$secondaryArchSuffix = $portVersion compat >= 3
	cmd:baddbdir$secondaryArchSuffix = $portVersion compat >= 3
	cmd:bltest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:btoa$secondaryArchSuffix = $portVersion compat >= 3
	cmd:certcgi$secondaryArchSuffix = $portVersion compat >= 3
	cmd:certutil$secondaryArchSuffix = $portVersion compat >= 3
	cmd:checkcert$secondaryArchSuffix = $portVersion compat >= 3
	cmd:chktest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:cmsutil$secondaryArchSuffix = $portVersion compat >= 3
	cmd:conflict$secondaryArchSuffix = $portVersion compat >= 3
	cmd:crlutil$secondaryArchSuffix = $portVersion compat >= 3
	cmd:crmftest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:dbtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:derdump$secondaryArchSuffix = $portVersion compat >= 3
	cmd:dertimetest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:digest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:ecperf$secondaryArchSuffix = $portVersion compat >= 3
	cmd:encodeinttest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:fbectest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:fipstest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:httpserv$secondaryArchSuffix = $portVersion compat >= 3
	cmd:listsuites$secondaryArchSuffix = $portVersion compat >= 3
	cmd:makepqg$secondaryArchSuffix = $portVersion compat >= 3
	cmd:mangle$secondaryArchSuffix = $portVersion compat >= 3
	cmd:modutil$secondaryArchSuffix = $portVersion compat >= 3
	cmd:multinit$secondaryArchSuffix = $portVersion compat >= 3
	cmd:nonspr10$secondaryArchSuffix = $portVersion compat >= 3
	cmd:nss_policy_check$secondaryArchSuffix = $portVersion compat >= 3
	cmd:ocspclnt$secondaryArchSuffix = $portVersion compat >= 3
	cmd:ocspresp$secondaryArchSuffix = $portVersion compat >= 3
	cmd:oidcalc$secondaryArchSuffix = $portVersion compat >= 3
	cmd:p7content$secondaryArchSuffix = $portVersion compat >= 3
	cmd:p7env$secondaryArchSuffix = $portVersion compat >= 3
	cmd:p7sign$secondaryArchSuffix = $portVersion compat >= 3
	cmd:p7verify$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk11ectest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk11gcmtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk11importtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk11mode$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk12util$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pk1sign$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pkix_errcodes$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pp$secondaryArchSuffix = $portVersion compat >= 3
	cmd:pwdecrypt$secondaryArchSuffix = $portVersion compat >= 3
	cmd:remtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:rsaperf$secondaryArchSuffix = $portVersion compat >= 3
	cmd:rsapoptst$secondaryArchSuffix = $portVersion compat >= 3
	cmd:sdrtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:secmodtest$secondaryArchSuffix = $portVersion compat >= 3
	cmd:selfserv$secondaryArchSuffix = $portVersion compat >= 3
	cmd:shlibsign$secondaryArchSuffix = $portVersion compat >= 3
	cmd:signtool$secondaryArchSuffix = $portVersion compat >= 3
	cmd:signver$secondaryArchSuffix = $portVersion compat >= 3
	cmd:ssltap$secondaryArchSuffix = $portVersion compat >= 3
	cmd:strsclnt$secondaryArchSuffix = $portVersion compat >= 3
	cmd:symkeyutil$secondaryArchSuffix = $portVersion compat >= 3
	cmd:tstclnt$secondaryArchSuffix = $portVersion compat >= 3
	cmd:vfychain$secondaryArchSuffix = $portVersion compat >= 3
	cmd:vfyserv$secondaryArchSuffix = $portVersion compat >= 3
	lib:libfreebl3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libnss3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libnssckbi$secondaryArchSuffix = $portVersion compat >= 3
	lib:libnssckbi_testlib$secondaryArchSuffix = $portVersion compat >= 3
	lib:libnssdbm3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libnssutil3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libsmime3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libsoftokn3$secondaryArchSuffix = $portVersion compat >= 3
	lib:libssl3$secondaryArchSuffix = $portVersion compat >= 3
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libnspr4$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	nss${secondaryArchSuffix}_devel = $portVersion compat >= 3
	devel:libfreebl3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libnss3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libnssckbi$secondaryArchSuffix = $portVersion compat >= 3
	devel:libnssckbi_testlib$secondaryArchSuffix = $portVersion compat >= 3
	devel:libnssdbm3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libnssutil3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libsmime3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libsoftokn3$secondaryArchSuffix = $portVersion compat >= 3
	devel:libssl3$secondaryArchSuffix = $portVersion compat >= 3
	"
REQUIRES_devel="
	nss$secondaryArchSuffix == $portVersion base
	devel:libnspr4$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libnspr4$secondaryArchSuffix >= 4.12
	devel:libsqlite3$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:autoconf
	cmd:awk
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"
TEST_REQUIRES="
	cmd:find
	"

BUILD()
{
	# FIXME: $secondaryArchSuffix is wrong here (_arch). What should be used?
	myCPPFLAGS="${CPPFLAGS} `pkg-config nspr --cflags`"
	myLDFLAGS="${LDFLAGS} `pkg-config nspr --libs-only-L`"

	# export BUILD_OPT=1
	export NSDISTMODE=copy
	export FREEBL_NO_DEPEND=1
	export NSS_ALLOW_SSLKEYLOGFILE=1
	export NSS_ENABLE_WERROR=0
	export ASFLAGS=""
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		export USE_64=1
	fi
	for d in . lib/dbm ; do
		CPPFLAGS="${myCPPFLAGS}"
		LDFLAGS="${myLDFLAGS}"
		make -j1 -C ${d}
	done
}

INSTALL()
{
	# files are in /sources/dist
	mkdir -p $includeDir $binDir
	mkdir -p $(dirname $libDir)
	cp ../dist/*/bin/* $binDir
	cp -r ../dist/*/lib $libDir
	cp -r ../dist/public/nss $includeDir

	# strip debug info
	strip $binDir/*
	strip $libDir/*

	# set up the develop directory correctly
	prepareInstalledDevelLibs libnss3 libfreebl3 libnssckbi libnssdbm3 \
		libnssutil3 libsmime3 libsoftokn3 libssl3

	mkdir -p $developLibDir/pkgconfig
	cat > $developLibDir/pkgconfig/nss.pc << EOF
prefix=${prefix}
exec_prefix=${prefix}
libdir=${libDir}
includedir=${includeDir}

Name: NSS
Description: Network Security Services
Version: $portVersion
Requires: nspr
Libs: -L${developLibDir} -lssl3 -lsmime3 -lnss3 -lnssutil3
Cflags: -I${includeDir}/nss
EOF

	# devel package
	packageEntries devel \
		$developDir

	rm $libDir/*.a
}

TEST()
{
	cd ../dist/*/lib
	export LIBRARY_PATH=$LIBRARY_PATH:$PWD
	echo "host localhost and domain localdomain must exist!"
	cd -
	cd tests
	if [ "$effectiveTargetArchitecture" = x86_64 ]; then
		export USE_64=1
	fi

	HOST=localhost DOMSUF=localdomain ./all.sh
}
