# Makefile for NDOUtils
#
# Copyright 2009-2014 Nagios Core Development Team and Community Contributors
# Copyright 2005-2009 Ethan Galstad
#
# This file is part of NDOUtils.
#
# NDOUtils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# NDOUtils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NDOUtils. If not, see <http://www.gnu.org/licenses/>.


# Source code directories
SRC_BASE=./src
SRC_INCLUDE=./include
SRC_CONFIG=./config

prefix=@prefix@
exec_prefix=@exec_prefix@
PIPEDIR=@localstatedir@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
LIBEXECDIR=@libexecdir@

NDO2DB_USER=@ndo2db_user@
NDO2DB_GROUP=@ndo2db_group@

INSTALL=@INSTALL@
GREP=@GREP@
EGREP=@EGREP@

OPSYS=@opsys@
DIST=@dist_type@

INIT_TYPE=@init_type@
INIT_DIR=@initdir@
INIT_OPTS=-o root -g @root_grp@
INIT_FILE=@initname@
INETD_TYPE=@inetd_type@
INETD_DIR=@inetddir@
INETD_FILE=@inetdname@
SRC_INETD=@src_inetd@
SRC_INIT=@src_init@

default:
	@echo;\
	echo Please enter 'make [option]' where [option] is one of:;\
	echo;\
	echo "     all                  builds the NDO Utilities";\
	echo "     ndo2db               builds the ndo2db daemon";\
	echo "     ndomod               builds the ndomod nagios module";\
	echo "     file2sock            builds the file2sock utility";\
	echo "     log2ndo              builds the log2ndo utility";\
	echo "     sockdebug            builds the sockdebug utility";\
	echo "     install-groups-users add the user and group if they do not exist";\
	echo "     install              installs the module and programs";\
	echo "     install-config       installs the sample configuration files";\
	echo "     install-inetd        install the startup files for inetd, launchd, etc.";\
	echo "     install-init         install the startup files for init, systemd, etc.";\
	echo "     fullinstall          runs install-groups-users, install,";\
	echo "                          install-config and install-init";\
	echo

all:
	cd $(SRC_BASE) && $(MAKE)

ndo2db:
	cd $(SRC_BASE); $(MAKE) $@

ndomod:
	cd $(SRC_BASE); $(MAKE) $@

file2sock:
	cd $(SRC_BASE); $(MAKE) $@

log2ndo:
	cd $(SRC_BASE); $(MAKE) $@

sockdebug:
	cd $(SRC_BASE); $(MAKE) $@

ctags:
	ctags -R

install:
	cd $(SRC_BASE) && $(MAKE) $@
	@echo ""
	@echo "Main NDOUtils components installed"
	@echo ""

install-config:
	$(INSTALL) -m 775 -d $(DESTDIR)$(CFGDIR)
	$(INSTALL) -m 775 -d $(DESTDIR)$(PIPEDIR)
	$(INSTALL) -m 644 config/ndo2db.cfg-sample $(DESTDIR)$(CFGDIR)
	$(INSTALL) -m 644 config/ndomod.cfg-sample $(DESTDIR)$(CFGDIR)
	@echo ""
	@echo "*** Config files installed ***"
	@echo ""
	@echo "Remember, these are *SAMPLE* config files.  You'll need to rename"
	@echo "the files in order to use them."
	@echo "Please read the documentation to know what they are doing."
	@echo ""

install-groups-users:
	@macros/add_group_user $(DIST) $(NDO2DB_USER) $(NDO2DB_GROUP)

