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

EAPI=5

inherit autotools eutils versionator games

MY_PN="${PN/6/}"
MY_PV=$(version_format_string '$1.$2.$3$4')
MY_P="${PN}-${MY_PV}"

DESCRIPTION="unique multiplayer wargame"
HOMEPAGE="http://www.gnu.org/software/liquidwar6/"
SRC_URI="http://www.ufoot.org/download/${MY_PN}/v6/${MY_PV}/${PN}-${MY_PV}.tar.gz
		maps? ( http://www.ufoot.org/download/${MY_PN}/v6/${MY_PV}/${PN}-extra-maps-${MY_PV}.tar.gz )"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+allinone csound doc gtk +http +maps nls ogg opengl openmp readline static-libs"


# TODO patch for libpng 1.6
RDEPEND="dev-db/sqlite:3
	dev-lang/perl
	dev-libs/expat
	dev-scheme/guile
	net-misc/curl
	sys-libs/zlib
	csound? ( media-sound/csound )
	nls? ( virtual/libintl
		virtual/libiconv )
	opengl? ( media-libs/freetype:2
		media-libs/libpng:0
		media-libs/sdl-ttf
		media-libs/sdl-image
		virtual/jpeg
		virtual/opengl
		gtk? ( x11-libs/gtk+:2 ) )
	readline? ( sys-libs/ncurses
		sys-libs/readline )"
DEPEND="${RDEPEND}
	nls? ( sys-devel/gettext )"

S=${WORKDIR}/${MY_P}
MAPS="${WORKDIR}/${PN}-extra-maps-${MY_PV}"

pkg_setup() {
	if use openmp; then
		if [[ $(tc-getCC) == *gcc ]] && ! tc-has-openmp ; then
			ewarn "OpenMP is not available in your current selected gcc"
			die "need openmp capable gcc"
		fi
	fi
	games_pkg_setup
}

src_prepare() {
	epatch ${FILESDIR}/${P}-autotools.patch || die "Cannot fix autotools"
	eautoreconf
}

src_configure() {
	if ! use ogg || ! use csound; then
		myconf="${myconf} --enable-silent"
	fi

	egamesconf $(use_enable nls) \
		$(use_enable allinone) \
		$(usex allinone "--disable-shared" "--enable-shared") \
		$(usex opengl "$(use_enable opengl mod-gl) $(use_enable gtk)" "--disable-gtk --enable-headless") \
		$(use_enable openmp) \
		$(use_enable ogg mod-ogg) \
		$(use_enable http mod-http) \
		$(use_enable readline console) \
		$(use_enable csound mod-csound) \
		$(use_enable static-libs static) \
		${myconf}

	if use maps; then
		cd "${MAPS}" || die "Cannot access extra maps directory"
		egamesconf
	fi
}

src_compile() {
	games_src_compile

	if use doc; then
		emake html
	fi

	use maps &&	emake -C "${MAPS}"
}

src_install() {
	emake DESTDIR="${D}" install || die "Cannot install base"

	if use maps; then
		find . -name README -delete
		emake -C "${MAPS}" DESTDIR="${D}" install || die "Cannot install maps"
	fi

	prepgamesdirs
}