###############################
# Makefile for Nagios CGIs
#
###############################


# Source code directories
srcdir=@srcdir@

SRC_COMMON=@top_srcdir@/common
SRC_INCLUDE=@top_srcdir@/include
SRC_XDATA=@top_srcdir@/xdata
SRC_LIB=@top_srcdir@/lib

BLD_COMMON=@top_builddir@/common
BLD_INCLUDE=@top_builddir@/include
BLD_LIB=@top_builddir@/lib


prefix=@prefix@
exec_prefix=@exec_prefix@
LOGDIR=@localstatedir@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
CGIDIR=@cgibindir@
HTMLDIR=@webdir@
INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@
COMMAND_OPTS=@COMMAND_OPTS@
STRIP=@STRIP@

CGIEXTRAS=@CGIEXTRAS@

CP=@CP@
CC=@CC@
CFLAGS=-Wall -I.. -I@builddir@ -I$(SRC_INCLUDE) -I@top_srcdir@ -I$(BLD_INCLUDE) -I$(BLD_LIB)  @CFLAGS@ @DEFS@ -DNSCGI
JSONFLAGS=-DJSON_NAGIOS_4X

# Compiler flags for optimization (overrides default)
#CFLAGS=-O3 -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -DHAVE_CONFIG_H -DNSCGI

# Compiler flags for optimization (complements default)
#CFLAGS_WARN=-Wall -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs
#CFLAGS_DEBUG=-ggdb3 -g3
#CFLAGS+=$(CFLAGS_WARN) $(CFLAGS_DEBUG)

LDFLAGS=@LDFLAGS@
LIBS=@LIBS@

CGIS=archivejson.cgi avail.cgi cmd.cgi config.cgi extinfo.cgi history.cgi notifications.cgi objectjson.cgi outages.cgi showlog.cgi status.cgi statusjson.cgi statuswml.cgi summary.cgi tac.cgi $(CGIEXTRAS)

# Object functions
ODATALIBS=objects-cgi.o xobjects-cgi.o
ODATAHDRS=
ODATADEPS=$(ODATALIBS)

# Host, service, and program status functions
SDATALIBS=statusdata-cgi.o xstatusdata-cgi.o comments-cgi.o downtime-cgi.o
SDATAHDRS=
SDATADEPS=$(SDATALIBS)

# Host and service comment functions
CDATALIBS=
CDATAHDRS=
CDATADEPS=$(CDATALIBS)

# Host and service downtime functions
DDATALIBS=
DDATAHDRS=
DDATADEPS=$(DDATALIBS)

# Common CGI functions (includes object and status functions)
CGILIBS=$(BLD_COMMON)/shared.o getcgi.o cgiutils.o cgiauth.o macros-cgi.o $(SNPRINTF_O) $(ODATALIBS) $(SDATALIBS) $(BLD_LIB)/libnagios.a
CGIHDRS=$(BLD_INCLUDE)/config.h $(SRC_INCLUDE)/common.h $(BLD_INCLUDE)/locations.h
CGIDEPS=$(CGILIBS) $(ODATADEPS) $(SDATADEPS) $(BLD_LIB)/libnagios.a


MATHLIBS=-lm
GDLIBS=@GDLIBS@


all cgis: $(CGIS)

$(CGILIBS): $(CGIHDRS)

%.o: $(srcdir)/%.c
	$(CC) $(CFLAGS) -c -o $@ $<

######## REQUIRED LIBRARIES ##########

$(BLD_LIB)/libnagios.a:
	$(MAKE) -C $(BLD_LIB)

macros-cgi.o: $(SRC_COMMON)/macros.c $(SRC_INCLUDE)/macros.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_COMMON)/macros.c

objects-cgi.o: $(SRC_COMMON)/objects.c $(SRC_INCLUDE)/objects.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_COMMON)/objects.c

xobjects-cgi.o: $(SRC_XDATA)/xodtemplate.c $(SRC_XDATA)/xodtemplate.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/xodtemplate.c

statusdata-cgi.o: $(SRC_COMMON)/statusdata.c $(SRC_INCLUDE)/statusdata.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_COMMON)/statusdata.c

xstatusdata-cgi.o: $(SRC_XDATA)/xsddefault.c $(SRC_XDATA)/xsddefault.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/xsddefault.c

comments-cgi.o: $(SRC_COMMON)/comments.c $(SRC_INCLUDE)/comments.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_COMMON)/comments.c

downtime-cgi.o: $(SRC_COMMON)/downtime.c $(SRC_INCLUDE)/downtime.h
	$(CC) $(CFLAGS) -c -o $@ $(SRC_COMMON)/downtime.c

$(BLD_COMMON)/shared.o: $(SRC_COMMON)/shared.c
	$(CC) $(CFLAGS) -c -o $@ $<

########## CGIS ##########

avail.cgi: $(srcdir)/avail.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/avail.c $(CGILIBS) $(LIBS)

