# Distributed under the terms of the GNU General Public License v2

EAPI="1"

inherit eutils toolchain-funcs autotools

DESCRIPTION="NetXMS is new and rapidly developing monitoring system"
HOMEPAGE="http://netxms.org/"
SRC_URI="http://www.netxms.org/download/netxms-0.2.22.tar.gz"

IUSE="server client console agent static-agent ipso-agent nxhttpd mdebug sqlite odbc oracle mysql pgsql openssl gd unstable debug"
SLOT="0"
LICENSE="GPL-2"
KEYWORDS="~x86 ~amd64"

RDEPEND="
"
DEPEND="${COMMON_DEPEND}
	sys-devel/autoconf:2.5
	sys-devel/automake:1.10
	mysql? ( virtual/mysql )
	openssl? ( dev-libs/openssl )
	postgres? ( virtual/postgresql-base )
	console? ( x11-libs/wxGTK )
	gd? ( media-libs/gd )
	sqlite? ( >=dev-db/sqlite-3.4.1 )
"

S="${WORKDIR}/${P/_/}"

src_unpack(){
	unpack ${P/_/}.tar.gz
	cd "${S}"
	eautoconf
	eautoheader
	eautomake
	elibtoolize
}

src_compile() {
	econf \
	$(useq server && echo --with-server ) \
	$(useq client && echo --with-client ) \
	$(useq agent && echo --with-agent ) \
	$(useq console && echo --with-console ) \
	$(useq static-agent && echo --with-static-agent ) \
	$(useq ipso-agent && echo --with-ipso-agent ) \
	$(useq nxhttpd && echo --with-nxhttpd ) \
	$(useq mdebug && echo --with-mdebug ) \
	$(useq sqlite && echo --with-sqlite ) \
	$(useq odbc && echo --with-odbc ) \
	$(useq oracle && echo --with-oracle ) \
	$(useq mysql && echo --with-mysql ) \
	$(useq pgsql && echo --with-pgsql ) \
	$(use_enable openssl encryption) \
	$(useq gd && echo --with-gd ) \
	$(useq unstable && echo --enable-unstable ) \
	$(useq debug && echo --enable-debug ) || die "econf failed"
	emake CONFDIR="/etc/netxms" || die "emake failed"
}

src_install() {
	emake DESTDIR="${D}" CONFDIR="${D}/etc/netxms" install || die "install failed"
}
pkg_postinst() {
	dodir /etc/netxms
	keepdir /etc/netxms
	if ( use server )
	then
		if ( test -e /etc/netxms/netxmsd.conf )
		then
		elog "Keeping your existing config /etc/netxms/netxmsd.conf"
		else
		elog "Installing initial server config file to /etc/netxms/netxmsd.conf"
		cp ${FILESDIR}/netxmsd.conf /etc/netxms/netxmsd.conf
		fi
	fi
	if ( use agent )
	then
		if ( test -e /etc/netxms/nxagentd.conf )
		then
		elog "Keeping your existing config /etc/netxms/nxagentd.conf"
		else
		elog "Installing initial server config file to /etc/netxms/nxagentd.conf"
		cp ${FILESDIR}/nxagentd.conf /etc/netxms/nxagentd.conf
		fi
	fi
	$(useq server && cp ${FILESDIR}/netxmsd /etc/init.d/netxmsd )
	$(useq agent && cp ${FILESDIR}/nxagentd /etc/init.d/nxagentd )
	elog
	elog "Startup scripts installed to /etc/init.d"
	elog
}

