#
# Makefile for ESMA components.
#
# REVISION HISTORY:
#
# 3mar2004  Zaslavsky  Initial imlementation.
#

# Make sure ESMADIR is defined
# ----------------------------
ifndef ESMADIR
       ESMADIR := $(PWD)/../..
endif

# Compilation rules, flags, etc
# -----------------------------
  include $(ESMADIR)/Config/ESMA_base.mk  # Generic stuff
  include $(ESMADIR)/Config/ESMA_arch.mk  # System dependencies
  include $(ESMADIR)/Config/GMAO_base.mk  # GMAO definitions
  include ../Arch.mk

#                  ---------------------
#                  Standard ESMA Targets
#                  ---------------------


RN := $(shell basename $(PWD))
NTHIS = gfio
NLIB  = lib$(NTHIS).a
THIS = $(NTHIS)_$(RN)
LIB  = lib$(THIS).a

NREAL := $(FREAL)
ifeq ($(RN),r4)
      override FREAL = $(FREAL4)
else
      override FREAL = $(FREAL8)
endif

ifeq ($(IDL_GFIO),yes)
  GFIO_IDL = libgfio_idl_$(RN).so
endif

esma_install install: $(LIB) GFIO_mean_$(RN).x $(GFIO_IDL) # gfio_test.x
	$(MKDIR) $(ESMALIB) $(ESMAETC) $(ESMAINC)/$(THIS) $(ESMABIN)
	$(CP) -p *.x    $(ESMABIN)
	$(CP) -p *.a    $(ESMALIB)
	$(CP) -p *.[Mm][Oo][Dd] ../*.h $(ESMAINC)/$(THIS)
ifeq ($(NREAL),$(FREAL))
	$(RM) -r $(ESMALIB)/$(NLIB) $(ESMAINC)/$(NTHIS)
	cd $(ESMALIB); $(LN) -s ./$(LIB)  $(NLIB)
	cd $(ESMAINC); $(LN) -s ./$(THIS) $(NTHIS)
endif

esma_clean clean:
	$(RM) *___.F *~ *.[aox] *.[Mm][Oo][Dd]

esma_distclean distclean:
	$(RM) *___.F *~ *.[aoxd] *.[Mm][Oo][Dd]

esma_doc doc:
	@echo "Target $@ not implemented yet in `pwd`"


esma_help:
	@echo "Standard ESMA targets:"
	@echo "% make esma_install    (builds and install under ESMADIR)"
	@echo "% make esma_clean      (removes deliverables: *.[aox], etc)"
	@echo "% make esma_distclean  (leaves in the same state as cvs co)"
	@echo "% make esma_doc        (generates PDF, installs under ESMADIR)"
	@echo "% make esma_help       (this message)"
	@echo "Environment:"
	@echo "      ESMADIR = $(ESMADIR)"
	@echo "      BASEDIR = $(BASEDIR)"
	@echo "         ARCH = $(ARCH)"
	@echo "         SITE = $(SITE)"
	@echo "        FREAL = |$(FREAL)|"

help:
	@ echo "Available targets:"
	@ echo "  make lib             (to create libods.a)"
	@ echo "  make lib MODOBJS=    (to omit f90 modules)"
	@ echo "  make test            (to test ODS library)"
	@ echo "  make doc             (to create documentation)"
	@ echo "  make all"  
	@ echo "  make export"  
	@ echo "  make clean"  
	@ echo "  make distclean"
	@ echo "  make depend"  

GFIOSRC = gfio___.F gfioCF.F diffdate.f getdate.f timesubs.f gfioutil___.F \
          m_GFIO_GetFld.f m_GFIO_PutFld.f gfio_flush.F

#                  --------------------
#                  User Defined Targets
#                  --------------------

SRCS = $(GFIOSRC)
OBJS := $(addsuffix .o, $(basename $(SRCS)))
DEPS := $(addsuffix .d, $(basename $(SRCS)))

NCGEN = $(BASEBIN)/hdfgen

INC_DIRS = . .. $(INC_SDF) $(INC_MPEU)
MOD_DIRS = .    $(INC_SDF) $(INC_MPEU)

BIG_ENDIAN =

USER_FINCS  = $(foreach dir,$(INC_DIRS),$(I)$(dir)) 
USER_FMODS  = $(foreach dir,$(MOD_DIRS),$(M)$(dir)) 
USER_FFLAGS = $(PP) $(BIG_ENDIAN) 

vpath % $(INC_DIRS) 

$(LIB) lib : $(DEP) $(OBJS)
	$(RM) $(LIB)
	$(AR) $(AR_FLAGS) $(LIB) $(OBJS)

%___.F : %.f
	@$(CP) ../$*.f $*___.F

# program to test the GFIO/NetCDF software
# ----------------------------------------
gfio_test.x : gfio_test.o $(LIB)
	$(LD) $(LDFLAGS) -o gfio_test.x gfio_test.o $(LIB) $(LIB_SDF)

libgfio_idl_$(RN).so : $(LIB) gfio_idl.o 
	$(LD) $(LDFLAGS) -shared -o libgfio_idl_$(RN).so gfio_idl.o \
              $(LIB) $(LIB_SDF)
#	$(LD) $(LDFLAGS) -o testcall_$(RN).o testcall.o \
#              $(LIB) $(LIB_SDF)

GFIO_mean_$(RN).x : $(LIB) GFIO_mean.o
	$(LD) $(LDFLAGS) -o GFIO_mean_$(RN).x GFIO_mean.o \
	      $(LIB) $(LIB_MPEU) $(LIB_SDF) $(LIB_SYS)

test: gfio_test.x 
	gfio_test.x > fortran_test.txt
	diff fortran_test.txt fortran_sample.txt
	@ echo '        ***** gfio Fortran test successful ****'
#                  --------------------
#                      Dependencies
#                  --------------------

# Hack to prevent remaking dep files during cleaning
# --------------------------------------------------
  ifneq ($(findstring clean,$(MAKECMDGOALS)),clean)
    -include $(DEPS)
  endif

#include ../Makefile.depend

#.
