
.PHONY: default export install install_inc install_target clean oclean distclean targetclean realclean

default:: $(TOPDIR)/lib/$(TARGET)

all:: default

export:: source
	$(INSTALL) $(INSTALLDIROPT) $(TOPDIR)/$(EXPORTDIR)/src
	for i in $(LIBSRCDIR)/*.cc; do \
	 $(INSTALL) $(INSTALLLIBOPT) $${i} $(TOPDIR)/$(EXPORTDIR)/src; \
        done
	$(INSTALL) $(INSTALLDIROPT) $(TOPDIR)/$(EXPORTDIR)/include
	for i in $(LIBSRCDIR)/*.h; do \
	 $(INSTALL) $(INSTALLLIBOPT) $${i} $(TOPDIR)/$(EXPORTDIR)/include; \
        done

$(TOPDIR)/lib/$(TARGET): source
	cd $(LIBSRCLINK); $(MAKE) TO_TOPDIR=$(TO_TOPDIR) TOPDIR=$(TOPOBJDIR) SRCDIR=$(SRCDIR)

install:: install_inc install_target install_pkgconfig install_data

install_inc:: $(TOPDIR)/lib/$(TARGET)
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(includedir)/$(NAME)$(VERSION)
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(includedir)/$(NAME)$(VERSION)/chemistry
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(includedir)/$(NAME)$(VERSION)/util
	-$(INSTALL) $(INSTALLLIBOPT) $(TOPDIR)/include/$(NAME)$(VERSION)/*.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)
	-$(INSTALL) $(INSTALLLIBOPT) $(TOPDIR)/include/$(NAME)$(VERSION)/config.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/include/$(NAME)$(VERSION).h $(DESTDIR)$(includedir)
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/include/$(NAME)$(VERSION).hpp $(DESTDIR)$(includedir)
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/include/$(NAME)$(VERSION)/*.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/include/$(NAME)$(VERSION)/chemistry/*.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)/chemistry
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/include/$(NAME)$(VERSION)/util/*.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)/util
	-$(INSTALL) $(INSTALLLIBOPT) $(SRCDIR)/*.h $(DESTDIR)$(includedir)/$(NAME)$(VERSION)
	gunzip -c $(SRCTOPDIR)/external/boost.tar.gz | tar -xf - -C $(DESTDIR)$(includedir)/$(NAME)$(VERSION)

install_target:: $(TOPDIR)/lib/$(TARGET)
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(libdir)
	$(LTINST) $(INSTALL) $(INSTALLLIBOPT) $< $(DESTDIR)$(libdir)

ifdef pkgconfigdir
install_pkgconfig::
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(pkgconfigdir)
	$(INSTALL) $(INSTALLLIBOPT) $(TOPDIR)/libint2.pc $(DESTDIR)$(pkgconfigdir)
endif

install_data::
	$(INSTALL) $(INSTALLDIROPT) $(DESTDIR)$(datadir)/basis
	$(INSTALL) $(INSTALLLIBOPT) $(SRCTOPDIR)/lib/basis/* $(DESTDIR)$(datadir)/basis

uninstall::
	-rm -f $(DESTDIR)$(libdir)/$(TARGET)
	for inc in $(INC); \
	do \
	  rm -f $(DESTDIR)$(includedir)/$(NAME)/$${inc}; \
	done
	-rm -rf $(DESTDIR)$(includedir)/$(NAME)

clean::
	if test -f source; then \
cd $(LIBSRCLINK); $(MAKE) TO_TOPDIR=$(TO_TOPDIR) TOPDIR=$(TOPOBJDIR) SRCDIR=$(SRCDIR) clean; \
fi

oclean::
	if test -f source; then \
cd $(LIBSRCLINK); $(MAKE) TO_TOPDIR=$(TO_TOPDIR) TOPDIR=$(TOPOBJDIR) SRCDIR=$(SRCDIR) oclean; \
fi

distclean::
	if test -f source; then \
cd $(LIBSRCLINK); $(MAKE) TO_TOPDIR=$(TO_TOPDIR) TOPDIR=$(TOPOBJDIR) SRCDIR=$(SRCDIR) distclean; \
fi
	$(MAKE) realclean

targetclean::
	if test -f source; then \
cd $(LIBSRCLINK); $(MAKE) TO_TOPDIR=$(TO_TOPDIR) TOPDIR=$(TOPOBJDIR) SRCDIR=$(SRCDIR) targetclean; \
fi

realclean:: rm_libsrcdir

rm_libsrcdir:
	-rm -rf $(LIBSRCDIR)
	-rm -f $(LIBSRCLINK)
	-rm -f source
	-rm -f make_libsrcdir

source: make_libsrcdir $(LIBSRCDIR)/Makefile $(LIBSRCDIR)/libint2_params.h
	echo "Last time library source was generated:\c" > $@
	date >> $@

make_libsrcdir:
	@if [ -e $@ ]; then \
$(RM) -rf $(LIBSRCDIR); \
$(RM) -f $(LIBSRCLINK); \
$(RM) -f source; \
$(RM) -f make_libsrcdir; \
fi; \
if ! $(INSTALL) $(INSTALLDIROPT) $(LIBSRCDIR); then \
echo Could not make directory $(LIBSRCDIR); \
exit 1; \
fi; \
if ! $(LN_S) $(LIBSRCDIR) $(LIBSRCLINK); then \
echo Could not $(LN_S) $(LIBSRCDIR) $(LIBSRCLINK); \
exit 1; \
fi;
	@echo "Last time the library source directory was created:\c" > $@
	@date >> $@

$(LIBSRCDIR)/Makefile: make_libsrcdir $(SRCDIR)/Makefile.library
	cp -f $(SRCDIR)/Makefile.library $@

$(LIBSRCDIR)/libint2_params.h: $(TOPOBJDIR)/src/bin/$(NAME)/$(COMPILER) $(LIBSRCDIR)/Makefile
	cd $(LIBSRCLINK); $(TOPOBJDIR)/src/bin/$(NAME)/$(COMPILER)

