# Template file for 'openssl'
pkgname=openssl
_openssl_version=1.0.1
version=${_openssl_version}e
revision=4
hostmakedepends="perl"
makedepends="zlib-devel"
short_desc="Secure Socket Layer and cryptographic library - runtime utilities"
maintainer="Juan RP <xtraeme@gmail.com>"
license="BSD"
homepage="http://www.openssl.org/"
distfiles="https://www.openssl.org/source/$pkgname-$version.tar.gz"
checksum=f74f15e8c8ff11aa3d5bb5f276d202ec18d7246e95f961db76054199c69c1ae3

do_configure() {
	local _opts

	case "$XBPS_TARGET_ARCH" in
		arm*) _target="linux-armv4";;
		mips*) msg_error "$pkgver: unknow cross arch\n";;
	esac

	if [ -z "${_target}" ]; then
		if [ "$XBPS_MACHINE" = "x86_64" ]; then
			_target="linux-x86_64"
			_opts="enable-ec_nistp_64_gcc_128"
		elif [ "$XBPS_MACHINE" = "i686" ]; then
			_target="linux-elf"
		elif [ "$XBPS_MACHINE" = "armv6l" ]; then
			_target="linux-armv4"
		fi
	fi

	sed -i '/^"linux-armv4/s/-DTERMIO/-DTERMIOS/' Configure
	sed -i '/^"linux-x86_64/s/-DTERMIO/-DTERMIOS/' Configure
	sed -i '/^"linux-elf/s/-DTERMIO/-DTERMIOS/' Configure
	sed -i 's/defined(linux)/0/' crypto/ui/ui_openssl.c

	./Configure --prefix=/usr --openssldir=/etc/ssl --libdir=lib \
		shared zlib enable-md2 threads ${_opts} \
		${_target} -Wa,--noexecstack "${CFLAGS}"
}

do_build() {
	make
}

do_install() {
	local _sover="1.0.0"

	make INSTALL_PREFIX=${DESTDIR} MANDIR=/usr/share/man install

	chmod 755 ${DESTDIR}/usr/lib/engines/*.so
	for _solib_ in libssl.so libcrypto.so; do
		chmod 755 ${DESTDIR}/usr/lib/${_solib_}.${_sover}
		cd ${DESTDIR}/usr/lib
		ln -sf ${_solib_}.${_sover} ${_solib_}.1
		ln -sf ${_solib_}.${_sover} ${_solib_}
	done

	# Rename passwd.1, conflicts with shadow.
	mv ${DESTDIR}/usr/share/man/man1/passwd.1 \
		${DESTDIR}/usr/share/man/man1/openssl-passwd.1

	# Rename rand.3 and err.3, conflicts with man-pages.
	for f in rand err; do
		mv ${DESTDIR}/usr/share/man/man3/${f}.3 \
			${DESTDIR}/usr/share/man/man3/openssl-${f}.3
	done

	# Correct broken symlinks.
	rm -f ${DESTDIR}/usr/share/man/man3/{DES_crypt,BUF_MEM_new,ASN1_STRING_type}.3
	ln -sfr ${DESTDIR}/usr/share/man/man3/des.3 \
		${DESTDIR}/usr/share/man/man3/DES_crypt.3
	ln -sfr ${DESTDIR}/usr/share/man/man3/buffer.3 \
		${DESTDIR}/usr/share/man/man3/BUF_MEM_new.3
	ln -sfr ${DESTDIR}/usr/share/man/man3/ASN1_STRING_type.3 \
		${DESTDIR}/usr/share/man/man3/ASN1_STRING_length.3
}

openssl-devel_package() {
	depends="zlib-devel libssl>=$version"
	short_desc="Secure Socket Layer and cryptographic library - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove usr/lib/pkgconfig
		vmove usr/share/man/man3
	}
}

openssl_package() {
	depends="perl"
	conf_files="/etc/ssl/openssl.cnf"
	make_dirs="
		/etc/ssl/certs 0755 root root
		/etc/ssl/private 0750 root root"
	pkg_install() {
		vmove all
	}
}

libssl_package() {
	short_desc="Secure Sockets Layer and Cryptographic library"
	pkg_install() {
		vmove "usr/lib/lib*.so*"
	}
}
