include ../../Makefile.vars

ALLINC = -I../../include ${CFG2JSONINC} ${DWMINC}
ALLLIB = ${OSLIBS}
OBJFILES = dwmrdapd.o \
	   DwmRDAPResponder.o \
	   DwmRDAPServer.o \
	   DwmRDAPServerConfig.o
OBJDEPS  = $(OBJFILES:%.o=deps/%_deps)
TARTARGETS = ${TARDIR}/sbin/dwmrdapd \
	     ${TARDIR}/man/man8/dwmrdapd.8 \
	     ${TARDIR}/man/man8/dwmrdapd.conf.5
ifeq ("${MANDOC}", "mandoc")
MANHTML = ${MANDOC} -Thtml -Ostyle=../mcplexman.css -Oman=../html%S/%N.%S.html
TARTARGETS += ${TARDIR}/${HTMLMAN}/html8/dwmrdapd.8.html
TARTARGETS += ${TARDIR}/${HTMLMAN}/html5/dwmrdapd.conf.5.html
endif

dwmrdapd: ${OBJFILES} ../../lib/libDwmRDAP.la
	${LIBTOOL} --mode=link --tag=CXX ${CXX} ${CXXFLAGS} -o $@ $^ -static ${CFG2JSONLIB} ${DWMLIB} ${ALLLIB}

../../lib/libDwmRDAP.la::
	${MAKE} -C ../../src

tarprep: ${TARTARGETS}

${TARDIR}/sbin/dwmrdapd: dwmrdapd
	../../install-sh -s -c -m 755 $< $@

${TARDIR}/man/man8/dwmrdapd.8: dwmrdapd.8
	../../install-sh -c -m 644 $< $@

${TARDIR}/man/man8/dwmrdapd.conf.5: dwmrdapd.conf.5
	../../install-sh -c -m 644 $< $@

${TARDIR}/${HTMLMAN}/html8/dwmrdapd.8.html: dwmrdapd.8
	${MANHTML} $< > $<.html
	../../install-sh -c -m 644 $<.html $@
	rm $<.html

${TARDIR}/${HTMLMAN}/html5/dwmrdapd.conf.5.html: dwmrdapd.conf.5
	${MANHTML} $< > $<.html
	../../install-sh -c -m 644 $<.html $@
	rm $<.html

#  dependency rule
deps/%_deps: %.cc
	@echo "making dependencies for $<"
	@set -e; \
	${CXX} -MM ${CXXFLAGS} ${PTHREADCXXFLAGS} ${ALLINC} -c $< | \
	 sed 's/\($*\)\.o[ :]*/\1.o $(@D)\/$(@F) : /g' > $@ ; [ -s $@ ] || \
	 rm -f $@

#  only include dependency makefiles if target is not 'clean' or 'distclean'
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),distclean)
-include ${OBJDEPS}
endif
endif

%.o: %.cc
	${CXX} ${CXXFLAGS} ${PTHREADCXXFLAGS} ${ALLINC} -c $<

clean: clean-tarprep
	rm -f ${OBJFILES} dwmrdapd

clean-tarprep::
	rm -f ${TARTARGETS}

