##############################################################################
#This is the top makefile for compiling OASIS3-MCT coupling library
# withing the EC-Earth environment.
#######

include make.inc

SHELL = /bin/sh
MAKEf = $(MAKE) -f TopMakefileOasis3

# Defining path for FORTRAN and C sources
LIBSRC  = $(COUPLE)/lib

#######
default: 
	@$(MAKEf) makedirs
	@$(MAKEf) makemct
	@$(MAKEf) makelibs
	@$(MAKEf) makepsmile

realclean:
	$(MAKEf) cleanlibs

# Rules for creating build and library directories
#
makedirs:
	@mkdir -p $(ARCHDIR)/build/lib/scrip
	@mkdir -p $(ARCHDIR)/build/lib/mctdir
	@mkdir -p $(ARCHDIR)/build/lib/mct
	@mkdir -p $(ARCHDIR)/build/lib/psmile.$(CHAN)
	@mkdir -p $(ARCHDIR)/lib

# Rules for compiling mpeu/mct library
# 
makemct:
	@echo "==> Compiling library mpeu and mct"
	@(cd $(ARCHDIR)/build/lib/mctdir;                    \
      cp -f -r -p $(LIBSRC)/mct/* .;                     \
      ./configure MPIFC="$(F90)" FC="$(F90)" CC="$(CC)" CFLAGS="$(addprefix -I,$(MPI_INCLUDE))" FCFLAGS="$(addprefix -I,$(MPI_INCLUDE))"; \
      $(MAKE);                                           \
      cp -f ./*/lib*.a $(ARCHDIR)/lib/;                  \
      cp -f ./*/*.o    $(ARCHDIR)/build/lib/mct/;        \
      cp -f ./*/*.mod  $(ARCHDIR)/build/lib/mct/;        )

# Rules for compiling the libraries scrip and psmile
#
makelibs:
	@echo "==> Compiling library scrip"
	@(cd $(ARCHDIR)/build/lib/scrip;        \
      cp -f $(LIBSRC)/scrip/src/Makefile .; \
      $(modifmakefile) ;                    \
      $(MAKE) all                           )

makepsmile:
	@echo "==> Compiling library psmile"
	(cd $(ARCHDIR)/build/lib/psmile.$(CHAN); \
     cp -f $(LIBSRC)/psmile/src/Makefile .;  \
     $(modifmakefile);                       \
     $(MAKE) all                             )

# Rules for cleaning
#
cleanlibs:
	@echo "==> Cleaning OASIS3 libraries"
	@rm -f $(ARCHDIR)/build/lib/scrip/*
	@rm -f $(ARCHDIR)/build/lib/mct/*
	@rm -f -r $(ARCHDIR)/build/lib/mctdir/*
	@rm -f $(ARCHDIR)/build/lib/psmile.$(CHAN)/*
	@rm -f $(ARCHDIR)/lib/libscrip.a
	@rm -f $(ARCHDIR)/lib/libmct.a
	@rm -f $(ARCHDIR)/lib/libmpeu.a
	@rm -f $(ARCHDIR)/lib/libpsmile.$(CHAN).a

## Some rules to modify/add variables in makefiles
#
modifmakefile =	( echo 'include $(COUPLE)/util/make_dir/make.inc' \
                         > infile; cat Makefile >> infile ; cp -f infile Makefile )
#
########################################################################################