# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="1"

inherit eutils flag-o-matic toolchain-funcs

DESCRIPTION="Toolkit for SSL v2/v3 and TLS v1"
HOMEPAGE="http://www.openssl.org/"
SRC_URI="mirror://openssl/source/${P}.tar.gz"

LICENSE="openssl"
SLOT="0.9.7"
KEYWORDS="-* ~alpha ~amd64 ~arm ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~sparc-fbsd ~x86 ~x86-fbsd"
IUSE="emacs test bindist zlib"

RDEPEND="!=dev-libs/openssl-0.9.7*:0"
DEPEND="${RDEPEND}
	sys-apps/diffutils
	>=dev-lang/perl-5
	test? ( sys-devel/bc )"
PDEPEND="app-misc/ca-certificates"

pkg_setup() {
	# avoid collisions with openssl-1 (preserve lib)
	if ! has_version dev-libs/openssl:${SLOT} ; then
		ewarn "Removing lib{crypto,ssl}.so.0.9.7 to avoid collision with openssl-1"
		rm -f "${ROOT}"/usr/$(get_libdir)/lib{crypto,ssl}.so.0.9.7
	fi
}


src_unpack() {
	unpack ${A}

	cd "${S}"

	epatch "${FILESDIR}"/${PN}-0.9.7g-ppc64.patch
	epatch "${FILESDIR}"/${PN}-0.9.7e-gentoo.patch
	epatch "${FILESDIR}"/${PN}-0.9.7-hppa-fix-detection.patch
	epatch "${FILESDIR}"/${PN}-0.9.7-alpha-default-gcc.patch
	epatch "${FILESDIR}"/${PN}-0.9.7g-mem-clr-ptr-cast.patch
	epatch "${FILESDIR}"/${PN}-0.9.7h-ABI-compat.patch
	epatch "${FILESDIR}"/${PN}-0.9.7g-superh.patch
	epatch "${FILESDIR}"/${PN}-0.9.7i-m68k.patch
	epatch "${FILESDIR}"/${PN}-0.9.7g-amd64-fbsd.patch
	epatch "${FILESDIR}"/${PN}-0.9.7j-doc-updates.patch

	# allow openssl to be cross-compiled
	cp "${FILESDIR}"/gentoo.config-0.9.7g gentoo.config || die "cp cross-compile failed"
	chmod a+rx gentoo.config

	# Don't build manpages if we don't want them
	has noman FEATURES && sed -i '/^install:/s:install_docs::' Makefile.org

	case $(gcc-version) in
		3.2)
			filter-flags -fprefetch-loop-arrays -freduce-all-givs -funroll-loop
		;;
		3.4 | 3.3 )
			filter-flags -fprefetch-loop-arrays -freduce-all-givs -funroll-loops
			[[ ${ARCH} == "ppc" ||  ${ARCH} == "ppc64" ]] && append-flags -fno-strict-aliasing
		;;
	esac
	append-flags -Wa,--noexecstack

	# replace CFLAGS
	OLDIFS=$IFS
	IFS=$'\n'
	for a in $( grep -n -e "^\"linux-" Configure ); do
		LINE=$( echo $a | awk -F: '{print $1}' )
		CUR_CFLAGS=$( echo $a | awk -F: '{print $3}' )
		NEW_CFLAGS=$(echo $CUR_CFLAGS | LC_ALL=C sed -r -e "s|-O[23]||" -e \
		"s:-fomit-frame-pointer::" -e "s:-mcpu=[-a-z0-9]+::" -e "s:-m486::" \
		-e "s:-mv8::")
		# ppc64's current toolchain sucks at optimization and will break this package
		[[ $(tc-arch) != "ppc64" ]] && NEW_CFLAGS="${NEW_CFLAGS} ${CFLAGS}"

		sed -i "${LINE}s:$CUR_CFLAGS:$NEW_CFLAGS:" Configure || die "sed failed"
	done
	IFS=$OLDIFS

	if [ "$(get_libdir)" != "lib" ] ; then
		# using a library directory other than lib requires some magic
		sed -i \
			-e "s+\(\$(INSTALL_PREFIX)\$(INSTALLTOP)\)/lib+\1/$(get_libdir)+g" \
			-e "s+libdir=\$\${exec_prefix}/lib+libdir=\$\${exec_prefix}/$(get_libdir)+g" \
			Makefile.org \
			|| die "sed failed"
		./config --test-sanity || die "sanity failed"
	fi
}

src_compile() {
	# Clean out patent-or-otherwise-encumbered code.
	# RC5:   5,724,428 03/03/2015
	# EC:    ????????? ??/??/2015
	local confopts=""
	use bindist && confopts="no-rc5 no-ec"

	use zlib && confopts="${confopts} zlib-dynamic"

	local sslout=$(./gentoo.config)
	einfo "Use configuration ${sslout}"

	local config="Configure"
	[[ -z ${sslout} ]] && config="config"
	./${config} \
		${sslout} \
		${confopts} \
		--prefix=/usr \
		--openssldir=/etc/ssl \
		shared threads \
		|| die "Configure failed"


        # Clean out hardcoded flags that openssl uses
        local CFLAG=$(grep ^CFLAG= Makefile | LC_ALL=C sed \
                -e 's:^CFLAG=::' \
                -e 's:-fomit-frame-pointer ::g' \
                -e 's:-O[0-9] ::g' \
                -e 's:-march=[-a-z0-9]* ::g' \
                -e 's:-mcpu=[-a-z0-9]* ::g' \
                -e 's:-m[a-z0-9]* ::g' \
        )
        sed -i \
                -e "/^LIBDIR=/s|=.*|=$(get_libdir)|" \
                -e "/^CFLAG/s|=.*|=${CFLAG} ${CFLAGS}|" \
                -e "/^SHARED_LDFLAGS=/s|$| ${LDFLAGS}|" \
                Makefile || die


	emake \
		CC="$(tc-getCC)" MAKEDEPPROG="$(tc-getCC)" \
		AR="$(tc-getAR) r" \
		RANLIB="$(tc-getRANLIB)" \
		all || die "make all failed"
}

src_test() {
	# make sure sandbox doesnt die on *BSD
	addpredict /dev/crypto

	make test || die "make test failed"
}

src_install() {
        dolib.so lib{crypto,ssl}.so.0.9.7 || die
}