#!/usr/bin/make -f
# Makefile to build doxygen API docs for the collective libs11n libs.
#
# Requires the following bins in the $(PATH):
# doxygen, libs11n-config, perl

default: all

# find_bin: helper function to find $(1) in PATH
find_bin = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))


DISTNAME = s11n.net-API-$(shell date +%Y.%m.%d)
DIST_FILES = Doxyfile.at Makefile index.txt


clean:
	-rm -fr Doxyfile *~ html latex

S11N_CONFIG = $(call find_bin,libs11n-config)
ifeq (,$(S11N_CONFIG))
    $(warning warning did not find libs11n-config in PATH: $(PATH). Not doing anything.)
all:
	@echo "Install the s11n-related libs, then run this makefile to generate the doxygen API docs."
else
# ^^^^ we have libs11n-config...

PERL_BIN = $(call find_bin,perl)
ifeq (,$(PERL_BIN))
    $(error Error: perl not found in PATH: $(PATH))
endif

DOXYGEN_BIN = $(call find_bin,doxygen)
ifeq (,$(DOXYGEN_BIN))
    $(error Error: doxygen not found in PATH: $(PATH))
endif



S11N_VERSION = $(shell $(S11N_CONFIG) --version)

prefix = $(shell $(S11N_CONFIG) --prefix)

INCLUDES_HOME = $(prefix)/include/s11n.net
INCLUDE_DIRS = $(prefix)/include/s11n.net
#INCLUDE_DIRS = $(shell ls -d $(INCLUDES_HOME)/*)


Doxyfile: Doxyfile.at index.txt Makefile
	sed -e 's,@PACKAGE_VERSION@,$(S11N_VERSION),;' \
		-e 's,@DOXYGEN_INPUT@,index.txt $(INCLUDE_DIRS),;' \
		-e 's,@PERL@,$(PERL_BIN),;' \
	< $< > $@

html: Doxyfile
	@echo "Building docs from headers under: $(INCLUDES_HOME)"
	$(DOXYGEN_BIN)

doxygen: html

INSTALL_DEST = $(prefix)/share/doc/libs11n/libs11n-$(S11N_VERSION)
INSTALL_SUBDIR = API-doxygen
INSTALL_ABS = $(INSTALL_DEST)/$(INSTALL_SUBDIR)
SYMLINK = libs11n-doxygen
install: html
	test -d $(INSTALL_DEST) || mkdirhier $(INSTALL_DEST); \
	test -d $(INSTALL_DEST) || exit
	test -d $(INSTALL_ABS) && rm -fr $(INSTALL_ABS); \
	cp -r html $(INSTALL_ABS)
	cd $(INSTALL_ABS); cd ../; dname=$$(basename $$PWD); cd ..; \
		test -e $(SYMLINK) && rm $(SYMLINK); \
		ln -fs $$dname/$(INSTALL_SUBDIR) $(SYMLINK)




tar: html
	mv html $(DISTNAME)
	tar czf $(DISTNAME).tar.gz $(DISTNAME)
	tar cjf $(DISTNAME).tar.bz2 $(DISTNAME)
	rm -fr $(DISTNAME)

all: doxygen

endif
# ^^^^ got libs11n-config

dist:
	mkdir $(DISTNAME)
	cp $(DIST_FILES) $(DISTNAME)
	tar czf $(DISTNAME).tar.gz $(DISTNAME)
	tar cjf $(DISTNAME).tar.bz2 $(DISTNAME)
	rm -fr $(DISTNAME)

