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

EAPI=5

inherit eutils multilib toolchain-funcs

DESCRIPTION="Asynchronous input/output library that uses the kernels native interface"
HOMEPAGE="http://www.kernel.org/pub/linux/kernel/people/andrea/libaio/ http://lse.sourceforge.net/io/aio.html"
SRC_URI="mirror://kernel/linux/libs/aio/${P}.tar.bz2"

LICENSE="LGPL-2"
SLOT="0"
KEYWORDS="amd64 x86"
IUSE="multilib static-libs test"

EMULTILIB_PKG="true"

S=${WORKDIR}

aio_get_install_abis() {
	use multilib && get_install_abis || echo ${ABI:-default}
}

src_unpack() {
	local OABI=${ABI}
	for ABI in $(aio_get_install_abis)
	do
		mkdir -p "${WORKDIR}"/${ABI} || die
		cd "${WORKDIR}"/${ABI} || die
		unpack ${A}
	done
	ABI=${OABI}
}

src_prepare() {
	local OABI=${ABI}
	for ABI in $(aio_get_install_abis)
	do
		einfo "Preparing ${ABI} ABI ..."
		cd "${WORKDIR}"/${ABI}/${P} || die

		# remove stuff provided by man-pages now
		rm man/{lio_listio,aio_{cancel,error,fsync,init,read,return,suspend,write}}.* || die

		epatch "${FILESDIR}"/${PN}-0.3.109-unify-bits-endian.patch \
			"${FILESDIR}"/${PN}-0.3.109-generic-arch.patch \
			"${FILESDIR}"/${PN}-0.3.106-build.patch \
			"${FILESDIR}"/${PN}-0.3.107-ar-ranlib.patch \
			"${FILESDIR}"/${PN}-0.3.109-install.patch \
			"${FILESDIR}"/${PN}-0.3.109-x32.patch \
			"${FILESDIR}"/${PN}-0.3.109-testcase-8.patch

		declare -a extra_sed
		if ! use static-libs; then
			extra_sed[${#extra_sed[@]}]='-e'
			extra_sed[${#extra_sed[@]}]='/\tinstall .*\/libaio.a/d'
			# Tests require the static library to be built.
			if ! use test; then
				extra_sed[${#extra_sed[@]}]='-e'
				extra_sed[${#extra_sed[@]}]='/^all_targets +=/s/ libaio.a//'
			fi
		fi
		sed -i \
			-e "/^libdir=/s:lib$:$(get_libdir):" \
			-e "/^prefix=/s:/usr:${EPREFIX}/usr:" \
			-e '/:=.*strip.*shell.*git/s:=.*:=:' \
			"${extra_sed[@]}" \
			src/Makefile Makefile || die

		sed -i -e "s:-Werror::g" harness/Makefile || die
	done
	ABI=${OABI}
}

emake_libaio() {
	# The Makefiles need these environments, but multilib_toolchain_setup()
	# does not export anything when there is only one default abi available.
	CC="$(tc-getCC) $(get_abi_CFLAGS)" \
	AR=$(tc-getAR) \
	RANLIB=$(tc-getRANLIB) \
	emake "$@"
}

src_compile() {
	local OABI=${ABI}
	for ABI in $(aio_get_install_abis)
	do
		einfo "Compiling ${ABI} ABI ..."
		cd "${WORKDIR}"/${ABI}/${P} || die
		emake_libaio
	done
	ABI=${OABI}
}

src_test() {
	local OABI=${ABI}
	for ABI in $(aio_get_install_abis)
	do
		einfo "Testing ${ABI} ABI ..."
		cd "${WORKDIR}"/${ABI}/${P}/harness || die
		mkdir testdir || die
		# 'make check' breaks with sandbox, 'make partcheck' works
		emake_libaio partcheck prefix="${S}/src" libdir="${S}/src"
	done
	ABI=${OABI}
}

src_install() {
	local OABI=${ABI}
	for ABI in $(aio_get_install_abis)
	do
		einfo "Installing ${ABI} ABI ..."
		cd "${WORKDIR}"/${ABI}/${P} || die

		# Don't use ED for emake, src_prepare already inserts EPREFIX in the correct
		# place
		emake_libaio install DESTDIR="${D}"

		doman man/*
		dodoc ChangeLog TODO

		# move crap to / for multipath-tools #325355
		CFLAGS="${CFLAGS} $(get_abi_CFLAGS)" gen_usr_ldscript -a aio
	done
	ABI=${OABI}
}