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

EAPI="4"

inherit cmake-utils eutils flag-o-matic

DEBIAN_URI="mirror://debian/pool/main/${PN:0:1}/${PN}"
DEBIAN_SRC="${PN}_${PV}-2.debian.tar.gz"

DESCRIPTION="MySQL database connector for C++ (mimics JDBC 4.0 API)"
HOMEPAGE="http://dev.mysql.com/downloads/connector/cpp/"
SRC_URI="${DEBIAN_URI}/${DEBIAN_SRC}
	mirror://mysql/Downloads/Connector-C++/${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~ppc ~ppc64 ~sparc ~x86"

IUSE="debug examples gcov static-libs"

DEPEND=">=virtual/mysql-5.1
	dev-libs/boost
	dev-libs/openssl"
RDEPEND="${DEPEND}"

# cmake config that works ...
CMAKE_IN_SOURCE_BUILD="1"

src_prepare() {
	local debian_patches="${WORKDIR}/debian/patches"

	rm -v "${debian_patches}/no_README_LICENSE.mysql.diff"

	EPATCH_SUFFIX="diff"
	EPATCH_FORCE="yes"
	EPATCH_MULTI_MSG="Applying Debian patches"
	epatch "${debian_patches}"

	epatch "${FILESDIR}"/libdir.diff
	epatch "${FILESDIR}"/install_components.diff
	epatch "${FILESDIR}"/kill_fubar.diff
}

src_configure() {
	# native lib/wrapper needs this!
	append-flags "-fno-strict-aliasing"

	local mycmakeargs=(
		"-DMYSQLCPPCONN_BUILD_EXAMPLES=OFF"
		"-DMYSQLCPPCONN_ICU_ENABLE=OFF"
		$(cmake-utils_use debug MYSQLCPPCONN_TRACE_ENABLE)
		$(cmake-utils_use gcov MYSQLCPPCONN_GCOV_ENABLE)
	)

	# eclass/cmake-utils relies on this variable for various things
	# - how to do proper install targets? dynamic vs static? :)
	use static-libs && CMAKE_BUILD_TYPE="GentooFull"

	cmake-utils_src_configure
}

src_compile() {
	# make
	cmake-utils_src_compile mysqlcppconn

	# make static
	use static-libs && cmake-utils_src_compile mysqlcppconn-static
}

src_install() {
	# install
	emake DESTDIR="${D}" install/fast || die

	dodoc ANNOUNCE* CHANGES* README || die

	# examples
	if use examples; then
		insinto /usr/share/doc/${PF}/examples
		doins "${S}"/examples/* || die
	fi
}