#+
#  Minimal Makefile for demos of MCS usage from Fortran.
#
#  Use the simplified user library "mcs_usrlib.f" to access the native MCS
#  functions and subroutines. Note the required include file "mcs_usrlib.inc".
#  Here the library is all in one single file. It will not be required in
#  the future being included in the MCS distribution as a standard
#  library: "mcs_f_usrlib.a".
#
#  24/06/2010: v. 0.1c
#  14/12/2014: v. 0.1d - use as default MySQL account info those of the
#                        MPE-IMPRS course. Change it as needed.
#
# LN @ IASF-INAF Feb. 2007                      Last change: 14/12/2014
#-

MAKE = make
FC = gfortran
CCPP = g++
FFLAGS = -cpp `mcs-config --cflags` -O2 -fPIC -Wno-unused-variable -DLINKAGE=
CPPFLAGS = `mcs-config --cflags` -O2 $(INC_HTM)
LIBN = mcs_usrlib
LIBNIN = mcs_usrlib_in
LIB = $(LIBN).o

HTMDIR = ./htmIndex
HTMLIB = SpatialIndex
HTMLIB_NAME = lib$(HTMLIB).a
INC_HTM = -I$(HTMDIR)/include
LIBS_HTM = -L$(HTMDIR)/lib -l$(HTMLIB)
LDFLAGS = `mcs-config --libs`

COMP.cpp = $(CCPP) -c $(CPPFLAGS)
COMP.fc = $(FC) -c $(FFLAGS)
LINK.fc = $(FC) $(LDFLAGS)

VER = v01d_`/bin/date +"%Y%m%d"`
distdir = mcs_fdemos_$(VER)

%.cpp.o:
	$(COMP.cpp) $<

EXE = mcsdemo1 mcsdemo2 mcsdemo3 mcsdemo4 mcsdemo_myCat_qry demo_htmID

all: lib htmlib $(EXE)
libs: lib htmlib

mcsdemo1: mcsdemo1.o
	$(LINK.fc) -o $@ $(LIB) $@.o

mcsdemo2: mcsdemo2.o
	$(LINK.fc) -o $@ $(LIB) $@.o

mcsdemo3: mcsdemo3.o
	$(LINK.fc) -o $@ $(LIB) $@.o

mcsdemo4: myCat_qry.o mcsdemo4.o
	$(LINK.fc) $(LIBS_HTM) -o $@ $(LIB) $@.o

mcsdemo_myCat_qry: htmqry6.o myCat_qry.o mcsdemo_myCat_qry.o
	$(LINK.fc) htmqry6.o myCat_qry.o $(LIBS_HTM) -lstdc++ -o $@ $(LIB) $@.o

demo_htmID: htmid_pp.o gethtmid.o demo_htmID.o
	$(LINK.fc) htmid_pp.o gethtmid.o $(LIBS_HTM) -lstdc++ -o $@ $(LIB) $@.o


# Library .f and .inc files
lib:
	@if test "`uname -m`" == "x86_64" -o "`uname -s`" == "Darwin"; then \
	echo This is a 64-bit machine && $(MAKE) 64bit; \
	else \
	echo This is a 32-bit machine && $(MAKE) 32bit; \
	fi
	$(COMP.fc) $(LIBN).f

# 32-bit versions
32bit:
#	rm -f $(LIBN).f $(LIBN).inc
	@ echo 'C*** This file is automatically generated. Do not edit. ***' > \
	$(LIBN).f
	@ echo 'C    Edit "$(LIBNIN).f" instead.' >> $(LIBN).f
	@ echo 'C' >> $(LIBN).f
	@ echo 'C 32-bit machine version.' >> $(LIBN).f
	cat $(LIBNIN).f >> $(LIBN).f
	@ echo 'C*** This file is automatically generated. Do not edit. ***' > \
	$(LIBN).inc
	@ echo 'C    Edit "$(LIBNIN).inc" instead.' >> $(LIBN).inc
	@ echo 'C' >> $(LIBN).inc
	@ echo 'C 32-bit machine version.' >> $(LIBN).inc
	cat $(LIBNIN).inc >> $(LIBN).inc

# 64-bit versions
64bit:
	@ echo 'C*** This file is automatically generated. Do not edit. ***' > \
	$(LIBN).f
	@ echo 'C    Edit "$(LIBNIN).f" instead.' >> $(LIBN).f
	@ echo 'C 64-bit machine version.' >> $(LIBN).f
	@ echo 'C' >> $(LIBN).f
	sed 's/INTeger\*4/INTeger\*8/' $(LIBNIN).f >> $(LIBN).f
	@ echo 'C*** This file is automatically generated. Do not edit. ***' > \
	$(LIBN).inc
	@ echo 'C    Edit "$(LIBNIN).inc" instead.' >> $(LIBN).inc
	@ echo 'C' >> $(LIBN).inc
	@ echo 'C 64-bit machine version.' >> $(LIBN).inc
	sed 's/INTeger\*4/INTeger\*8/' $(LIBNIN).inc >> $(LIBN).inc

# HTM library
htmlib:
	@ cd $(HTMDIR); $(MAKE)

# Clean-up
clean:
	rm -f *.o $(EXE) $(LIBN).f $(LIBN).inc
	@ cd $(HTMDIR); ${MAKE} $@

# Tar file
tar: clean
	mkdir $(distdir)
	cp -a \
	$(HTMDIR) Makefile $(LIBNIN).inc $(LIBNIN).f \
	htmqry6.f myCat_qry.cpp mcsdemo_myCat_qry.f demo_htmID.f gethtmid.f htmid_pp.cpp \
	mcsdemo1.f mcsdemo2.f mcsdemo3.f mcsdemo4.f $(distdir)
	tar zcvf $(distdir).tar.gz $(distdir)
	rm -rf $(distdir)

# Dependecies
mcsdemo1.o mcsdemo2.o mcsdemo3.o mcsdemo4.o mcsdemo_myCat_qry.o: $(LIB)
