# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

EAPI="5"

inherit autotools-multilib eutils flag-o-matic multilib toolchain-funcs

DESCRIPTION="modular and portable cryptographic toolkit"
HOMEPAGE="http://www.libtom.org/"
SRC_URI="https://github.com/libtom/libtomcrypt/releases/download/${PV}/crypt-${PV}.tar.bz2"

LICENSE="WTFPL-2"
SLOT="0"
KEYWORDS="~amd64 ~ppc ~x86"
IUSE="doc static-libs"

RDEPEND="dev-libs/libtommath[static-libs?,${MULTILIB_USEDEP}]"
DEPEND="${RDEPEND}
	doc? ( virtual/latex-base app-text/ghostscript-gpl )"

src_prepare() {
	use doc || sed -i '/^install:/s:docs::' makefile
	epatch "${FILESDIR}"/libtomcrypt-1.17-r7-libtool-tag-and-make-fix.patch
	sed -i \
		-e "s:--mode=link gcc:--mode=link $(tc-getCC) ${LDFLAGS} --tag CC $(tc-getCC):g" \
		-e "s: gcc: $(tc-getCC):g" \
		{,testprof/}makefile.shared || die

	# need libtool for cross compilation. Bug #376643
	cat <<-EOF > configure.ac
	AC_INIT(libtomcrypt, 0)
	AM_INIT_AUTOMAKE
	LT_INIT
	AC_CONFIG_FILES(Makefile)
	AC_OUTPUT
	EOF
	touch NEWS README AUTHORS ChangeLog Makefile.am
	eautoreconf
#	export LT="${S}"/libtool
	multilib_copy_sources
}

_emake() {
	cd ${BUILD_DIR}
	emake CC="$(tc-getCC)" -f makefile.shared \
		EXTRALIBS="-ltommath" \
		IGNORE_SPEED=1 \
		LIBPATH="${EPREFIX}/usr/$(get_libdir)" \
		INCPATH="${EPREFIX}/usr/include" \
		"$@"
}

src_compile() {
	append-cflags -DLTM_DESC
	multilib_foreach_abi _emake
}

src_test() {
	# Tests don't compile
	true
}

src_install() {
	multilib_foreach_abi _emake DESTDIR="${ED}" install
	dodoc TODO changes
	if use doc ; then
		dodoc doc/*
		docinto notes ; dodoc notes/*
		docinto demos ; dodoc demos/*
	fi
}