#-------------------------------------------------------------------------------
#
# include configuration
#
ifeq ($(wildcard make.config),make.config)
$(info )
$(info Using customized "$(wildcard make.config)" file.)
$(info )
include make.config
else
$(info )
$(info Could not find the customized "make.config" file!)
$(info )
$(info Copy "make.default" to "make.config" and customize it, if you do not want the default configuration.)
$(info )
$(shell sleep 5)
include make.default
endif

#-------------------------------------------------------------------------------
#
# host variables
#
HOST    := $(shell hostname -f)
OS      := $(shell uname)
DATE    := $(shell date +%Y%m%d%H%M)

include hosts/default
ifeq ($(wildcard hosts/$(HOST)),hosts/$(HOST))
$(info Using compiler setup from "hosts/$(HOST)" file.)
$(info )
include hosts/$(HOST)
endif

#-------------------------------------------------------------------------------
#
# check flag conditions
#
ifeq ($(EQS),HYDRO)
ifeq ($(FLUX),HLLD)
FLUX = HLLC
endif
ifeq ($(EOS),ISO)
FLUX = HLL
endif
endif
ifeq ($(EQS),MHD)
ifeq ($(FLUX),HLLC)
FLUX = HLLD
endif
endif

#-------------------------------------------------------------------------------
#
# pass additional flags to the compiler
#
# number of dimensions
#
FFLAGS  += -DNDIMS=${NDIMS}
ifeq ($(SHAPE),Y)
FFLAGS  += -DSHAPE
endif

# check if reconstruction is TVD
#
ifeq ($(SPACE),MINMOD)
FFLAGS  += -DTVD
endif
ifeq ($(SPACE),LF)
FFLAGS  += -DTVD
endif
ifeq ($(SPACE),MP5)
FFLAGS  += -DMP
endif
ifeq ($(SPACE),MP7)
FFLAGS  += -DMP
endif
ifeq ($(SPACE),MP9)
FFLAGS  += -DMP
endif

# output data format
#
FFLAGS  += -D${OUTPUT}

# compression
#
ifeq ($(COMPRESS),Y)
FFLAGS  += -DDEFLATE
endif

# precision
#
FFLAGS  += -DPREC=${PREC}

# time integration method
#
FFLAGS  += -D${TIME}

# spacial reconstruction method
#
FFLAGS  += -D${SPACE}

# set of equations
#
FFLAGS  += -D${EQS}

# equation of state
#
FFLAGS  += -D${EOS}

# flux approximation method
#
FFLAGS  += -D${FLUX}

# electromotive force approximation method
#
ifeq ($(EQS),MHD)
FFLAGS  += -D${FLUXEMF}
endif

# source terms
#
ifeq ($(FORCING),Y)
FFLAGS  += -DFORCE
endif
ifeq ($(RESISTIVITY),Y)
FFLAGS  += -DRESIS
endif

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

.SUFFIXES:
.SUFFIXES: .F90 .o

.F90.o:
	$(FC) -c $(FFLAGS) $*.F90

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

name = godunov-amr

default: $(name).x

sources = blocks.F90 boundaries.F90 config.F90 constants.F90 driver.F90        \
          error.F90 evolution.F90 forcing.F90 interpolation.F90 io.F90         \
          mesh.F90 mpitools.F90 problem.F90 random.F90 scheme.F90 timer.F90    \
          variables.F90
objects = blocks.o boundaries.o config.o constants.o driver.o error.o          \
          evolution.o forcing.o interpolation.o io.o mesh.o mpitools.o         \
          problem.o random.o scheme.o timer.o variables.o
files   = $(sources) makefile make.default config.in license.txt hosts

$(name).x: $(objects)
	$(LD) $(LDFLAGS) $(objects) $(LIBS) -o $(name).x

arch: $(files)
	tar czvf $(name)-$(DATE).tar.gz $(files)

clean:
	rm -rf *.x *.o *.mod *.il

clean-all:
	rm -rf *.x *.o *.mod *.il *.out *.dat *.bin *.h5 *.hdf *~ gprof.txt

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

blocks.o        : blocks.F90 config.o error.o mpitools.o variables.o
boundaries.o    : boundaries.F90 blocks.o config.o error.o interpolation.o     \
                  mpitools.o variables.o
config.o        : config.F90 error.o
constants.o     : constants.F90
driver.o        : driver.F90 config.o evolution.o forcing.o io.o mesh.o        \
                  mpitools.o random.o timer.o
error.o         : error.F90
evolution.o     : evolution.F90 blocks.o boundaries.o config.o forcing.o       \
                  interpolation.o mesh.o mpitools.o problem.o scheme.o timer.o \
                  variables.o
forcing.o       : forcing.F90 config.o constants.o mesh.o mpitools.o random.o
interpolation.o : interpolation.F90 blocks.o config.o variables.o
io.o            : io.F90 blocks.o error.o mesh.o mpitools.o scheme.o variables.o
mesh.o          : mesh.F90 blocks.o config.o error.o interpolation.o           \
                  mpitools.o problem.o variables.o
mpitools.o      : mpitools.F90
problem.o       : problem.F90 blocks.o constants.o scheme.o variables.o
scheme.o        : scheme.F90 blocks.o config.o interpolation.o variables.o
random.o        : random.F90 mpitools.o
timer.o         : timer.F90
variables.o     : variables.F90

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