# Copyright 1999-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

PYTHON_COMPAT=( python3_{5,6,7} )
VALA_USE_DEPEND="vapigen"
inherit cmake-utils python-any-r1 vala

DESCRIPTION="An implementation of basic iCAL protocols"
HOMEPAGE="https://github.com/libical/libical"
SRC_URI="https://github.com/${PN}/${PN}/releases/download/v${PV}/${P}.tar.gz"

LICENSE="|| ( MPL-2.0 LGPL-2.1 )"
SLOT="0/3"
KEYWORDS="alpha amd64 arm arm64 ~hppa ia64 ~mips ppc ppc64 sparc x86 ~amd64-fbsd ~x86-fbsd ~amd64-linux ~x86-linux ~ppc-macos ~x86-macos ~x86-solaris"
IUSE="berkdb doc examples static-libs test +glib +vala +introspection"
REQUIRED_USE="vala? ( introspection )"

DEPEND="
	dev-libs/icu:=
	berkdb? ( sys-libs/db:= )
	glib? (
		dev-libs/glib:2
		dev-libs/libxml2:2
	)
	introspection? ( dev-libs/gobject-introspection:= )
	vala? ( $(vala_depend) )
"
BDEPEND="
	dev-lang/perl
	virtual/pkgconfig
	doc? ( app-doc/doxygen )
	test? ( ${PYTHON_DEPS} )
"
RDEPEND="${DEPEND}
	sys-libs/timezone-data
"

DOCS=(
	AUTHORS ReadMe.txt ReleaseNotes.txt TEST THANKS TODO
	doc/{AddingOrModifyingComponents,UsingLibical}.txt
)

pkg_setup() {
	use test && python-any-r1_pkg_setup
}

src_prepare() {
	use vala && vala_src_prepare
	cmake-utils_src_prepare
	use examples || cmake_comment_add_subdirectory examples
}

src_configure() {
	local mycmakeargs=(
		$(cmake-utils_use_find_package berkdb BDB)
		-DICAL_BUILD_DOCS=$(usex doc)
		-DSHARED_ONLY=$(usex !static-libs)
		-DICAL_GLIB=$(usex glib)
		-DICAL_GLIB_VAPI=$(usex vala)
		-DGOBJECT_INTROSPECTION=$(usex introspection)
	)
	if use vala; then
		mycmakeargs+=(
			-DVALAC="${VALAC}"
			-DVAPIGEN="${VAPIGEN}"
		)
	fi
	cmake-utils_src_configure
}

src_compile() {
	cmake-utils_src_compile
	use doc && cmake-utils_src_compile docs
}

src_test() {
	local myctestargs=(
		-E "(icalrecurtest|icalrecurtest-r)" # bug 660282
	)

	cmake-utils_src_test
}

src_install() {
	use doc && HTML_DOCS=( "${BUILD_DIR}"/apidocs/html/. )

	cmake-utils_src_install

	if use examples; then
		rm examples/CMakeLists.txt || die
		dodoc -r examples
	fi
}

pkg_postinst() {
	# workaround for incorrect pkgconfig on 17.1 profile
	if [[ -f "/usr/lib64/pkgconfig/libical-glib.pc" ]]; then
		sed -i -e 's|libdir=${exec_prefix}/lib|libdir=${exec_prefix}/lib64|' /usr/lib64/pkgconfig/libical-glib.pc
	fi
	if [[ -f "/usr/lib64/pkgconfig/libical.pc" ]]; then
		sed -i -e 's|libdir=${exec_prefix}/lib|libdir=${exec_prefix}/lib64|' /usr/lib64/pkgconfig/libical.pc
	fi
}