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

inherit eutils

DESCRIPTION="A small daemon to collect system statistics into RRD files."
HOMEPAGE="http://collectd.org/"
SRC_URI="http://collectd.org/files/${P}.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="apcupsd dns hddtemp ipmi iptables libvirt lm_sensors mysql
	netlink nut perl rrdtool snmp"

DEPEND="
	net-misc/curl
	sys-libs/libstatgrab
	apcupsd? ( sys-power/apcupsd )
	dns? ( net-libs/libpcap )
	hddtemp? ( app-admin/hddtemp )
	ipmi? ( sys-libs/openipmi )
	iptables? ( net-firewall/iptables )
	libvirt? ( app-emulation/libvirt )
	lm_sensors? ( >=sys-apps/lm_sensors-2.9.0 )
	mysql? ( >=dev-db/mysql-4.1 )
	netlink? ( sys-apps/iproute2 )
	nut? ( sys-power/nut )
	liboping? ( net-libs/liboping )
	pcap? ( net-libs/libpcap )
	rrdtool? ( >=net-analyzer/rrdtool-1.2 )
	snmp? ( net-analyzer/net-snmp )
	"

src_compile() {
	local myconf
	if ! built_with_use --missing true dev-lang/perl ithreads; then
		myconf="--disable-perl"
	else
		myconf="$(use_enable perl)"
	fi

	econf \
		--with-libstatgrab \
		$(use_enable apcups) \
		$(use_enable dns) \
		$(use_enable hddtemp) \
		$(use_enable ipmi) \
		$(use_enable iptables) \
		$(use_with iptables libiptc) \
		$(use_enable libvirt) \
		$(use_enable lm_sensors sensors) \
		$(use_enable mysql) \
		$(use_enable netlink) \
		$(use_enable nut) \
		$(use_enable rrdtool) \
		$(use_enable snmp) \
		${myconf} \
		KERNEL_DIR=/usr/src/linux \
		|| die
	emake || die
}

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

	dodoc AUTHORS ChangeLog README NEWS TODO

	docinto contrib
	dodoc contrib/*

	keepdir /var/lib/collectd

	newinitd "${FILESDIR}/${PN}-4.0.4.initd" collectd || die
	newconfd "${FILESDIR}/${PN}-4.0.4.confd" collectd || die

	insinto /etc
	newins "${FILESDIR}/${PN}-4.0.4.conf" collectd.conf || die
}

pkg_postinst() {
	einfo "collectd introduced some changes in the new 4.x series."
	einfo "For further information, read http://collectd.org/migrate-v3-v4.shtml"
	einfo "The migration script can be found at:"
	einfo "/usr/share/doc/${P}/contrib/migrate-3-4.px.bz2"
}