## Process this file with automake to produce Makefile.in

ACLOCAL_AMFLAGS = -I m4
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc --enable-introspection --enable-hkl3d

SUBDIRS = hkl binoculars
if HKL3D
SUBDIRS += hkl3d data
endif
SUBDIRS += tests
if GUI
SUBDIRS += gui
endif
if HKL_DOC
SUBDIRS += Documentation
endif
if CONTRIB
SUBDIRS += contrib
endif

EXTRA_DIST = hkl.pc.in hkl-binoculars.pc.in hkl3d.pc.in

installed_mainheaderdir = $(includedir)/hkl-@VMAJ@
dist_installed_mainheader_DATA = hkl.h

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = hkl.pc hkl-binoculars.pc

if HKL3D
#for not do not install
noinst_DATA = hkl3d.pc
endif

release:
	git clean -xdf
	git fetch
	git checkout next
	cd scripts && ./clean-all
	-git commit -a -m "pre-release"
	git checkout maint
	git merge -m "release" next
	./autogen.sh
	./configure $(AM_DISTCHECK_CONFIGURE_FLAGS)
	$(MAKE)
	$(MAKE) distcheck
	git tag v`./configure -V | head -n 1 | awk '{print $$3}'`
	git checkout next


## lcov part
.PHONY: lcov genlcov lcov-clean release
# use recursive makes in order to ignore errors during check
lcov:
	-$(MAKE) $(AM_MAKEFLAGS) -k check
	$(MAKE) $(AM_MAKEFLAGS) genlcov

# we have to massage the lcov.info file slightly to hide the effect of libtool
# placing the objects files in the .libs/ directory separate from the *.c
# we also have to delete tests/.libs/*.gcda
genlcov:
	rm -f $(top_builddir)/test/hkl/.libs/*.gcda
	$(LTP) --directory $(top_builddir) --capture --output-file hkl-lcov.info --test-name hkl-lcov --no-checksum --compat-libtool
	LANG=C $(LTP_GENHTML) --prefix $(top_builddir) --output-directory hkl-lcov --title "hkl Code Coverage" --legend --show-details hkl-lcov.info

lcov-clean:
	-$(LTP) --directory $(top_builddir) -z
	-rm -rf hkl-lcov.info hkl-lcov
	-find -name '*.gcda' -print | xargs rm
	-find -name '*.gcno' -print | xargs rm

distclean-local: lcov-clean

## Generate the Changelog file for the distribution.
dist-hook:
	@if test -d "$(srcdir)/.git"; \
	then \
		echo Creating ChangeLog && \
                ( cd "$(top_srcdir)" && \
                  echo '# Generated by Makefile. Do not edit.'; echo; \
                  $(top_srcdir)/config/missing --run git log --stat ) > ChangeLog.tmp \
                && mv -f ChangeLog.tmp $(top_distdir)/ChangeLog \
                || ( rm -f ChangeLog.tmp ; \
                     echo Failed to generate ChangeLog >&2 ); \
        else \
                echo A git clone is required to generate a ChangeLog >&2; \
        fi

## Update the ccan library

CCAN_PUBLIC_MODULES=darray
CCAN_PRIVATE_MODULES=array_size container_of autodata list generator
CCAN_LOCAL_REPO=/tmp/ccan
CCAN_LOCAL_EXTRACT=/tmp/ccan.tmp
CCAN_URL=https://github.com/rustyrussell/ccan.git
CCAN_CREATE_TREE=rm -rf $(CCAN_LOCAL_EXTRACT) && cd $(CCAN_LOCAL_REPO) && tools/create-ccan-tree -b automake $(CCAN_LOCAL_EXTRACT)
CCAN_DEST=$(top_srcdir)/hkl/
CCAN_TMP_DEST=/tmp/ccan.next
ccan-update:
	@if test -d $(CCAN_LOCAL_REPO); \
	then \
		cd $(CCAN_LOCAL_REPO) && git pull; \
	else \
		git clone $(CCAN_URL) $(CCAN_LOCAL_REPO); \
	fi

	@echo Extracting all the modules
	@$(CCAN_CREATE_TREE) $(CCAN_PUBLIC_MODULES) $(CCAN_PRIVATE_MODULES)

	@rm -rf $(CCAN_TMP_DEST) && mkdir -p $(CCAN_TMP_DEST)
	@rsync -av --exclude=Makefile.am $(CCAN_LOCAL_EXTRACT)/* $(CCAN_TMP_DEST)
	@find $(CCAN_TMP_DEST)/ccan -maxdepth 2 -name '*.[ch]' | xargs sed -i -e 's,ccan/,hkl/ccan/,'
	@find $(CCAN_TMP_DEST)/ccan -maxdepth 2 -name '*.[ch]' | xargs sed -i -e 's,"config.h",<hkl/ccan/ccan_config.h>,'
	@find $(CCAN_TMP_DEST)/ccan -maxdepth 2 -name '_info' | xargs rm -f
	@find $(CCAN_TMP_DEST)/ccan -maxdepth 2 -name 'test' | xargs rm -rf

	@$(CCAN_CREATE_TREE) $(CCAN_PUBLIC_MODULES)

	@echo Generating the Makefile.am
	@(\
		echo "AM_CFLAGS=-I\$$(top_srcdir)"; echo;\
		echo "noinst_PROGRAMS=configurator";\
		echo "BUILT_SOURCES=ccan_config.h";\
		echo "ccan_config.h: configurator";\
		(\
			echo -e "\t\$$(builddir)/configurator \$$(CC) \$$(CCAN_CFLAGS) > \$$@.tmp && mv \$$@.tmp \$$@";\
		);\
		echo "noinst_LTLIBRARIES=libccan.la";\
		echo "libccan_la_SOURCES = \\";\
		(\
			echo -e "\tccan_config.h \\";\
			cd $(CCAN_TMP_DEST)/ccan && find . -maxdepth 2 -name '*.[ch]'\
				| sed -e 's,^./,\t,;$$!s,$$, \\,'; echo;\
		);\
		echo "ccanincludedir=\$$(includedir)/hkl-@""VMAJ@/hkl/ccan";\
		echo "nobase_ccaninclude_HEADERS= \\";\
		(\
			cd $(CCAN_LOCAL_EXTRACT)/ccan && find . -maxdepth 2 -name '*.[h]'\
				| sed -e 's,^./,\t,;$$!s,$$, \\,';\
		);\
		echo "nodist_ccaninclude_HEADERS=ccan_config.h";\
		echo "EXTRA_DIST=configurator.c";\
		echo "CLEANFILES=configurator ccan_config.h";\
	) > $(CCAN_TMP_DEST)/ccan/Makefile.am

	@echo Extracting the configurator
	@rsync -av $(CCAN_LOCAL_REPO)/tools/configurator/configurator.c $(CCAN_TMP_DEST)/ccan

	@echo purge the previous ccan directory
	cd $(CCAN_DEST) && rm -rf licenses && find ccan -mindepth 1 -type d \! -name ".*" | xargs rm -rf
	rsync -av $(CCAN_TMP_DEST)/* $(top_srcdir)/hkl
