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

EAPI=6

inherit cmake-utils bash-completion-r1

GSHHG="gshhg-gmt-2.3.4"
DCW="dcw-gmt-1.1.2"

DESCRIPTION="Powerful map generator"
HOMEPAGE="http://gmt.soest.hawaii.edu/"
SRC_URI="
	mirror://gmt/${P}-src.tar.gz
	mirror://gmt/${GSHHG}.tar.gz
	mirror://gmt/${DCW}.tar.gz"

IUSE="debug +fftw +gpl lgpl pcre +threads"

LICENSE="LGPL-3 gpl? ( GPL-3 )
		!gpl? ( !lgpl? ( Shewchuk ) )"
SLOT="0"
KEYWORDS="~amd64 ~x86"

RDEPEND="
	!sci-biology/probcons
	app-text/ghostscript-gpl
	sci-libs/gdal
	sci-libs/netcdf[hdf5]
	fftw? ( sci-libs/fftw:3.0 )
	pcre? ( dev-libs/libpcre )
	threads? ( dev-libs/glib:2 )"
DEPEND="${RDEPEND}"

REQUIRED_USE="?? ( gpl lgpl ( !gpl !lgpl ) )"

src_configure() {
	license_restrict=off
	if use gpl ; then
		license_restrict=GPL
	elif use lgpl ; then
		license_restrict=LGPL
	fi

	local mycmakeargs=(
		-DGSHHG_ROOT="${WORKDIR}/${GSHHG}"
		-DDCW_ROOT="${WORKDIR}/${DCW}"
		-DLICENSE_RESTRICTED=${license_restrict}
		-DGMT_INSTALL_RELOCATABLE=true
		-DGMT_INSTALL_TRADITIONAL_FOLDERNAMES=off
		-DGMT_INSTALL_NAME_SUFFIX="-${PV}"
		-DCMAKE_DISABLE_FIND_PACKAGE_GDAL=off
		-DGMT_USE_THREADS=$(usex threads)
		$(cmake-utils_use_find_package pcre PCRE)
		$(cmake-utils_use_find_package fftw FFTW3)
	)

	cmake-utils_src_configure
}

src_install() {
	cmake-utils_src_install

	# Install bash completion files
	dobashcomp "${D}/usr/etc/bash_completion.d/gmt"
	rm "${D}/usr/etc/bash_completion.d/gmt" || die
	rmdir "${D}/usr/etc/bash_completion.d" \
		"${D}/usr/etc" || die
}