###############################
# Makefile for Nagios
#
# Last Modified: 10-25-2002
###############################


# Source code directories
SRC_BASE=@srcdir@/base
SRC_CGI=@srcdir@/cgi
SRC_HTM=@srcdir@/html
SRC_COMMON=@srcdir@/common
SRC_XDATA=@srcdir@/xdata
SRC_CONTRIB=@srcdir@/contrib

CC=@CC@
CFLAGS=@CFLAGS@ @DEFS@
LDFLAGS=@LDFLAGS@ @LIBS@

prefix=@prefix@
exec_prefix=@exec_prefix@
LOGDIR=@localstatedir@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
CGIDIR=@sbindir@
HTMLDIR=@datadir@
INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@
COMMAND_OPTS=@COMMAND_OPTS@
INIT_DIR=@init_dir@
INIT_OPTS=-o root -g root
CGICFGDIR=$(CGIDIR)
PERLDIR=@PERLDIR@

INSTALLPERLSTUFF=@INSTALLPERLSTUFF@

CGIEXTRAS=@CGIEXTRAS@

SNPRINTF_O=@SNPRINTF_O@

CP=@CP@

@SET_MAKE@

none:
		@echo "Please supply a command line argument (i.e. 'make all').  Other targets are:"
		@echo "   nagios cgis contrib"
		@echo "   clean"
		@echo "   install install-html install-config install-init install-commandmode fullinstall"
#		@echo "   uninstall"

# FreeBSD make does not support -C option, so we'll use the Apache style... (patch by Stanley Hopcroft 12/27/1999)

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

	@echo ""
	@echo "*** Compile finished ***"
	@echo ""
	@echo "If the main program and CGIs compiled without any errors, you"
	@echo "can continue with installing Nagios as follows (type 'make'"
	@echo "without any arguments for a list of all possible options):"
	@echo ""
	@echo "  make install"
	@echo "     - This installs the main program, CGIs, and HTML files"
	@echo ""
	@echo "  make install-init"
	@echo "     - This installs the init script in $(DESTDIR)$(INIT_DIR)"
	@echo ""
	@echo "  make install-commandmode"
	@echo "     - This installs and configures permissions on the"
	@echo "       directory for holding the external command file"
	@echo ""
	@echo "  make install-config"
	@echo "     - This installs *SAMPLE* config files in $(DESTDIR)$(CFGDIR)"
	@echo "       You'll have to modify these sample files before you can"
	@echo "       use Nagios.  Read the HTML documentation for more info"
	@echo "       on doing this.  Pay particular attention to the docs on"
	@echo "       object configuration files, as they determine what/how"
	@echo "       things get monitored!"
	@echo ""
	@echo ""
	@echo "*** Support Notes *******************************************"
	@echo ""
	@echo "If you have questions about configuring or running Nagios,"
	@echo "please make sure that you:"
	@echo ""
	@echo "     - Look at the sample config files"
	@echo "     - Read the HTML documentation"
	@echo "     - Read the FAQs online at http://www.nagios.org/faqs"
	@echo ""
	@echo "before you post a question to one of the mailing lists."
	@echo "Also make sure to include pertinent information that could"
	@echo "help others help you.  This might include:"
	@echo ""
	@echo "     - What version of Nagios you are using"
	@echo "     - What version of the plugins you are using"
	@echo "     - Relevant snippets from your config files"
	@echo "     - Relevant error messages from the Nagios log file"
	@echo ""
	@echo "For those of you who are interested in contract support or"
	@echo "consulting services for Nagios, please visit:"
	@echo ""
	@echo "       http://www.nagios.org/contractsupport"
	@echo ""
	@echo "*************************************************************"
	@echo ""
	@echo "Enjoy."
	@echo ""

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

config:
	@echo "Sample config files are automatically generated once you run the"
	@echo "configure script.  You can install the sample config files on your"
	@echo "system by using the 'make install-config' command."

cgis:
	cd $(SRC_CGI) && $(MAKE)

html:
	cd $(SRC_HTM) && $(MAKE)

contrib:
	cd $(SRC_CONTRIB) && $(MAKE)

