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

EAPI="6"

inherit scons-utils toolchain-funcs

if [[ ${PV} = "9999" ]] ; then
	EGIT_REPO_URI="git://swift.im/swift"
	EGIT_BRANCH="swift-4.x"
	inherit git-r3
	KEYWORDS=""
else
	RESTRICT="mirror"
	SRC_URI="http://swift.im/downloads/releases/${P}/${P}.tar.gz"
	KEYWORDS="~amd64 ~x86"
fi

DESCRIPTION="Your friendly chat client"
HOMEPAGE="http://swift.im/"

LICENSE="GPL-3"
SLOT="0"
IUSE="debug doc +expat gconf icu static-libs test upnp zeroconf"

# LDNS / Unbound is bundled only...
RDEPEND="dev-libs/boost:=
	dev-libs/openssl:0
	sys-libs/zlib
	zeroconf? ( net-dns/avahi )
	expat? ( dev-libs/expat )
	!expat? ( dev-libs/libxml2 )
	gconf? ( gnome-base/gconf dev-libs/glib:2 )
	icu? ( dev-libs/icu:= )
	!icu? ( net-dns/libidn:= )
	upnp? ( net-libs/libnatpmp net-libs/miniupnpc:= )"
DEPEND="${RDEPEND}
	doc? (
		>=app-text/docbook-xsl-stylesheets-1.75
		>=app-text/docbook-xml-dtd-4.5
		dev-libs/libxslt
	)"

src_prepare() {
	# remove all bundled packages to ensure
	# consistency of headers and linked libraries
	rm -r 3rdParty || die

	eapply_user
}

src_configure() {
	MYSCONS=(
		cc="$(tc-getCC)"
		cxx="$(tc-getCXX)"
		ccflags="${CFLAGS}"
		cxxflags="${CXXFLAGS}"
		link="$(tc-getCXX)"
		linkflags="${LDFLAGS}"
		ar="$(tc-getAR)"
		swiften_dll=$(usex static-libs false true)
		zlib_includedir=/usr/include
		zlib_libdir=/$(get_libdir)
		{boost,libidn,zlib}_bundled_enable=false
		icu=$(usex icu true false)
		try_avahi=$(usex zeroconf true false)
		try_gconf=$(usex gconf true false)
		try_expat=$(usex expat true false)
		try_libxml=$(usex expat false true)
		experimental_ft=$(usex upnp true false)
		Swiften
	)
}

src_compile() {
	escons V=1 "${MYSCONS[@]}"
}

src_test() {
	escons "${MYSCONS[@]}" test=unit QA
}

src_install() {
	escons "${MYSCONS[@]}" SWIFTEN_INSTALLDIR="${ED}/usr" \
		SWIFTEN_LIBDIR="${ED}/usr/$(get_libdir)" "${ED}/usr"

	use doc && dohtml "Documentation/SwiftenDevelopersGuide/Swiften Developers Guide.html"
}