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

EAPI=7

PYTHON_COMPAT=( python3_{7,8,9} )
inherit autotools xdg-utils perl-module distutils-r1

DESCRIPTION="Library for programming the Logitech Harmony universal remote"
HOMEPAGE="https://www.phildev.net/concordance"
SRC_URI="https://github.com/jaymzh/concordance/archive/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"
IUSE="perl python static-libs"

DEPEND="perl? ( >=dev-lang/swig-2:= )
	python? ( ${PYTHON_DEPS} )"
RDEPEND="dev-libs/hidapi
	dev-libs/libzip"

S="${WORKDIR}/concordance-${PV}/${PN}"

src_prepare() {
	default

	eautoreconf

	if use perl ; then
		cd "${S}/bindings/perl"
		einfo "Prepare perl bindings in ${PWD}"
		perl-module_src_prepare
		swig -perl5 concord.i
		cd "${S}"
	fi

	if use python ; then
		cd "${S}/bindings/python"
		einfo "Prepare python bindings in ${PWD}"
		distutils-r1_src_prepare
		cd "${S}"
	fi
}

src_configure() {
	econf \
		--disable-mime-update \
		$(use_enable static-libs static)

	if use perl ; then
		cd "${S}/bindings/perl"
		einfo "Configure perl bindings in ${PWD}"
		perl-module_src_configure
		cd "${S}"
	fi
}

src_compile() {
	default

	if use perl ; then
		cd "${S}/bindings/perl"
		einfo "Compile perl bindings in ${PWD}"
		mymake=( OPTIMIZE="${CFLAGS} -I${S}" )
		perl-module_src_compile
		cd "${S}"
	fi

	if use python ; then
		cd "${S}/bindings/python"
		einfo "Prepare python bindings in ${PWD}"
		distutils-r1_src_compile
		cd "${S}"
	fi
}

src_install() {
	emake install DESTDIR="${ED}"

	dodoc README
	dodoc ../Changelog ../TODO

	if use perl ; then
		cd "${S}/bindings/perl"
		einfo "Install perl bindings in ${PWD}"
		perl-module_src_install
		cd "${S}"
	fi

	if use python ; then
		cd "${S}/bindings/python"
		einfo "Install python bindings in ${PWD}"
		distutils-r1_src_install
		cd "${S}"
	fi
}

pkg_postinst() {
	xdg_mimeinfo_database_update
}