clean:
	cd $(SRC_BASE) && $(MAKE) $@
	cd $(SRC_CGI) && $(MAKE) $@
	cd $(SRC_COMMON) && $(MAKE) $@
	cd $(SRC_XDATA) && $(MAKE) $@
	cd $(SRC_HTM) && $(MAKE) $@
	cd $(SRC_CONTRIB) && $(MAKE) $@
	rm -f *.cfg *.sub core
	rm -f *~ */*~
	rm -f $(SRC_COMMON)/config.h $(SRC_COMMON)/locations.h $(SRC_COMMON)/snprintf.h 
	rm -f sample-config/*.cfg sample-config/template-object/*.cfg sample-config/default-object/*.cfg
	rm -f daemon-init pkginfo
	rm -f config.log config.status config.cache Makefile

distclean: clean

devclean: clean

install-html:
	cd $(SRC_HTM) && $(MAKE) install

install:
	cd $(SRC_BASE) && $(MAKE) $@
	cd $(SRC_CGI) && $(MAKE) $@
	cd $(SRC_HTM) && $(MAKE) $@
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(LOGDIR)
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(LOGDIR)/archives
	if [ $(INSTALLPERLSTUFF) = yes ]; then \
		$(INSTALL) -m 664 $(INSTALL_OPTS) p1.pl $(DESTDIR)$(BINDIR); \
	fi;

	@echo ""
	@echo "*** Main program, CGIs and HTML files installed ***"
	@echo ""
	@echo "You can continue with installing Nagios as follows (type 'make'"
	@echo "without any arguments for a list of all possible options):"
	@echo ""
	@echo "  make install-init"
	@echo "     - This installs the init script in $(DESTDIR)$(INIT_DIR)"
	@echo ""
	@echo "  make install-commandmode"
	@echo "     - This installs and configures permissions on the"
	@echo "       directory for holding the external command file"
	@echo ""
	@echo "  make install-config"
	@echo "     - This installs *SAMPLE* config files in $(DESTDIR)$(CFGDIR)"
	@echo "       You'll have to modify these sample files before you can"
	@echo "       use Nagios.  Read the HTML documentation for more info"
	@echo "       on doing this.  Pay particular attention to the docs on"
	@echo "       object configuration files, as they determine what/how"
	@echo "       things get monitored!"
	@echo ""


install-config:
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(CFGDIR)
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/nagios.cfg $(DESTDIR)$(CFGDIR)/nagios.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/cgi.cfg $(DESTDIR)$(CFGDIR)/cgi.cfg-sample
	$(INSTALL) -m 660 $(INSTALL_OPTS) sample-config/resource.cfg $(DESTDIR)$(CFGDIR)/resource.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/checkcommands.cfg $(DESTDIR)$(CFGDIR)/checkcommands.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/contactgroups.cfg $(DESTDIR)$(CFGDIR)/contactgroups.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/contacts.cfg $(DESTDIR)$(CFGDIR)/contacts.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/dependencies.cfg $(DESTDIR)$(CFGDIR)/dependencies.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/escalations.cfg $(DESTDIR)$(CFGDIR)/escalations.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/hostgroups.cfg $(DESTDIR)$(CFGDIR)/hostgroups.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/hosts.cfg $(DESTDIR)$(CFGDIR)/hosts.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/misccommands.cfg $(DESTDIR)$(CFGDIR)/misccommands.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/services.cfg $(DESTDIR)$(CFGDIR)/services.cfg-sample
	$(INSTALL) -m 664 $(INSTALL_OPTS) sample-config/template-object/timeperiods.cfg $(DESTDIR)$(CFGDIR)/timeperiods.cfg-sample

	@echo ""
	@echo "*** Sample config file installed ***"
	@echo ""
	@echo "Remeber, these are *sample* config files.  You'll need to read"
	@echo "the documentation for more information on how to actually define"
	@echo "services, hosts, etc. to fit your particular needs."
	@echo ""
	@echo "If you have questions about configuring Nagios properly, please:"
	@echo "       - Look at the sample config files"
	@echo "       - Read the HTML documentation"
	@echo "       - Read the FAQs online at http://www.nagios.org/faqs"
	@echo "*BEFORE* you post a question to one of the mailing lists."
	@echo ""

install-init: install-daemoninit

install-daemoninit:
	$(INSTALL) -m 755 -d $(INIT_OPTS) $(DESTDIR)$(INIT_DIR)
	$(INSTALL) -m 774 $(INIT_OPTS) daemon-init $(DESTDIR)$(INIT_DIR)/nagios

	@echo ""
	@echo "*** Init script installed ***"
	@echo ""
	@echo "You can continue with installing Nagios as follows (type 'make'"
	@echo "without any arguments for a list of all possible options):"
	@echo ""
	@echo "  make install-commandmode"
	@echo "     - This installs and configures permissions on the"
	@echo "       directory for holding the external command file"
	@echo ""
	@echo "  make install-config"
	@echo "     - This installs *SAMPLE* config files in $(DESTDIR)$(CFGDIR)"
	@echo "       You'll have to modify these sample files before you can"
	@echo "       use Nagios.  Read the HTML documentation for more info"
	@echo "       on doing this.  Pay particular attention to the docs on"
	@echo "       object configuration files, as they determine what/how"
	@echo "       things get monitored!"
	@echo ""

install-commandmode:
	$(INSTALL) -m 775 $(COMMAND_OPTS) -d $(DESTDIR)$(LOGDIR)/rw
	chmod g+s $(DESTDIR)$(LOGDIR)/rw

	@echo ""
	@echo "*** External command directory configured ***"
	@echo ""
	@echo "You can continue with installing Nagios as follows (type 'make'"
	@echo "without any arguments for a list of all possible options):"
	@echo ""
	@echo "  make install-config"
	@echo "     - This installs *SAMPLE* config files in $(DESTDIR)$(CFGDIR)"
	@echo "       You'll have to modify these sample files before you can"
	@echo "       use Nagios.  Read the HTML documentation for more info"
	@echo "       on doing this.  Pay particular attention to the docs on"
	@echo "       object configuration files, as they determine what/how"
	@echo "       things get monitored!"
	@echo ""

fullinstall: install install-init install-commandmode

# Uninstall is too destructive if base install directory is /usr, etc.
#uninstall:
#	rm -rf $(DESTDIR)$(BINDIR)/nagios $(DESTDIR)$(CGIDIR)/*.cgi $(DESTDIR)$(CFGDIR)/*.cfg $(DESTDIR)$(HTMLDIR)

#
# Targets for creating packages on various architectures
#

# Solaris pkgmk
PACKDIR=@PACKDIR@
VERSION=@VERSION@

Prototype:
	if [ ! -d $(PACKDIR) ] ; then mkdir $(PACKDIR); fi
	if [ ! -d $(PACKDIR)/etc ] ; then mkdir $(PACKDIR)/etc; fi
	if [ ! -d $(PACKDIR)/etc/init.d ] ; then mkdir $(PACKDIR)/etc/init.d; fi
	if [ ! -d $(PACKDIR)/etc/nagios ] ; then mkdir $(PACKDIR)/etc/nagios; fi
	$(MAKE) all
	$(MAKE) DESTDIR=$(PACKDIR) INIT_OPTS='' INSTALL_OPTS='' COMMAND_OPTS='' nagios_grp='' nagios_usr='' fullinstall
	$(INSTALL) -m 644 sample-config/nagios.cfg $(PACKDIR)$(CFGDIR)/nagios.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/cgi.cfg $(PACKDIR)$(CFGDIR)/cgi.cfg.$(VERSION)
	$(INSTALL) -m 640 sample-config/resource.cfg $(PACKDIR)$(CFGDIR)/resource.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/checkcommands.cfg $(PACKDIR)$(CFGDIR)/checkcommands.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/contactgroups.cfg $(PACKDIR)$(CFGDIR)/contactgroups.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/contacts.cfg $(PACKDIR)$(CFGDIR)/contacts.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/dependencies.cfg $(PACKDIR)$(CFGDIR)/dependencies.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/escalations.cfg $(PACKDIR)$(CFGDIR)/escalations.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/hostgroups.cfg $(PACKDIR)$(CFGDIR)/hostgroups.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/hosts.cfg $(PACKDIR)$(CFGDIR)/hosts.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/misccommands.cfg $(PACKDIR)$(CFGDIR)/misccommands.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/services.cfg $(PACKDIR)$(CFGDIR)/services.cfg.$(VERSION)
	$(INSTALL) -m 644 sample-config/template-object/timeperiods.cfg $(PACKDIR)$(CFGDIR)/timeperiods.cfg.$(VERSION)
	cd contrib; $(MAKE) all; $(MAKE) DESTDIR=$(PACKDIR) INIT_OPTS='' INSTALL_OPTS='' COMMAND_OPTS='' nagios_grp='' nagios_usr='' install
	echo i pkginfo> Prototype
	if [ -f checkinstall ] ; then echo i checkinstall>> Prototype; fi
	if [ -f preinstall ] ; then echo i preinstall>> Prototype; fi
	if [ -f postinstall ] ; then echo i postinstall>> Prototype; fi
	pkgproto $(PACKDIR)=/ | sed -e "s|$(LOGNAME) $(GROUP)$$|root root|" | egrep -v "(s|d) none (/|/etc|/var|/usr|/usr/local) " >> Prototype

pkg/nagios/pkgmap: Prototype
	mkdir $(PACKDIR)/nagios
	pkgmk -o -r / -f Prototype -d $(PACKDIR) nagios

nagios.SPARC.pkg.tar.gz: pkg/nagios/pkgmap
	cd $(PACKDIR) && tar -cf - nagios | gzip -9 -c > ../nagios.SPARC.pkg.tar.gz

pkgset: nagios.SPARC.pkg.tar.gz

pkgclean:
	rm -rf pkg Prototype nagios.SPARC.pkg.tar.gz
