# Template file for 'libressl'
pkgname=libressl
version=3.1.5
revision=2
bootstrap=yes
build_style=gnu-configure
configure_args="$(vopt_enable asm)"
short_desc="Version of the TLS/crypto stack forked from OpenSSL"
maintainer="Juan RP <xtraeme@gmail.com>"
license="OpenSSL, ISC"
#changelog="https://raw.githubusercontent.com/libressl-portable/portable/master/ChangeLog"
homepage="http://www.libressl.org/"
distfiles="http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/${pkgname}-${version}.tar.gz"
checksum=2c13ddcec5081c0e7ba7f93d8370a91911173090f1922007e1d90de274500494
provides="openssl-${version}_${revision}"
replaces="openssl>=0"
conf_files="/etc/ssl/openssl.cnf /etc/ssl/x509v3.cnf"
_lssl_asm_ver="1.1.0"

build_options="asm"
desc_option_asm="Use platform assembly for faster crypto"

if [ "$build_option_asm" ]; then
	distfiles+=" https://github.com/q66/libressl-portable-asm/archive/v${_lssl_asm_ver}.tar.gz"
	checksum+=" 7d92bf898c3a526cfdd804ea2e6ba1c89bd5a95589092e3ea585ef7c4818d789"
fi

# only enable asm for full chroots by default
# otherwise we'd be introducing an autotools dependency on the host
if [ "$CHROOT_READY" ]; then
	build_options_default="asm"
fi

case "$XBPS_TARGET_MACHINE" in
	# disable ssp
	i686-musl) configure_args+=" --disable-hardening";;
	# on armv5 always disable asm as it's not supported
	armv5*) configure_args+=" --disable-asm";;
esac

if [ "$CROSS_BUILD" -o "$build_option_asm" ]; then
	_regen_build=yes
fi

if [ -n "$_regen_build" ]; then
	hostmakedepends=" automake libtool"
fi

post_extract() {
	[ -z "$build_option_asm" ] && return 0
	mv ../libressl-portable-asm-${_lssl_asm_ver} .
}

pre_configure() {
	[ -z "$_regen_build" ] && return 0
	if [ "$build_option_asm" ]; then
		./libressl-portable-asm-${_lssl_asm_ver}/patch_libressl.sh .
	fi
	autoreconf -if
}

post_install() {
	# Use CA file from ca-certificates instead.
	rm -f ${DESTDIR}/etc/ssl/cert.pem
	ln -s certs.pem ${DESTDIR}/etc/ssl/cert.pem

	vlicense COPYING
	if [ "$build_option_asm" ]; then
		vlicense libressl-portable-asm-${_lssl_asm_ver}/LICENSE.cryptogams
		vlicense libressl-portable-asm-${_lssl_asm_ver}/LICENSE.openssl
	fi
	find ${DESTDIR}/usr/share/man/man1 -type f ! -name openssl.1 -delete
}

libcrypto46_package() {
	short_desc+=" - crypto library"
	pkg_install() {
		vmove usr/lib/libcrypto.so.*
	}
}

libssl48_package() {
	short_desc+=" - SSL/TLS library"
	pkg_install() {
		vmove usr/lib/libssl.so.*
	}
}

libtls20_package() {
	short_desc+=" - new TLS library"
	pkg_install() {
		vmove usr/lib/libtls.so.*
	}
}

libressl-devel_package() {
	short_desc+=" - development files"
	depends="libressl-${version}_${revision}
	 libcrypto46-${version}_${revision}
	 libssl48-${version}_${revision}
	 libtls20-${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
		vmove usr/lib/pkgconfig
		vmove usr/share/man/man3
	}
}

libressl-netcat_package() {
	short_desc="TCP/IP swiss army knife (LibreSSL variant)"
	alternatives="
	 nc:nc:/usr/bin/libressl-nc
	 nc:nc.1:/usr/share/man/man1/libressl-nc.1"
	pkg_install() {
		vbin apps/nc/.libs/nc libressl-nc
		vman apps/nc/nc.1 libressl-nc.1
	}
}
