# Makefile for NDOUtils source
#
# 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_INCLUDE=../include

prefix=@prefix@
exec_prefix=@exec_prefix@
PIPEDIR=@localstatedir@
BINDIR=@bindir@
INSTALL=@INSTALL@

CC=@CC@

CFLAGS=-fPIC @CFLAGS@ @DEFS@ 
CFLAGS_4X=-I $(SRC_INCLUDE)/nagios-4x 

# We don't like ANSI because ANSI doesn't like us! phhht!
#CFLAGS=-fPIC -g -Wall -ansi -pedantic -DHAVE_CONFIG_H

# Compiler flags for use with Valgrind
#CFLAGS=-fPIC -O0 -g -DHAVE_CONFIG_H

MOD_CFLAGS=@MOD_CFLAGS@
LDFLAGS=@LDFLAGS@
MOD_LDFLAGS=@MOD_LDFLAGS@
LIBS=@LIBS@
SOCKETLIBS=@SOCKETLIBS@
DBCFLAGS=@DBCFLAGS@
DBLDFLAGS=@DBLDFLAGS@
DBLIBS=@DBLIBS@
MATHLIBS=-lm
SNPRINTF_O=@SNPRINTF_O@

COMMON_INC=$(SRC_INCLUDE)/config.h $(SRC_INCLUDE)/common.h $(SRC_INCLUDE)/io.h $(SRC_INCLUDE)/protoapi.h $(SRC_INCLUDE)/utils.h
COMMON_SRC=io.c utils.c
COMMON_OBJS=io.o utils.o

NDO_INC=$(SRC_INCLUDE)/ndo2db.h $(SRC_INCLUDE)/db.h $(SRC_INCLUDE)/queue.h
NDO_SRC=db.c
NDO_OBJS=db.o


all: file2sock log2ndo ndo2db ndomod sockdebug

file2sock: file2sock.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ file2sock.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

log2ndo: log2ndo.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ log2ndo.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

ndo2db:
	$(MAKE) ndo2db-2x
	$(MAKE) ndo2db-3x
	$(MAKE) ndo2db-4x

ndo2db-2x: queue.c ndo2db.c $(NDO_INC) $(NDO_OBJS) $(COMMON_INC) $(COMMON_OBJS) dbhandlers-2x.o $(SNPRINTF_O)
	$(CC) $(CFLAGS) $(DBCFLAGS) -D BUILD_NAGIOS_2X -o ndo2db-2x queue.c ndo2db.c dbhandlers-2x.o $(SNPRINTF_O) $(COMMON_OBJS) $(NDO_OBJS) $(LDFLAGS) $(DBLDFLAGS) $(LIBS) $(SOCKETLIBS) $(DBLIBS) $(MATHLIBS) $(OTHERLIBS)

ndo2db-3x: queue.c ndo2db.c $(NDO_INC) $(NDO_OBJS) $(COMMON_INC) $(COMMON_OBJS) dbhandlers-3x.o $(SNPRINTF_O)
	$(CC) $(CFLAGS) $(DBCFLAGS) -D BUILD_NAGIOS_3X -o ndo2db-3x queue.c ndo2db.c dbhandlers-3x.o $(SNPRINTF_O) $(COMMON_OBJS) $(NDO_OBJS) $(LDFLAGS) $(DBLDFLAGS) $(LIBS) $(SOCKETLIBS) $(DBLIBS) $(MATHLIBS) $(OTHERLIBS)

ndo2db-4x: queue.c ndo2db.c $(NDO_INC) $(NDO_OBJS) $(COMMON_INC) $(COMMON_OBJS) dbhandlers-4x.o $(SNPRINTF_O)
	$(CC) $(CFLAGS) $(DBCFLAGS) -D BUILD_NAGIOS_4X -o ndo2db-4x queue.c ndo2db.c dbhandlers-4x.o $(SNPRINTF_O) $(COMMON_OBJS) $(NDO_OBJS) $(LDFLAGS) $(DBLDFLAGS) $(LIBS) $(SOCKETLIBS) $(DBLIBS) $(MATHLIBS) $(OTHERLIBS)

ndomod: 
	$(MAKE) ndomod-2x.o
	$(MAKE) ndomod-3x.o
	$(MAKE) ndomod-4x.o

