#
# Master Makefile for making the 'getm' executable.
#
include Rules.make

#$(GETMDIR)/src/halos		\

# Set the subdirectories with Makefiles
SUBDIRS = \
$(GETMDIR)/src/futils		\
$(GETMDIR)/src/domain		\
$(GETMDIR)/src/pool		\
$(GETMDIR)/src/meteo		\
$(GETMDIR)/src/ice		\
$(GETMDIR)/src/waves		\
$(GETMDIR)/src/les		\
$(GETMDIR)/src/2d
ifneq ($(GETM_NO_3D),true)
SUBDIRS += \
$(GETMDIR)/src/3d
endif
SUBDIRS += \
$(GETMDIR)/src/output		\
$(GETMDIR)/src/ncdf		\
$(GETMDIR)/src/input		\
$(GETMDIR)/src/getm		\
$(GETMDIR)/src/coupling

ifneq ($(or $(findstring true,$(GETM_NUOPC_EXEC)), $(findstring true,$(GETM_ESMF_EXEC)) ),)
CORE_LIBS	=	\
		$(LIBDIR)/libgetm_esmf${buildtype}.a
else
ifeq ($(GETM_OASIS),true)
CORE_LIBS	=	\
		$(LIBDIR)/libgetm_oasis${buildtype}.a
endif
endif
CORE_LIBS	+=	\
		$(LIBDIR)/libgetm${buildtype}.a 	\
		$(LIBDIR)/liboutput${buildtype}.a	\
		$(LIBDIR)/libmeteo${buildtype}.a
ifneq ($(GETM_NO_3D),true)
CORE_LIBS	+=	\
		$(LIBDIR)/lib3d${buildtype}.a
endif
CORE_LIBS	+=	\
		$(LIBDIR)/lib2d${buildtype}.a	\
		$(LIBDIR)/libwaves${buildtype}.a	\
		$(LIBDIR)/libice${buildtype}.a	\
		$(LIBDIR)/libles${buildtype}.a	\
		$(LIBDIR)/libpool${buildtype}.a	\
		$(LIBDIR)/libdomain${buildtype}.a 	\
		$(LIBDIR)/libinput${buildtype}.a	\
		$(LIBDIR)/libncdfio${buildtype}.a	\
		$(LIBDIR)/libfutils${buildtype}.a

#		-lhalo${buildtype} 	\

LIBS		= $(CORE_LIBS) $(EXTRA_LIBS)

all: build extra install confdir

build: GIT FORTRAN ../VERSION makedirs subdirs

extra: nuopc

nuopc nuopcinstall: build
ifeq ($(GETM_ESMF),true)
	$(RM) $(LIBDIR)/libgetm_all.a
	$(AR) Trs $(LIBDIR)/libgetm_all.a $(LIBDIR)/libgetm_esmf${buildtype}.a $(LIBS)
ifeq ($(GETM_NUOPC),true)
	$(MAKE) -C coupling $@ DESTDIR=$(DESTDIR)
endif
endif


../VERSION: ../Makefile Makefile Rules.make
	$(MAKE) -C ..

confall: confdir confflags

confdir:
	@echo "GETMDIR:    "$(GETMDIR)
ifdef  GOTM_PREFIX
	@echo "GOTM_PREFIX:"$(GOTM_PREFIX)
else
	@echo "GOTMDIR:    "$(GOTMDIR)
endif
ifeq ($(FABM),true)
ifeq ($(wildcard $(GOTMLIBDIR)/libfabm.*), )
ifdef  FABM_PREFIX
	@echo "FABM_PREFIX:"$(FABM_PREFIX)
else
	@echo "FABMDIR:    "$(FABMDIR)
endif
endif
endif
	@echo "BINDIR:     "$(BINDIR)
	@echo "MODDIR:     "$(MODDIR)
	@echo "LIBDIR:     "$(LIBDIR)

confflags:
	@echo "DEFINES:    "$(DEFINES)
	@echo "INCDIRS:    "$(INCDIRS)
	@echo "LINKDIRS:   "$(LINKDIRS)
	@echo "EXTRA_LIBS: "$(EXTRA_LIBS)
	@echo "F90FLAGS:   "$(F90FLAGS)

GIT FORTRAN:
	$(MAKE) -C .. $@

getm$(buildtype)_$(FORTRAN_COMPILER):
	$(FC) -o $@ getm/main.F90 $(F90FLAGS) $(EXTRA_FFLAGS) $(LINKFLAGS) $(LIBS) $(SYS_LIBS)

install: getm$(buildtype)_$(FORTRAN_COMPILER)
	mkdir -p $(BINDIR)
	mv getm$(buildtype)_$(FORTRAN_COMPILER) $(BINDIR)

.PHONY: subdirs $(SUBDIRS)

makedirs:
	mkdir -p $(MODDIR) $(LIBDIR)

subdirs: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE) -C $@

modules:
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i $@; done

objects:
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i $@; done

doc:
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i $@; done

bkup: distclean
	tar -czvf ../../backups/getm.$$USER.`date +%Y%m%d%H`_`cat ../VERSION`.tar.gz *

clean:

realclean: clean
	$(RM) ../modules/*.mod ../lib/lib*$(buildtype).a
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i $@; done

distclean: realclean
	$(RM) ../lib/*/lib*.a
	$(RM) ../modules/*/*
	$(RM) ../bin/getm_*

#-----------------------------------------------------------------------
# Copyright (C) 2001 - Hans Burchard and Karsten Bolding (BBH)         !
#-----------------------------------------------------------------------
