###############################
# Makefile for contrib software
#
# Last Modified: 12-25-2021
###############################

CC=@CC@
CFLAGS=-I.. @CFLAGS@ @DEFS@
LDFLAGS=@LDFLAGS@ @LIBS@

# Source code directories
SRC_INCLUDE=../include
SRC_COMMON=../common
SRC_CGI=../cgi
SRC_LIB=../lib

# Generated automatically from configure script
SNPRINTF_O=@SNPRINTF_O@
INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@


prefix=@prefix@
exec_prefix=@exec_prefix@
CGIDIR=@cgibindir@
BINDIR=@bindir@

CGIS=traceroute.cgi daemonchk.cgi
UTILS=convertcfg
ALL=$(CGIS) $(UTILS)


CGI_C=$(SRC_CGI)/getcgi.c
CGI_O=$(SRC_CGI)/getcgi.o $(SNPRINTF_O)
CGI_H=$(SRC_INCLUDE)/getcgi.h
COMMON_H=$(SRC_INCLUDE)/config.h $(SRC_INCLUDE)/common.h $(SRC_INCLUDE)/locations.h

##############################################################################
# standard targets (all, clean, distclean, devclean, install)

all: $(ALL)

clean:
	rm -f convertcfg daemonchk.cgi core *.o
	rm -f */*/*~
	rm -f */*~
	rm -f *~
	rm -rf *.rpm

distclean: clean
	rm -f Makefile

devclean: distclean

install:
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(CGIDIR)
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(BINDIR)
	for f in $(CGIS); do $(INSTALL) -m 775 $(INSTALL_OPTS) $$f $(DESTDIR)$(CGIDIR); done
	for f in $(UTILS); do $(INSTALL) -m 775 $(INSTALL_OPTS) $$f $(DESTDIR)$(BINDIR); done

##############################################################################
# rules and dependencies for actual target programs

$(SRC_LIB)/libnagios.a:
	make -C $(SRC_LIB) libnagios.a

daemonchk.cgi: daemonchk.o $(CGI_O) $(CGI_H) $(COMMON_H) $(SRC_LIB)/libnagios.a
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(CGI_O) $(SRC_LIB)/libnagios.a

daemonchk.o: daemonchk.c
	$(CC) $(CFLAGS) -c -o $@ $< -I$(SRC_INCLUDE)

nagios-worker: nagios-worker.c
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBS) $(SRC_LIB)/libnagios.a

##############################################################################
# dependencies

$(CGI_O): $(CGI_C)
	cd $(SRC_CGI) && make $(CGI_O)

##############################################################################
# implicit rules

%.cgi : %.c
	$(CC) $(CFLAGS) $(LDFLAGS) $< $(CGI_O) -o $@

##############################################################################
# making similar EPEL rpm for Fedora/RHEL/CentOS 

ifneq ("$(wildcard /etc/redhat-release)","")

ARCH ?= $(shell uname -m)
ifeq ($(ARCH),x86_64)
RPM_ARCH := x86_64
else
    ifeq ($(ARCH),i686)
RPM_ARCH := i386
    else
        ifeq ($(ARCH),aarch64)
RPM_ARCH := aarch64
        else
$(error Unknown arch "$(ARCH)".)
        endif
    endif
endif

# Create our own tarball and put ino rpmbuild/SOURCES
localsrc-rpm:
	# create nagios tar ball.
	@(cd ..;rm -f nagios-@VERSION@)
	@(cd ..;ln -s . nagios-@VERSION@)
	@(cd ..;tar zhcf nagios-@VERSION@.tar.gz --exclude nagios-@VERSION@/nagios-@VERSION@.tar.gz --exclude nagios-@VERSION@/nagios-@VERSION@ --exclude RCS --exclude CVS --exclude build-* --exclude *~ --exclude .git* nagios-@VERSION@/)
	@(cd ..;rm -f nagios-@VERSION@)
	# build the rpm using rpmbuild from ./rmbuild as topdir
	@rm -rf rpmbuild && mkdir -p ./rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
	@cp  ../nagios-@VERSION@.tar.gz rpmbuild/SOURCES/nagios-@VERSION@.tar.gz
	@cp epel-patches/* ./rpmbuild/SOURCES
	@rpmbuild -ba --define "_topdir ${PWD}/rpmbuild"   epel-nagios.spec
	@cp  rpmbuild/RPMS/$(RPM_ARCH)/*.rpm .
	@cp rpmbuild/SRPMS/*.src.rpm .
	@ls -l *.rpm

# tarball from https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-%{version}/nagios-%{version}.tar.gz
rpm: init-epelrpm  build-epelrpm
	@mv rpmbuild/RPMS/$(RPM_ARCH)/*.rpm .
	@ls -l *.rpm
init-epelrpm: 
	# build the rpm using rpmbuild from ./rmbuild as topdir
	@rm -rf ./rpmbuild && mkdir -p ./rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
	@spectool -C ./rpmbuild/SOURCES -g epel-nagios.spec
	@cp epel-patches/* ./rpmbuild/SOURCES
build-epelrpm:
	@rpmbuild -ba --define "_topdir ${PWD}/rpmbuild"  epel-nagios.spec
endif
