# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-analyzer/nrpe/nrpe-2.13-r4.ebuild,v 1.8 2012/12/16 15:21:39 armin76 Exp $

EAPI=4

inherit eutils toolchain-funcs multilib user autotools

DESCRIPTION="Nagios Remote Plugin Executor"
HOMEPAGE="http://www.nagios.org/"
SRC_URI="mirror://sourceforge/nagios/${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="alpha amd64 hppa ppc ppc64 sparc x86"
IUSE="command-args ssl tcpd minimal"

DEPEND="ssl? ( dev-libs/openssl )
	!minimal? ( tcpd? ( sys-apps/tcp-wrappers ) )"
RDEPEND="${DEPEND}
	!minimal? ( >=net-analyzer/nagios-plugins-1.3.0 )"

pkg_setup() {
	enewgroup nagios
	enewuser nagios -1 /bin/bash /dev/null nagios

	elog "If you plan to use \"nrpe_check_control\" then you may want to specify"
	elog "different command and services files. You can override the defaults"
	elog "through the \"NAGIOS_COMMAND_FILE\" and \"NAGIOS_SERVICES_FILE\" environment variables."
	elog "NAGIOS_COMMAND_FILE=${NAGIOS_COMMAND_FILE:-/var/rw/nagios.cmd}"
	elog "NAGIOS_SERVICES_FILE=${NAGIOS_SERVICES_FILE:-/etc/services.cfg}"
}

src_prepare() {
	# Add support for large output,
	# http://opsview-blog.opsera.com/dotorg/2008/08/enhancing-nrpe.html
	epatch "${FILESDIR}/nagios-nrpe-2.13-multiline.patch"

	# TCP wrappers conditional, bug 326367
	epatch "${FILESDIR}/nagios-nrpe-2.13-tcpd.patch"
	# Make command-args really conditional, bug 397603
	epatch "${FILESDIR}/nagios-nrpe-2.13-command-args.patch"

	sed -i -e '/define \(COMMAND\|SERVICES\)_FILE/d' contrib/nrpe_check_control.c || die

	eautoreconf
}

src_configure() {
	local myconf
	if use minimal; then
		myconf="--disable-tcp-wrapper --disable-command-args"
	else
		myconf="$(use_enable tcpd tcp-wrapper) $(use_enable command-args)"
	fi

	econf \
		--libexecdir=/usr/$(get_libdir)/nagios/plugins \
		--localstatedir=/var/nagios \
		--sysconfdir=/etc/nagios \
		--with-nrpe-user=nagios \
		--with-nrpe-group=nagios \
		$(use_enable ssl) \
		${myconf}
}

src_compile() {
	emake -C src check_nrpe $(use minimal || echo nrpe)

	# Add nifty nrpe check tool
	$(tc-getCC) ${CPPFLAGS} ${CFLAGS} \
		-DCOMMAND_FILE=\"${NAGIOS_COMMAND_FILE:-/var/rw/nagios.cmd}\" \
		-DSERVICES_FILE=\"${NAGIOS_SERVICES_FILE:-/etc/services.cfg}\" \
		${LDFLAGS} -o nrpe_check_control contrib/nrpe_check_control.c || die
}

src_install() {
	dodoc LEGAL Changelog README SECURITY \
		contrib/README.nrpe_check_control \
		$(use ssl && echo README.SSL)

	exeinto /usr/$(get_libdir)/nagios/plugins
	doexe src/check_nrpe nrpe_check_control

	use minimal && return 0

	## NON-MINIMAL INSTALL FOLLOWS ##

	insinto /etc/nagios
	newins sample-config/nrpe.cfg nrpe.cfg
	fowners root:nagios /etc/nagios/nrpe.cfg
	fperms 0640 /etc/nagios/nrpe.cfg

	exeinto /usr/libexec
	doexe src/nrpe

	newinitd "${FILESDIR}"/nrpe.init nrpe

	insinto /etc/xinetd.d/
	newins "${FILESDIR}/nrpe.xinetd.2" nrpe

	if use tcpd; then
		sed -i -e '/^reload()/, /^}/ d' -e '/extra_started_commands/s:reload::' \
			"${D}"/etc/init.d/nrpe
	fi
}

pkg_postinst() {
	elog "If you are using the nrpe daemon, remember to edit"
	elog "the config file /etc/nagios/nrpe.cfg"

	if use command-args ; then
		ewarn ""
		ewarn "You have enabled command-args for NRPE. This enables"
		ewarn "the ability for clients to supply arguments to commands"
		ewarn "which should be run. "
		ewarn "THIS IS CONSIDERED A SECURITY RISK!"
		ewarn "Please read /usr/share/doc/${PF}/SECURITY.bz2 for more info"
	fi
}