#-*- makefile -*-
#GNU Makefile to build Library libpest3_2.a 
#
#03/03/04 C. Ludescher-Furth
#
# define system dependent flags, etc.
ifneq ("$(wildcard ../share/Make.flags)","")
	SHR = ../share/Make.flags
	SHRL = ../share/Make.local
else
	SHR = $(CODESYSDIR)/source/misc/makeflags.mk
	SHRL = $(CONFIGDIR)/Make.local
endif
-include $(SHRL)
include $(SHR)


ifdef DEBUG
        FFLAGS := $(DFFLAGS)
        CXXFLAGS := $(DCXXFLAGS)
endif

ifeq ($(MACHINE),CRAY)
   CINCL := -I$(NETCDF_DIR)/include
endif

  ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
    ifeq ("${OBJ}",".")
	override OBJ=..
    endif
  endif
   EXEC=pest3.2
   MYEXEC=$(OBJ)/test/$(EXEC)
   PACKAGE=pest3_2
   OBJDIR = $(OBJ)/obj/$(PACKAGE)
   ARC =  $(OBJ)/lib/lib$(PACKAGE).a
   TO_DO= libs exec
   LIB_ONLY = chkdirs $(ARC)
   EXEC_ONLY = $(MYEXEC)
   DO_FIRST = $(LIB_ONLY)



.PHONY: all install clean realclean


# don't rebuild library if using installed one in $PREFIX 
ifeq ($(MAKELEVEL),0)
	THISLIB=$(ARC)
endif

srcdir = $(shell pwd)
DATA   = $(shell ls *.cdf INP1* *.m geqdsk gbeta8.8 *ref)

# f90 modules
Mnams = bzio.mod   dims.mod   pstcom.mod comggf.mod combla.mod \
	comivi.mod l22com.mod mtrik1.mod newmet.mod comfft.mod \
	l21com.mod r33com.mod l34com.mod temps.mod  eigens.mod
Mobjs = bzio.o com.o
MODS = $(foreach file,$(Mobjs),$(ARC)($(file)))

ifeq ($(MODUP),Y)
 MODS0=$(foreach m,$(Mnams),$(shell  echo $(m) | tr 'a-z' 'A-Z'))
 MODULES=$(foreach m,$(MODS0),$(MDIR)/$(subst .MOD,.$(MODEXT),$(m)))
else
 MODULES = $(foreach m,$(Mnams),$(MDIR)/$(subst .mod,.$(MODEXT),$(m)))
endif 

# library members
ALLMEM = $(subst .f90,.o, $(wildcard *.f90))
FMEM =$(filter-out pest3x.o, $(ALLMEM))
MEM = $(foreach m,$(FMEM),$(ARC)($(m)))


  ifdef NO_TRXPLIB
      TRXPLIB= -lgeneric_dummy
  else
      TRXPLIB=-ltrxplib -ltrread -ltr_getnl -lrp_kernel -lrplot_io \
	-lmds_sub -lxdatmgr -linterp_sub
  endif

      LDLIBS = -L$(OBJ)/lib -li2mex  $(LLOC) -lesc -llsode -llsode_linpack \
	$(TRXPLIB)  -lold_xplasma -lxplasma_debug -lxplasma2 -lgeqdsk_mds \
	-lnscrunch -lfluxav -lr8bloat -lsmlib -lpspline -lezcdf -lmdstransp \
	-lmclib -lcomput -lvaxonly  -lportlib  


default: $(DO_FIRST)

all: $(TO_DO)
	@echo done 

libs: $(LIB_ONLY)

exec: $(EXEC_ONLY)


$(OLDLIB): timestamp.inf
	@echo "--- DETECTED $(PACKAGE).a source update"
	@echo "--- Re-Making $(ARC)"
	@$(MAKE) libs

chkdirs:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJ)/mod || mkdir -p $(OBJ)/mod
	@test -d $(OBJDIR)  || mkdir -p $(OBJDIR)
	@test -d $(OBJ)/test || mkdir $(OBJ)/test

# compile f90
$(ARC)(%.o): %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)

$(ARC): $(MODS) $(MEM)
	@$(RANLIB) $@ > /dev/null

