#
# CINTS Makefile
#
# Edward Valeev, Eve-of-the-Fake-New-Millenium
#
# Edit this list of subdirectories whose content
# to be included in CINTS to suit your needs. Valid subdirs are:
# 1. Default_Ints
# 2. OEProp_Ints
# 3. Fock
# 4. MP2
# 5. Default_Deriv1
# 6. Default_Deriv2
# 7. MP2R12
# 8. R12_Ints (8/30/03 EFV - doesn't work anymore, transqt needs to be fixed)
# 9. CC
#
SUBDIRS = Default_Ints OEProp_Ints Fock MP2 Default_Deriv1 Default_Deriv2 CC \
MP2R12

#
# Some subdirectories are not completely independent - need to perform
# additional "dependency" checks:
# 1. if R12_Ints included - make sure MP2R12 is included also.
# 2. if Fock (DFT) is included - include DFT (Fock) as well.
#
ifeq ($(findstring R12_Ints,$(SUBDIRS)),R12_Ints)
  ifeq ($(findstring MP2R12,$(SUBDIRS)),)
    SUBDIRS += MP2R12
  endif
endif
ifeq ($(findstring Fock,$(SUBDIRS)),Fock)
  ifeq ($(findstring DFT,$(SUBDIRS)),)
     SUBDIRS += DFT
  endif
else
  ifeq ($(findstring DFT,$(SUBDIRS)),DFT)
     SUBDIRS += Fock
  endif
endif
ifeq ($(findstring Default_Deriv2,$(SUBDIRS)),Default_Deriv2)
  ifeq ($(findstring Default_Deriv1,$(SUBDIRS)),)
    SUBDIRS += Default_Deriv1
  endif
endif

#
# Most subdirectories include code that requires some "integrals"
# library, like the notorious libint, which is always included.
# Here we test SUBDIRS for presence of such
# and form the list of the libraries. Remember that libderiv and libr12
# depend on libint, and hence need to come first for GNU ld to work.
#
INTLIBS := -lPSI_int
ifeq ($(findstring MP2R12,$(SUBDIRS)),MP2R12)
  ifneq ($(findstring -lPSI_r12,$(INTLIBS)),-lPSI_r12)
    INTLIBS := -lPSI_r12 $(INTLIBS)
  endif
endif
ifeq ($(findstring R12_Ints,$(SUBDIRS)),R12_Ints)
  ifneq ($(findstring -lPSI_r12,$(INTLIBS)),-lPSI_r12)
    INTLIBS := -lPSI_r12 $(INTLIBS)
  endif
endif
ifeq ($(findstring Default_Deriv1,$(SUBDIRS)),Default_Deriv1)
  ifneq ($(findstring -lPSI_deriv,$(INTLIBS)),-lPSI_deriv)
    INTLIBS := -lPSI_deriv $(INTLIBS)
  endif
endif
ifeq ($(findstring Default_Deriv2,$(SUBDIRS)),Default_Deriv2)
  ifneq ($(findstring -lderiv,$(INTLIBS)),-lPSI_deriv)
    INTLIBS := -lPSI_deriv $(INTLIBS)
  endif
endif

#
# These preprocessor defines will tell main.c and parsing.c what 
#  sections of code to include
#
OPTIONS = $(SUBDIRS:%=-DINCLUDE_%)

#
# Tools will always be included
#
DIRS = Tools $(SUBDIRS)

srcdir = @srcdir@
VPATH = @srcdir@
VPATH += $(DIRS:%=$(srcdir)/%)

include ../MakeVars

#Need do remove -DAIX because I have a variable named AIX
CDEF_NOAIX = $(CDEF:-DAIX=)
CFLAGS = $(CDBG) $(COPT) $(CDEF_NOAIX) $(COTH) $(CINC)
CINC += -I$(srcdir) $(DIRS:%=-I$(srcdir)/%)
CDEF += $(OPTIONS)

CXXDEF_NOAIX = $(CXXDEF:-DAIX=)
CXXFLAGS = $(CXXDBG) $(CXXOPT) $(CXXDEF_NOAIX) $(CXXOTH) $(CXXINC)
CXXINC += -I$(srcdir) $(DIRS:%=-I$(srcdir)/%)
CXXDEF += $(OPTIONS)

LD = $(CXX)
LDLIBS += $(BLAS) -lpthread
PSILIBS = -lPSI_qt -lPSI_chkpt $(INTLIBS) -lPSI_iwl \
          -lPSI_psio -lPSI_ciomr -lPSI_ipv1
ifeq ($(findstring CC,$(SUBDIRS)),CC)
  ifneq ($(findstring -lPSI_dpd,$(PSILIBS)),-lPSI_dpd)
    PSILIBS := -lPSI_dpd $(PSILIBS)
  endif
endif

CSRC = main.c parsing.c gprgid.c init_globals.c check_max_am.c $(DIRS:%=%/*.c)
TRUECSRC = $(notdir $(shell echo `ls $(CSRC:%=$(srcdir)/%)`))
CXXSRC = $(DIRS:%=%/*.cc)
TRUECXXSRC = $(notdir $(shell echo `ls $(CXXSRC:%=$(srcdir)/%)`))

BINOBJ = $(TRUECSRC:%.c=%.o) $(TRUECXXSRC:%.cc=%.o)

include ../MakeRules

# DEC/Compaq Alpha systems apparently require this for threaded code
ifneq (,$(findstring osf, $(target)))
  COPT += -pthread
  CXXOPT += -pthread
  LDFLAGS += -pthread
endif

install:: install_html

install_man:: cints.1
	$(MKDIRS) $(mandir)/man1
	$(INSTALL_INCLUDE) $^ $(mandir)/man1

install_html:: install_index install_htmlsrc

install_index:: cints.html
	$(MKDIRS) $(htmldir)
	$(INSTALL_INCLUDE) $^ $(htmldir)

HTMLSRC = $(shell echo `ls $(srcdir)/Documentation/*.html`)
install_htmlsrc: $(HTMLSRC)
	$(MKDIRS) $(htmldir)/CINTS
	$(INSTALL_INCLUDE) $(HTMLSRC) $(htmldir)/CINTS