ndomod-2x.o: ndomod.c $(COMMON_INC) $(COMMON_OBJS) $(SNPRINTF_O)
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -D BUILD_NAGIOS_2X -o ndomod-2x.o ndomod.c $(SNPRINTF_O) $(COMMON_OBJS) $(MOD_LDFLAGS) $(LDFLAGS) $(LIBS) $(SOCKETLIBS) $(OTHERLIBS)

ndomod-3x.o: ndomod.c $(COMMON_INC) $(COMMON_OBJS) $(SNPRINTF_O)
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -D BUILD_NAGIOS_3X -o ndomod-3x.o ndomod.c $(SNPRINTF_O) $(COMMON_OBJS) $(MOD_LDFLAGS) $(LDFLAGS) $(LIBS) $(SOCKETLIBS) $(OTHERLIBS)

ndomod-4x.o: ndomod.c $(COMMON_INC) $(COMMON_OBJS) $(SNPRINTF_O)
	$(CC) $(MOD_CFLAGS) $(CFLAGS) $(CFLAGS_4X) -D BUILD_NAGIOS_4X -o ndomod-4x.o ndomod.c $(SNPRINTF_O) $(COMMON_OBJS) $(MOD_LDFLAGS) $(LDFLAGS) $(LIBS) $(SOCKETLIBS) $(OTHERLIBS)

sockdebug: sockdebug.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ sockdebug.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

io.o: io.c $(SRC_INCLUDE)/io.h
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -c -o $@ io.c

utils.o: utils.c $(SRC_INCLUDE)/utils.h
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -c -o $@ utils.c

db.o: db.c $(SRC_INCLUDE)/db.h
	$(CC) $(CFLAGS) -c -o $@ db.c

dbhandlers-2x.o: dbhandlers.c $(SRC_INCLUDE)/dbhandlers.h
	$(CC) $(CFLAGS) -D BUILD_NAGIOS_2X -c -o $@ dbhandlers.c

dbhandlers-3x.o: dbhandlers.c $(SRC_INCLUDE)/dbhandlers.h
	$(CC) $(CFLAGS) -D BUILD_NAGIOS_3X -c -o $@ dbhandlers.c

dbhandlers-4x.o: dbhandlers.c $(SRC_INCLUDE)/dbhandlers.h
	$(CC) $(CFLAGS) $(CFLAGS_4X) -D BUILD_NAGIOS_4X -c -o $@ dbhandlers.c

clean:
	rm -f core file2sock log2ndo ndo2db-2x ndo2db-3x ndo2db-4x sockdebug *.o
	rm -f *~ */*~

distclean: clean
	rm -f Makefile

devclean: distclean

install: install-4x
	$(INSTALL) -m 755 file2sock $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 log2ndo $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 sockdebug $(DESTDIR)$(BINDIR)
	@echo ""
	@echo "  Hint: NDOUtils Installation against Nagios v4.x"
	@echo "  completed."
	@echo ""
	@echo "  If you want to install NDOUtils for Nagios v3.x"
	@echo "  please type  'make install-3x"
	@echo ""
	@echo "  If you want to install NDOUtils for Nagios v2.x"
	@echo "  please type  'make install-2x"
	@echo ""
	@echo ""
	@echo "  Next step should be the database initialization/upgrade"
	@echo "  cd into the db/ directory and either:"
	@echo "     ./installdb  (for a new installation) or:"
	@echo "     ./upgradedb  (for an existing one)"
	@echo ""

install-2x:
	$(INSTALL) -m 775 -d $(DESTDIR)$(PIPEDIR)
	$(INSTALL) -m 775 -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 ndo2db-2x $(DESTDIR)$(BINDIR)/ndo2db
	$(INSTALL) -m 755 ndomod-2x.o $(DESTDIR)$(BINDIR)/ndomod.o

install-3x:
	$(INSTALL) -m 775 -d $(DESTDIR)$(PIPEDIR)
	$(INSTALL) -m 775 -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 ndo2db-3x $(DESTDIR)$(BINDIR)/ndo2db
	$(INSTALL) -m 755 ndomod-3x.o $(DESTDIR)$(BINDIR)/ndomod.o

install-4x:
	$(INSTALL) -m 775 -d $(DESTDIR)$(PIPEDIR)
	$(INSTALL) -m 775 -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 ndo2db-4x $(DESTDIR)$(BINDIR)/ndo2db
	$(INSTALL) -m 755 ndomod-4x.o $(DESTDIR)$(BINDIR)/ndomod.o