exec: $(MYEXEC) makelink 

makelink: 
	@for i in $(DATA); do \
	(cd $(OBJ)/test; test -f $$i || ln -s $(srcdir)/$$i $$i;) done 
	@test -d $(OBJ)/bin || ln -s $(OBJ)/test $(OBJ)/bin
	@test -f $(OBJ)/test/Makefile || \
	{ echo tests:; \
	echo "	@rm -f results"; \
	echo '	@echo   "zero-beta 1/1 mode ..."' ;\
	echo '	@./pest3.2 -i1 -f"inp1.cdf" -m1 -n1 > results' ;\
	echo '	@echo " "';\
	echo '	@echo "1/1 mode radial convergence study..."'; \
	echo '	@./pest3.2 -i1 -f"inp1.cdf" -m1 -n1 -k"100 50 70 140" >> results'; \
	echo '	@echo " "';\
	echo '	@echo "n=2 coupled rational surfaces 3/2 4/2 "'; \
	echo '	@./pest3.2 -i1 -f"inp1.cdf" -n2 -m".xx." >> results'; \
	echo '	@echo " "';\
	echo '	@echo "finite beta eqdsk.cdf input, q-axis=1.1, uniform mesh,"'; \
	echo '	@echo "wall at infinity, -12..+12 modes, symmetric large solution support"'; \
	echo '	@./pest3.2 -i2 -f"eqdsk.cdf" -q-1.1 -u -b11.0 -l12 -S >> results';\
	echo '	@echo " "';\
	echo '	@echo "finite beta geqdsk input 4/3 mode no-wall"'; \
	echo '	@echo "*Small differences can occur between platforms* "';\
	echo '	@./pest3.2 -i3 -f"geqdsk" -n3 -m4 -b11.0 >> results'; \
	echo "	@echo printing out differences"; \
	echo "	diff results results_ref"; } > $(OBJ)/test/Makefile


$(OBJDIR)/%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(CDEFS) $(CINCL) -o $@ $<

$(MYEXEC): $(OBJDIR)/pest3x.o $(ARC)
	$(CXX) $(LDCFLAGS) -o $@ $< $(ARC) $(LDLIBS) $(MDSLIB) $(LUSRLIB) $(NETCDF) $(LAPACK) $(BLAS) $(FORTLIBS)

#-----------------------------------------------------------------

tests:
	@(cd $(OBJ)/test; \
	rm results; \
	echo " zero beta 1/1 mode ..."; \
	./$(EXEC) -i1 -f"inp1.cdf" -m1 -n1 > results; \
	echo " 1/1 mode radial convergence study..."; \
	./$(EXEC) -i1 -f"inp1.cdf" -m1 -n1 -k"100 50 70 140" >> results; \
	echo " n=2 coupled rational surfaces 3/2 4/2 "; \
	./$(EXEC) -i1 -f"inp1.cdf" -n2 -m".xx." >> results; \
	echo " finite beta eqdsk.cdf input, q-axis=1.1, uniform mesh, wall at infinity, -12..+12 modes, symmetric large solution support"; \
	./$(EXEC) -i2 -f"eqdsk.cdf" -q-1.1 -u -b11.0 -l12 -S >> results; \
	echo "  finite beta geqdsk input 4/3 mode no-wall"; \
	echo " *Small differences can occur between platforms* "; \
	./$(EXEC) -i3 -f"geqdsk" -n3 -m4 -b11.0  >> results; \
	echo "printing out differences"; \
	diff results results_ref)

#--------------------------------------------------------------------
install: installdirs installlib

installlib: 
	umask 133; cp $(OBJ)/lib/lib$(PACKAGE).a $(LIBDIR)
#	umask 133; cp get_c.3 $(MANDIR)/man3


installdirs:
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@test -d $(MANDIR)/man3 || mkdir -p $(MANDIR)/man3


clean:
	@rm -f $(OBJDIR)/*
	@(cd $(OBJ)/test; rm -f pest3* )

realclean: clean
	@rm -f $(ARC)
	@(cd $(OBJ)/test; rm -f $(DATA))



