#GNU Makefile to build Library libtrgraf.a 
#
#04/22/00 C. Ludescher
#

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


ARC = $(OBJ)/lib/libtrgraf.a
OBJDIR = $(OBJ)/obj/trgraf

LDLIBS =  -lureadsub -lvaxonly -lcomput -lsg -ljc -lportlib

srcdir  = $(shell pwd)
DATA    = tgtest.ind tgtest_output.ps

#=============================================================
# f90 modules
Mnams = trgraf_wname_save.mod bottoms_up.mod
Mobjs = trgraf_wname_save.o bottoms_up.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 .for,.o, $(wildcard *.for)) $(subst .F,.o, $(wildcard *.F))
F90 = $(subst .f90,.o, $(wildcard *.f90)) $(subst .F90,.o, $(wildcard *.F90))
FALL = $(F) $(F90)
MEM = $(foreach file,$(FALL),$(ARC)($(file)))


.PHONY: clean realclean checkdir all exec

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

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

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

$(ARC): $(MODS) $(MEM)
	@$(RANLIB) $@ > /dev/null
 
#pre-process/compile fixed length fortran 90 code
$(ARC)(%.o): %.F90
	$(F90_proc)
	$(make_ar)

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

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

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

$(ARC)(%.o): %.for
	$(FC) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT)  $(OBJDIR)/$*.o $<
	$(make_ar)

#MG$(OBJDIR)/%.o: %.f
#MG	$(FC) $(FFLAGS) $(INCFLAGS) $(OUT) $@ $<

#MG$(OBJDIR)/%.o: %.for
#MG	$(FC) $(FFLAGS) $(INCFLAGS) $(OUT) $@ $<

#MG$(OBJDIR)/%.o: %.f90
#MG	$(FC) $(FFLAGS) $(INCFLAGS) $(OUT) $@ $<
#
#---------------------------------------------------

exec:
ifneq ("$(wildcard tgtest.*)","")
	@test -d $(OBJ)/test || mkdir -p $(OBJ)/test
	@test -d $(OBJ)/obj/trgraf || mkdir -p $(OBJ)/obj/trgraf
	@for i in $(DATA); do \
	(cd $(OBJ)/test; $(TSTLNK) $$i || ln -s $(srcdir)/$$i $$i;) done 
	$(MAKE) $(OBJ)/test/tgtest
else
	@echo 
endif

$(OBJ)/test/tgtest: $(OBJDIR)/tgtest.o $(THISLIB) 
	$(FC) $(LDFLAGS) -o $@ $< -L$(OBJ)/lib -ltrgraf $(LLOC) $(LDLIBS)\
	 $(XFLIB) $(LUSRLIB) $(EDITLIBS) $(NETCDF)

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

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

ckins:
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@test -d $(MODDIR) || mkdir -p $(MODDIR)

install: ckins
	@if test -f $(ARC); then \
	  umask 133; cp $(ARC) $(LIBDIR)/; \
	else test -f $(LIBDIR)/libtrgraf.a || echo ">>> can not find $(ARC)";fi
	$(foreach file,$(MODULES),$(shell cp $(MDIR)/$(file) $(MODDIR)/))

uninstall:
	rm $(LIBDIR)/libtrgraf.a
	$(foreach m,$(MODULES),$(shell rm -f $(MODDIR)/$(m) ))
