SUMMARY="Provides a way to load and enumerate PKCS#11 modules"
DESCRIPTION="p11-kit provides a way to load and enumerate PKCS#11 modules. \
Provides a standard configuration setup for installing PKCS#11 modules \
in such a way that they're discoverable."
HOMEPAGE="https://p11-glue.freedesktop.org/p11-kit.html"
COPYRIGHT="2017 Red Hat, Inc."
LICENSE="BSD (3-clause)"
REVISION="2"
SOURCE_URI="https://github.com/p11-glue/p11-kit/releases/download/$portVersion/p11-kit-$portVersion.tar.gz"
CHECKSUM_SHA256="34c3bd8c0050dd7c4e6228aecf0f168de0a1b34562ddbf74a1c70904c2523c6f"
SOURCE_DIR="p11-kit-$portVersion"
PATCHES="p11_kit-$portVersion.patchset"

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

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

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

PROVIDES="
	p11_kit$secondaryArchSuffix = $portVersion
	cmd:p11_kit$commandSuffix = $portVersion
	cmd:trust$commandSuffix = $portVersion
	lib:libp11_kit$secondaryArchSuffix = $libVersionCompat
	lib:p11_kit_proxy$secondaryArchSuffix = $libVersionCompat
	"

REQUIRES="
	haiku$secondaryArchSuffix
	ca_root_certificates
	lib:libffi$secondaryArchSuffix
	lib:libtasn1$secondaryArchSuffix
	"

PROVIDES_devel="
	p11_kit${secondaryArchSuffix}_devel = $portVersion
	devel:libp11_kit$secondaryArchSuffix = $libVersion
	"
REQUIRES_devel="
	p11_kit$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libffi$secondaryArchSuffix
	devel:libtasn1$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:autoconf
	cmd:aclocal
	cmd:awk
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"

USER_SETTINGS_FILES="
	settings/pkcs11/pkcs11.conf template data/pkcs11/pkcs11.conf.example
	"

GLOBAL_WRITABLE_FILES="
	settings/pkcs11/pkcs11.conf keep-old
	"

defineDebugInfoPackage p11_kit$secondaryArchSuffix \
	$commandBinDir/p11-kit \
	$commandBinDir/trust \
	$libDir/libp11-kit.so.$libVersion \
	$libDir/p11-kit/p11-kit-remote \
	$libDir/p11-kit/p11-kit-server \
	$libDir/pkcs11/p11-kit-client.so \
	$libDir/pkcs11/p11-kit-trust.so

BUILD()
{
	certsInstallDir="$portPackageLinksDir/ca_root_certificates"
	certsDir="$certsInstallDir/$relativeDataRootDir/ssl"
	runConfigure --omit-dirs binDir ./configure --bindir $commandBinDir \
		LDFLAGS="-lnetwork" \
		--with-trust-paths="${certsDir}/CARootCertificates.pem" \
		--with-user-config="`finddir B_USER_SETTINGS_DIRECTORY`/pkcs11"
	make $jobArgs
}

INSTALL()
{
	make install

	# Replace, in the config example, the real path of the settings dir by
	# the user-friendly path. Then install it as the config file.
	sed -i \
		-e "s|$sysconfDir|`finddir B_SYSTEM_SETTINGS_DIRECTORY`|;" \
		$settingsDir/pkcs11/pkcs11.conf.example
	install -d $dataDir/pkcs11
	install -t $dataDir/pkcs11 $settingsDir/pkcs11/pkcs11.conf.example
	mv \
		$settingsDir/pkcs11/pkcs11.conf.example \
		$settingsDir/pkcs11/pkcs11.conf

	# clean up
	rm -f $libDir/*.la $libDir/pkcs11/*.la

	# fix incorrect install directory on secondary arch builds
	if [ -n "$secondaryArchSuffix" ]; then
		mv $prefix/lib/p11-kit $libDir/p11-kit
	fi

	prepareInstalledDevelLib libp11-kit
	fixPkgconfig

	packageEntries devel \
		$developDir
}

TEST()
{
	make check
}