install-init:
	@if test $(SRC_INIT) = unknown; then \
		echo No init file to install; \
		exit 1; \
	fi
	@if test $(INIT_TYPE) = upstart; then\
		echo $(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		$(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		echo initctl reload-configuration; \
		initctl reload-configuration; \
	elif test $(INIT_TYPE) = systemd; then\
		echo $(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		$(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
	elif test $(INIT_TYPE) = smf10 -o $(INIT_TYPE) = smf11; then \
		echo $(INSTALL) -m 775 -g sys -d $(INIT_DIR);\
		$(INSTALL) -m 775 -g sys -d $(INIT_DIR);\
		echo $(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		$(INSTALL) -m 644 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		echo svccfg import $(INIT_DIR)/$(INIT_FILE); \
		svccfg import $(INIT_DIR)/$(INIT_FILE); \
		echo "*** Run 'svcadm enable ndo2db' to start it"; \
	else\
		echo $(INSTALL) -m 755 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		$(INSTALL) -m 755 startup/$(SRC_INIT) $(INIT_DIR)/$(INIT_FILE); \
		if test $(INIT_TYPE) = newbsd; then\
			if test $(DIST) = openbsd; then\
				echo "# ndo2db@bsd_enable@=NO" >> /etc/rc.conf;\
				echo "ndo2db@bsd_enable@=\"-d -c $(CFGDIR)/ndo2db.cfg\"" >> /etc/rc.conf;\
				echo "Make sure to enable the ndo2db daemon";\
			else\
				echo "ndo2db@bsd_enable@=YES" >> /etc/rc.conf;\
				echo "ndo2db_configfile=$(CFGDIR)/ndo2db.cfg" >> /etc/rc.conf;\
			fi;\
		elif test $(INIT_TYPE) = launchd; then\
			launchctl load $(INIT_DIR)/$(INIT_FILE); \
		else\
			if test -f /sbin/chkconfig ; then \
			    /sbin/chkconfig ndo2db on;\
			else\
				echo "Make sure to enable the ndo2db daemon";\
			fi;\
		fi;\
	fi

install-inetd:
	@if test $(SRC_INETD) = unknown; then \
		echo No inetd file to install; \
		exit 1; \
	fi
	@if test $(INETD_TYPE) = inetd; then \
		$(EGREP) -q "^\W*nrpe\s+" $(INETD_DIR)/$(INETD_FILE) 2>/dev/null || \
		   cat startup/$(SRC_INETD) >> $(INETD_DIR)/$(INETD_FILE); \
	elif test $(INETD_TYPE) = systemd; then \
		SRC_INETD_FILE=`echo "$(SRC_INETD)" | sed -e 's/socket/socket-svc/'`; \
		echo $(INSTALL) -m 644 startup/$$SRC_INETD_FILE $(INETD_DIR)/$(INIT_FILE); \
		$(INSTALL) -m 644 startup/$$SRC_INETD_FILE $(INETD_DIR)/$(INIT_FILE); \
	elif test $(INETD_TYPE) = smf10 -o $(INETD_TYPE) = smf11; then \
		echo $(INSTALL) -m 775 -g sys -d $(INETD_DIR);\
		$(INSTALL) -m 775 -g sys -d $(INETD_DIR);\
		echo $(INSTALL) -m 644 startup/$(SRC_INETD) $(INETD_DIR)/$(INETD_FILE); \
		$(INSTALL) -m 644 startup/$(SRC_INETD) $(INETD_DIR)/$(INETD_FILE); \
		$(INSTALL) -m 775 -d $(INETD_DIR);\
		echo svccfg import $(INETD_DIR)/$(INETD_FILE); \
		svccfg import $(INETD_DIR)/$(INETD_FILE); \
		echo "*** Run 'svcadm enable nrpe' to start it"; \
	elif test $(INIT_TYPE) = launchd; then\
		$(INSTALL) -m 644 startup/$(SRC_INETD) $(INETD_DIR)/$(INETD_FILE); \
		launchctl load $(INETD_DIR)/$(INETD_FILE); \
	else\
		echo $(INSTALL) -m 644 startup/$(SRC_INETD) $(INETD_DIR)/$(INETD_FILE); \
		$(INSTALL) -m 644 startup/$(SRC_INETD) $(INETD_DIR)/$(INETD_FILE); \
	fi
	@$(EGREP) -q "^nrpe[\t ]+@ndo2db_port@/tcp" /etc/services || \
		echo "***** MAKE SURE 'ndo2db @ndo2db_port@/tcp' IS IN YOUR /etc/services FILE"

fullinstall: install-groups-users install install-config install-init

clean:
	cd $(SRC_BASE) && $(MAKE) $@
	rm -f core
	rm -f *~ */*~ include/nagios-*/*~

distclean: clean
	cd $(SRC_BASE) && $(MAKE) $@; cd ..
	cd docs/docbook/en-en/ && $(MAKE) $@
	rm -f config.log config.status config.cache 
	rm -f $(SRC_INCLUDE)/dh.h $(SRC_INCLUDE)/config.h
	rm -f $(SRC_CONFIG)/ndo2db.cfg-sample $(SRC_CONFIG)/ndomod.cfg-sample $(SRC_CONFIG)/nagios.cfg $(SRC_CONFIG)/misccommands.cfg
	rm -f startup/bsd-init startup/debian-init startup/default-init startup/default-inetd
	rm -f startup/default-service startup/default-socket startup/default-socket-svc
	rm -f startup/default-xinetd startup/mac-init.plist startup/mac-inetd.plist
	rm -f startup/newbsd-init startup/openbsd-init startup/openrc-conf
	rm -f startup/openrc-init startup/rh-upstart-init startup/solaris-init.xml
	rm -f startup/solaris-inetd.xml startup/tmpfile.conf startup/upstart-init
	rm -f Makefile
	rm -f tags
	rm -rf autom4te.cache/

devclean: distclean

