#GNU Makefile to build Library libfrantic.a 
#
#12/04/02 C. Ludescher : created

ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
ifeq ("${OBJ}",".")
	override OBJ=..
endif
endif

# define system dependent flags, etc.
-include  ../share/Make.local
include ../share/Make.flags


ifdef DEBUG
	FFLAGS = $(DFFLAGS)
endif

ifdef SHARED
	FFLAGS := $(FFLAGS) $(X_FFLAGS)
endif

LINK = $(FC)

ARC = $(OBJ)/lib/libfrantic.a
OBJDIR = $(OBJ)/obj/frantic

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

#=============================================================
# f90 modules
#public Module names
Mnams = frantic_module.mod
# source file names
Mobjs = frantic_module.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),$(subst .MOD,.$(MODEXT),$(m)))
else
 MODULES = $(foreach m,$(Mnams),$(subst .mod,.$(MODEXT),$(m)))
endif 
 
# fortran archive members
F  = $(subst .f,.o, $(wildcard *.f)) $(subst .F,.o, $(wildcard *.F))
F90 = $(subst .f90,.o, $(wildcard *.f90)) $(subst .F90,.o, $(wildcard *.F90))
FALL = $(F) $(F90) 

#remove test programs
FMEM =$(filter-out frantic_test.o, $(FALL))
MEM = $(foreach file,$(FMEM),$(ARC)($(file)))

OBJECTS = $(foreach file,$(Mobjs) $(FMEM), $(OBJDIR)/$(file))

LDLIBS =  -L$(OBJ)/lib -lphysconst -lfrantic -L$(PSPLINE_HOME)/lib -lpspline \
          $(LLOC) -lfpreact -lpreact -lsigsub -laladdinsub -ladpak -lcppsub \
	  -lezcdf -lcomput -luflib -lufhdf -lureadsub -lvaxonly -lportlib 

srcdir = $(shell pwd)
testdir = $(shell cd $(OBJ)/test; pwd)
DATA   = $(shell $(LS) *.cdf *.ref)


.PHONY: clean realclean checkdir all exec chktest

libs: FORCE  $(ARC)
 
all:  libs exec 
	@echo done  

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

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

chkdirs:
	@test -d $(OBJ)/obj/frantic || mkdir -p $(OBJ)/obj/frantic
	@test -d $(OBJ)/test || mkdir $(OBJ)/test
 
$(ARC): $(MODS) $(MEM)
	@$(RANLIB) $@ > /dev/null


 
#pre-process/compile fixed length fortran 90 code
$(ARC)(%.o): %.F
	$(F_proc)
	$(make_ar)

#  compile fixed length fortran 90 code
$(ARC)(%.o): %.f
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(make_ar)


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

# pre-process/compile free form fortran 90 code
$(ARC)(%.o): %.F90
	$(F90_proc)
	$(make_ar)

#
#---------------------------------------------------
#
# Build Test Programs
#=====================

$(OBJDIR)/%.o: %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $@ $<
	$(mv_mod)


exec: chkdirs  chktest $(OBJ)/test/frantic_test

chktest:
	@for i in $(DATA); do \
	(cd $(OBJ)/test; test -f $$i || ln -s $(srcdir)/$$i $$i;) done

$(OBJ)/test/frantic_test: $(OBJDIR)/frantic_test.o $(THISLIB)
	$(LINK) $(LDFLAGS) -o $@ $< $(LDLIBS) $(XFLIB) \
	      $(XLIBS) $(LUSRLIB) $(NETCDF) $(MDSLIB) $(CLIBS)


#-------------------------------------------------------------------------
install:
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@test -d $(BINDIR) || mkdir -p $(BINDIR)
	@umask 133; cp $(OBJ)/lib/libfrantic.a $(LIBDIR)/
	@test -d $(MODDIR) || mkdir $(MODDIR)
	@(cd $(MDIR); umask 133; cp $(MODULES) $(MODDIR)/)
#	@test -d $(MANDIR) || mkdir -p $(MANDIR)/man3
#	@umask 133; cp frantic.3 $(MANDIR)/man3/

uninstall:
	rm -f $(LIBDIR)/libfrantic.a
#	rm $(MANDIR)/man3/frantic.3
#	(cd $(MODDIR); rm -f $(MODULES))

clean: 
	@rm -f $(OBJDIR)/*
	@rm -f $(OBJ)/test/frantic_test

realclean: clean
	@rm -f $(ARC)
	@if test -d $(MDIR); then \
	  (cd $(MDIR); rm -f $(MODULES)); fi