checksanity.cgi: $(srcdir)/checksanity.c $(CGIDEPS) $(CDATADEPS) $(DDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/checksanity.c $(CGILIBS) $(CDATALIBS) $(DDATALIBS) $(LIBS)

cmd.cgi: $(srcdir)/cmd.c $(CGIDEPS) $(CDATADEPS) $(DDATADEPS) extcmd_list.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/cmd.c extcmd_list.o $(CGILIBS) $(CDATALIBS) $(DDATALIBS) $(LIBS)

config.cgi: $(srcdir)/config.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/config.c  $(CGILIBS) $(LIBS)

extinfo.cgi: $(srcdir)/extinfo.c $(CGIDEPS) $(CDATADEPS) $(DDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/extinfo.c $(CGILIBS) $(CDATALIBS) $(DDATALIBS) $(LIBS)

history.cgi: $(srcdir)/history.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/history.c $(CGILIBS) $(LIBS)

ministatus.cgi: $(srcdir)/ministatus.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/ministatus.c $(CGILIBS) $(LIBS)

notifications.cgi: $(srcdir)/notifications.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/notifications.c $(CGILIBS) $(LIBS)

outages.cgi: $(srcdir)/outages.c $(CGIDEPS) $(CDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/outages.c $(CGILIBS) $(CDATALIBS) $(LIBS)

showlog.cgi: $(srcdir)/showlog.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/showlog.c $(CGILIBS) $(LIBS)

status.cgi: $(srcdir)/status.c $(CGIDEPS) $(CDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/status.c $(CGILIBS) $(CDATALIBS) $(LIBS)

statuswml.cgi: $(srcdir)/statuswml.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/statuswml.c $(CGILIBS) $(LIBS)

statusmap.cgi: $(srcdir)/statusmap.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/statusmap.c $(CGILIBS) $(GDLIBS) $(LIBS)

statuswrl.cgi: $(srcdir)/statuswrl.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/statuswrl.c $(CGILIBS) $(MATHLIBS) $(LIBS)

summary.cgi: $(srcdir)/summary.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/summary.c $(CGILIBS) $(LIBS)

tac.cgi: $(srcdir)/tac.c $(CGIDEPS) $(CDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/tac.c $(CGILIBS) $(CDATALIBS) $(LIBS)

tac-xml.cgi: $(srcdir)/tac-xml.c $(CGIDEPS) $(CDATADEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/tac-xml.c $(CGILIBS) $(CDATALIBS) $(LIBS)

trends.cgi: $(srcdir)/trends.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/trends.c $(CGILIBS) $(GDLIBS) $(LIBS)

histogram.cgi: $(srcdir)/histogram.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(srcdir)/histogram.c $(CGILIBS) $(GDLIBS) $(LIBS)

########## JSON CGIS ##########

jsonutils.o: $(srcdir)/jsonutils.c $(SRC_INCLUDE)/jsonutils.h
	$(CC) $(CFLAGS) $(JSONFLAGS) -c -o $@ $(srcdir)/jsonutils.c

archiveutils.o: $(srcdir)/archiveutils.c $(SRC_INCLUDE)/archiveutils.h
	$(CC) $(CFLAGS) $(JSONFLAGS) -c -o $@ $(srcdir)/archiveutils.c

archivejson.cgi: $(srcdir)/archivejson.c $(CGIDEPS) archiveutils.o jsonutils.o $(SRC_INCLUDE)/archivejson.h
	$(CC) $(CFLAGS) $(JSONFLAGS) $(LDFLAGS) -o $@ $(srcdir)/archivejson.c $(CGILIBS) archiveutils.o jsonutils.o $(LIBS)

objectjson.cgi: $(srcdir)/objectjson.c $(CGIDEPS) jsonutils.o $(SRC_INCLUDE)/objectjson.h
	$(CC) $(CFLAGS) $(JSONFLAGS) $(LDFLAGS) -o $@ $(srcdir)/objectjson.c $(CGILIBS) jsonutils.o $(LIBS)

statusjson.cgi: $(srcdir)/statusjson.c $(CGIDEPS) jsonutils.o $(SRC_INCLUDE)/statusjson.h
	$(CC) $(CFLAGS) $(JSONFLAGS) $(LDFLAGS) -o $@ $(srcdir)/statusjson.c $(CGILIBS) jsonutils.o $(LIBS)


clean:
	rm -f $(CGIS)
	rm -f *.o core gmon.out
	rm -f *~ *.*~

distclean: clean
	rm -f Makefile cgiutils.h

devclean: distclean

install:
	$(MAKE) install-basic

install-unstripped:
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(CGIDIR)
	for file in *.cgi; do \
		$(INSTALL) -m 775 $(INSTALL_OPTS) $$file $(DESTDIR)$(CGIDIR); \
	done

install-basic:
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(CGIDIR)
	for file in *.cgi; do \
		$(INSTALL) -s -m 775 $(INSTALL_OPTS) $$file $(DESTDIR)$(CGIDIR); \
	done

.PHONY: libnagios
