#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# for hardening
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
include /usr/share/dpkg/architecture.mk

DEBIANDIR=$(CURDIR)/debian
NP_LIBEXEC:=/usr/lib/nagios/plugins
NP_STD_DIR:=$(DEBIANDIR)/monitoring-plugins-standard

ifeq ($(DEB_HOST_ARCH_OS),linux)
PING_CONFIGURE_ARGS = --with-ping-command="/bin/ping -4 -n -U -w %d -c %d %s" \
                      --with-ping6-command="/bin/ping -6 -n -U -w %d -c %d %s"
else
PING_CONFIGURE_ARGS = --with-ping-command="/bin/ping -n -w %d -c %d %s" \
                      --with-ping6-command="/bin/ping6 -n -w %d -c %d %s"
endif

%:
	dh $@

override_dh_auto_configure:
	# if /proc is not mounted, check_nagios and check_procs are
	# silently omitted from build.
	@if ! [ -d /proc/$$$$ ]; then \
	  echo "monitoring-plugins needs a mounted /proc to correctly build"; \
	  exit 1; \
	fi

	CFLAGS="$$(DEB_CFLAGS_MAINT_APPEND='-Wall -Wl,-z,now' dpkg-buildflags --get CFLAGS)" \
	dh_auto_configure -- \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--libexecdir=${NP_LIBEXEC} \
		--with-perl=/usr/bin/perl \
		--with-openssl=/usr \
		--with-pgsql=/usr \
		--with-mysql=/usr \
		--with-proc-loadavg=/proc/loadavg \
		--with-ps-command="/bin/ps axwwo 'stat uid pid ppid vsz rss pcpu etime comm args'" \
		--with-ps-format="%s %d %d %d %d %d %f %s %s %n" \
		--with-ps-cols=10 \
		--with-ps-varlist="procstat,&procuid,&procpid,&procppid,&procvsz,&procrss,&procpcpu,procetime,procprog,&pos" \
		$(PING_CONFIGURE_ARGS) \
		--with-nslookup-command="/usr/bin/nslookup -sil" \
		--with-uptime-command="/usr/bin/uptime" \
		--with-rpcinfo-command="/usr/sbin/rpcinfo" \
		--with-smbclient-command="/usr/bin/smbclient" \
		--with-snmpget-command="/usr/bin/snmpget" \
		--with-snmpgetnext-command="/usr/bin/snmpgetnext" \
		--with-qstat-command="/usr/bin/quakestat" \
		--with-fping-command="/usr/bin/fping" \
		--with-fping6-command="/usr/bin/fping6" \
		--with-ssh-command="/usr/bin/ssh" \
		--with-sudo-command="/usr/bin/sudo" \
		--with-mailq-command="/usr/bin/mailq" \
		--with-proc-meminfo="/proc/meminfo" \
		--with-dig-command="/usr/bin/dig" \
		--with-apt-get-command="/usr/bin/apt-get" \
		--enable-extra-opts \
		--disable-rpath

override_dh_auto_test:

execute_after_dh_auto_install:
	chmod 644 debian/tmp/usr/lib/nagios/plugins/utils.pm

	# rename check_http to check_http_deprecated for making use of update-alternatives
	mv debian/tmp${NP_LIBEXEC}/check_http debian/tmp${NP_LIBEXEC}/check_http.deprecated

	# remove check_uptime for now (conflicting with check_uptime from n-p-c)
	rm debian/tmp${NP_LIBEXEC}/check_uptime

	# generate debian/README.Debian.plugins
	if [ -e "${NP_STD_DIR}${NP_LIBEXEC}" ]; then \
		perl ./debian/bin/gen_plugin_deps.pl; \
	fi

override_dh_installchangelogs:
	dh_installchangelogs -p monitoring-plugins-basic -a ChangeLog
	dh_installchangelogs -N monitoring-plugins-basic

override_dh_shlibdeps:
	dh_shlibdeps -N monitoring-plugins-standard

	dpkg-shlibdeps -Tdebian/monitoring-plugins-standard.substvars \
		${NP_STD_DIR}${NP_LIBEXEC}/check_dns
	dpkg-shlibdeps -Tdebian/monitoring-plugins-standard.substvars.in \
		-dSuggests ${NP_STD_DIR}${NP_LIBEXEC}/check_*

	grep shlibs:Suggests debian/monitoring-plugins-standard.substvars.in \
		| sed -e 's/shlibs:Suggests/shlibs:Recommends/' \
		>> debian/monitoring-plugins-standard.substvars
