#GNU Makefile to build Library libureadsub.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/libureadsub.a
OBJDIR = $(OBJ)/obj/ureadsub

LDLIBS = -lsgdummy -lvaxonly -lportlib

#=============================================================
# f90 modules
# public modules names

Mnams = urd_progmod.mod userio.mod
Mobjs = urd_progmod.o userio_mod.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
F1  = $(subst .f90,.o, $(wildcard *.f90)) $(subst .F90,.o, $(wildcard *.F90))
F2  = $(subst .f,.o, $(wildcard *.f)) $(subst .F,.o, $(wildcard *.F)) $(subst .for,.o, $(wildcard *.for))  
FALL = $(F1) $(F2)
FMEM  = $(filter-out nulltron.o, $(FALL))
MEM = $(foreach file,$(FMEM),$(ARC)($(file)))


.PHONY: clean realclean checkdir all exec

libs: FORCE $(ARC)

all:  libs 
	@echo done  

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

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

# compile 
$(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)

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

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

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

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

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

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

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

exec:
ifneq ("$(wildcard nulltron.*)","")
	@test -d $(OBJ)/test || mkdir -p $(OBJ)/test
	@test -d $(OBJ)/obj/ureadsub || mkdir -p $(OBJ)/obj/ureadsub
	$(MAKE) $(OBJ)/test/nulltron 
else
	@echo 
endif

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

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

realclean: clean
	@rm -f $(OBJ)/lib/*ureadsub.a

install: 
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@if test -f $(ARC); then \
	  umask 133; cp $(ARC) $(LIBDIR)/; \
	else test -f $(LIBDIR)/libureadsub.a || echo ">>> can not find $(ARC)";fi
	@if test -f uread.3; then \
	 test -d $(MANDIR)/man3 || mkdir -p $(MANDIR)/man3; \
	 umask 133; cp uread.3 $(MANDIR)/man3/; fi
	@test -d $(MODDIR) || mkdir $(MODDIR)
	@(cd $(MDIR); umask 133; cp $(MODULES) $(MODDIR)/)

uninstall:
	rm -f $(LIBDIR)/libureadsub.a
	rm -f $(MANDIR)/man3/uread.3
	(cd $(MODDIR); rm -f $(MODULES))
