###############################
# Makefile for Nagios CGIs
#
# Last Modified: 05-30-2006
###############################


# Source code directories
SRC_COMMON=../common
SRC_INCLUDE=../include
SRC_XDATA=../xdata

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@
STRIP=@STRIP@

CGIEXTRAS=@CGIEXTRAS@

CP=@CP@
CC=@CC@
CFLAGS=@CFLAGS@ @DEFS@ -DNSCGI

# 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=avail.cgi cmd.cgi config.cgi extinfo.cgi history.cgi notifications.cgi outages.cgi showlog.cgi status.cgi statuswml.cgi summary.cgi tac.cgi $(CGIEXTRAS)

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

# Generated automatically from configure script
SNPRINTF_O=@SNPRINTF_O@

# Object functions
#ODATALIBS=$(SRC_COMMON)/objects.c $(SRC_XDATA)/$(XODC)
#ODATAHDRS=$(SRC_INCLUDE)/objects.h $(SRC_XDATA)/$(XODH)
ODATALIBS=objects-cgi.o xobjects-cgi.o
ODATAHDRS=
ODATADEPS=$(ODATALIBS)

# Host, service, and program status functions
#SDATALIBS=$(SRC_COMMON)/statusdata.c $(SRC_XDATA)/$(XSDC)
#SDATAHDRS=$(SRC_INCLUDE)/statusdata.h $(SRC_XDATA)/$(XSDH)
SDATALIBS=statusdata-cgi.o xstatusdata-cgi.o
SDATAHDRS=
SDATADEPS=$(SDATALIBS)

# Host and service comment functions
#CDATALIBS=$(SRC_COMMON)/comments.c $(SRC_XDATA)/$(XCDC)
#CDATAHDRS=$(SRC_INCLUDE)/comments.h $(SRC_XDATA)/$(XCDH)
CDATALIBS=comments-cgi.o xcomments-cgi.o
CDATAHDRS=
CDATADEPS=$(CDATALIBS)

# Host and service downtime functions
#DDATALIBS=$(SRC_COMMON)/downtime.c $(SRC_XDATA)/$(XDDC)
#DDATAHDRS=$(SRC_INCLUDE)/downtime.h $(SRC_XDATA)/$(XDDH)
DDATALIBS=downtime-cgi.o xdowntime-cgi.o
DDATAHDRS=
DDATADEPS=$(DDATALIBS)

# Common CGI functions (includes object and status functions)
CGILIBS=getcgi.o cgiutils.o cgiauth.o $(SNPRINTF_O) $(ODATALIBS) $(SDATALIBS)
CGIHDRS=$(SRC_INCLUDE)/config.h $(SRC_INCLUDE)/common.h $(SRC_INCLUDE)/locations.h
CGIDEPS=$(CGILIBS) $(ODATADEPS) $(SDATADEPS)


MATHLIBS=-lm
GDLIBS=@GDLIBS@


all cgis: $(CGIS)

$(CGILIBS): $(CGIHDRS)


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

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

xobjects-cgi.o: $(SRC_XDATA)/$(XODC) $(SRC_XDATA)/$(XODH)
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/$(XODC)

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

xstatusdata-cgi.o: $(SRC_XDATA)/$(XSDC) $(SRC_XDATA)/$(XSDH)
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/$(XSDC)

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

xcomments-cgi.o: $(SRC_XDATA)/$(XCDC) $(SRC_XDATA)/$(XCDH)
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/$(XCDC)

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

xdowntime-cgi.o: $(SRC_XDATA)/$(XDDC) $(SRC_XDATA)/$(XDDH)
	$(CC) $(CFLAGS) -c -o $@ $(SRC_XDATA)/$(XDDC)


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

histogram.cgi: histogram.c $(CGIDEPS)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ histogram.c $(CGILIBS) $(GDLIBS) $(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
	 $(MAKE) strip-post-install

install-unstripped:
	 $(MAKE) install-basic

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

strip-post-install:
	for file in *.cgi; do \
		$(STRIP) $(DESTDIR)$(CGIDIR)/$$file; \
	done


