# Makefile for Icinga tests using libtap


# Source code directories
SRC_COMMON=../common
SRC_INCLUDE=../include
SRC_XDATA=../xdata
SRC_BASE=../base
SRC_CGI=../cgi

CC=@CC@
CFLAGS=@TESTS_CFLAGS@ @DEFS@ -DNSCORE -I../include -I../tools/libtap
TAPOBJ=../tools/libtap/tap.o

#TESTS = test_logging test_events test_timeperiods test_icinga_config test_xsddefault test_checks test_strtoul test_commands test_downtime
TESTS = test_logging test_events test_timeperiods test_icinga_config test_strtoul test_commands test_downtime

# these objects must be the same as defined in cgi/Makefile.in as CGILIBS!
XSD_OBJS = $(SRC_CGI)/statusdata-cgi.o $(SRC_CGI)/xstatusdata-cgi.o
XSD_OBJS += $(SRC_CGI)/objects-cgi.o $(SRC_CGI)/xobjects-cgi.o
XSD_OBJS += $(SRC_CGI)/comments-cgi.o $(SRC_CGI)/downtime-cgi.o
XSD_OBJS += $(SRC_COMMON)/shared.o $(SRC_CGI)/getcgi.o $(SRC_CGI)/cgiutils.o $(SRC_CGI)/cgiauth.o $(SRC_CGI)/readlogs.o $(SRC_CGI)/macros-cgi.o  $(SRC_CGI)/skiplist.o

TP_OBJS = $(SRC_BASE)/utils.o $(SRC_BASE)/config.o $(SRC_BASE)/macros-base.o
TP_OBJS += $(SRC_BASE)/objects-base.o $(SRC_BASE)/xobjects-base.o
TP_OBJS += $(SRC_BASE)/skiplist.o ../common/shared.o

CFG_OBJS = $(TP_OBJS)
CFG_OBJS += $(SRC_BASE)/comments-base.o $(SRC_BASE)/xcomments-base.o
CFG_OBJS += $(SRC_BASE)/downtime-base.o $(SRC_BASE)/xdowntime-base.o
CFG_OBJS += $(SRC_BASE)/retention-base.o $(SRC_BASE)/xretention-base.o


LDFLAGS=@LDFLAGS@
LIBS=@LIBS@

prefix=@prefix@
exec_prefix=@exec_prefix@
LOGDIR=@LOGDIR@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
LIBDIR=@libdir@
CGIDIR=@sbindir@
HTMLDIR=@datarootdir@
INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@
COMMAND_OPTS=@COMMAND_OPTS@
STRIP=@STRIP@

CGIURL=@cgiurl@
HTMURL=@htmurl@

MATHLIBS=-lm
PERLLIBS=@PERLLIBS@
PERLXSI_O=@PERLXSI_O@
SOCKETLIBS=@SOCKETLIBS@
THREADLIBS=@THREADLIBS@
BROKERLIBS=@BROKERLIBS@

BROKER_LDFLAGS=@BROKER_LDFLAGS@

CP=@CP@

# External data I/O code and headers
XSDC=@XSDC@
XSDH=@XSDH@
XCDC=@XCDC@
XCDH=@XCDH@
XRDC=@XRDC@
XRDH=@XRDH@
XODC=@XODC@
XODH=@XODH@
XPDC=@XPDC@
XPDH=@XPDH@
XDDC=@XDDC@
XDDH=@XDDH@

# Extra base code
BASEEXTRALIBS=@BASEEXTRALIBS@

# Generated automatically from configure script
SNPRINTF_O=@SNPRINTF_O@
BROKER_O=@BROKER_O@
BROKER_H=@BROKER_H@

TAPOBJ += $(SNPRINTF_O)


###############################
# Debug
###############################
ENABLE_DEBUG=@ENABLE_DEBUG@

# Compiler flags for use with Valgrind - set when debug is enabled
ifeq ('$(ENABLE_DEBUG)', 'yes')
        CFLAGS=-O0 -g -Wall -DNSCORE -I../include -I../tools/libtap
endif


all: $(TESTS)

########## TESTS ##########

test_logging: test_logging.o $(SRC_BASE)/logging.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

test_events: test_events.o $(SRC_BASE)/events.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(MATHLIBS) $(LIBS)

test_checks: test_checks.o $(SRC_BASE)/checks.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(MATHLIBS) $(LIBS)

test_commands: test_commands.o $(SRC_COMMON)/shared.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^

test_downtime: test_downtime.o $(SRC_BASE)/downtime-base.o $(SRC_BASE)/xdowntime-base.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^

test_freshness: test_freshness.o $(SRC_BASE)/freshness.o $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^

test_icinga_config: test_icinga_config.o $(CFG_OBJS) $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(BROKER_LDFLAGS) $(LDFLAGS) $(MATHLIBS) $(SOCKETLIBS) $(THREADLIBS) $(BROKERLIBS) $(LIBS)

test_timeperiods: test_timeperiods.o $(TP_OBJS) $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(BROKER_LDFLAGS) $(LDFLAGS) $(MATHLIBS) $(SOCKETLIBS) $(THREADLIBS) $(BROKERLIBS) $(LIBS)

test_xsddefault: test_xsddefault.o $(XSD_OBJS) $(TAPOBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

test_strtoul: test_strtoul.c
	$(CC) $(CFLAGS) -o $@ test_strtoul.c $(TAPOBJ)

test: $(TESTS)
	HARNESS_PERL=./test_each.t perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map { "./$$_" } @ARGV)' $(TESTS)

clean:
	rm -f icinga icingastats core *.o gmon.out
	rm -f $(TESTS)
	rm -f *~ *.*~

distclean: clean
	rm -f perlxsi.c
	rm -f Makefile

devclean: distclean

