#-------------------------------------------------------------------------
# Usage :: 
#    1. make             : Make torus with optimization flag(s)
#    2. make clean       : Clean up directory
#    3. make cfitsio=no  : Use cfitsio stubs rather than library
#    4. make debug=yes   : Make torus with debug option,
#    5. make profile=yes : Make torus with profiler option
#    6. make static=yes  : Make torus with static linking
#    7. make trace=yes   : Make torus with ITAC profiling
#    8. make openmp=yes  : Make torus with OpenMP 
#    9. make coverage=yes: Generate coverage statistics with gnu compilers 
#   10. make zlib=yes:   : Use z-lib compression libary to produce vtu files
#-------------------------------------------------------------------------

USEMKL  = no
cfitsio = yes
memcheck = yes
zlib = yes
debug   = no
profile = no
static  = no
trace   = no
openmp  = yes 
v2      = yes
coverage = no
withsphng = no
hydro = yes
molecular = yes
photoion = yes
sph = yes
atomic = yes
# These are set later on, don't set in the make command
stateq = no
datacube = no
# Run script to get subversion version number
getsvnver = yes

PATH2G2C     = /usr/lib/gcc/i686-apple-darwin10/4.2.1/
GUANINE_HOME = /Network/Guanine

KNOWN_SYSTEM = no
MPIFLAG = 
#
# Set system-dependent variables
#

ifeq ($(SYSTEM), davesmac)
    F95COMP  = ifort
    DEBUGFLAG=-O0 -warn -traceback -debug full -check pointer -CB -ftz -save-temps -shared-intel #-assume nounderscore
    FASTFLAG = -mtune=core2 -openmp -O3 -mdynamic-no-pic -no-prec-div -fp-model fast=2 -no-prec-sqrt -shared-intel -ip -pad#-g -save-temps -traceback -mmacosx-version-min=10.4 -intel-static#-ipo #-g-ip
    CFITSIOLIBS = -L/opt/local/lib/ -lcfitsio # -libcfitsio ?
    ZLIBLIBS = -lz	
    MKLIBS = -I/Library/Frameworks/Intel_MKL.framework/Versions/10.0.5.025/Headers -L/Library/Frameworks/Intel_MKL.framework/Versions/10.0.5.025/Libraries/universal -lmkl_lapack -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -pthread
#    STARLIBS = -limf                                                                   
    EXE = torus.intelmac
    KNOWN_SYSTEM = yes
endif

#
# Deal with make options
#
ETCFLAG  =

ifeq ($(cfitsio), no)
   CFITSIOLIBS  =
else
   ETCFLAG += -DUSECFITSIO
endif

ifeq ($(zlib), no)
   ZLIBLIBS  =
else
   ETCFLAG += -DUSEZLIB
endif

ifeq ($(memcheck), yes)
   ETCFLAG += -DMEMCHECK
endif

ifeq ($(USEMKL), yes)
   ETCFLAG += -DUSEMKL
else
   MKLIBS = 
endif

ifeq ($(debug), yes)
   FASTFLAG =
   ETCFLAG += $(DEBUGFLAG)
endif

ifeq ($(profile), yes)
   ETCFLAG += -pg
endif

ifeq ($(coverage), yes)
   ETCFLAG +=  -fprofile-arcs -ftest-coverage
   FASTFLAG = 
endif

ifeq ($(trace), yes)
   TRACEFLAG = -trace
   ETCFLAG  += -tcollect
else
   TRACEFLAG = 
endif

ifeq ($(static), yes)
   ETCFLAG += $(STATICFLAG)
else
   STATICLIBS =
endif

# Set OpenMP flags
ifeq ($(openmp), yes)
  OPENMPFLAG = -openmp
  ifeq ($(SYSTEM), gfortran)
     OPENMPFLAG = -fopenmp 
# -static-libgcc fixes linking problems under Mac OS
     STARLIBS += -static-libgcc
     FASTFLAG = 
  endif
  ifeq ($(SYSTEM), ompiosx)
     OPENMPFLAG = -fopenmp 
     STARLIBS += -static-libgcc
     FASTFLAG = 
  endif
  ETCFLAG += $(OPENMPFLAG)
endif

# Add flags required for sphNG compatibility
ifeq ($(withsphng), yes)
  ETCFLAG +=  -mcmodel=medium -i-dynamic
endif

ifeq ($(hydro), yes)
  ETCFLAG += -DHYDRO
endif

ifeq ($(molecular), yes)
  ETCFLAG += -DMOLECULAR
  datacube = yes
endif

ifeq ($(photoion), yes)
  ETCFLAG += -DPHOTOION
  stateq = yes
endif

ifeq ($(sph), yes)
  ETCFLAG += -DSPH
endif

ifeq ($(atomic), yes)
  ETCFLAG += -DCMFATOM
  stateq = yes
  datacube = yes
endif

ifeq ($(stateq), yes)
  ETCFLAG += -DSTATEQ
endif

ifeq ($(datacube), yes)
  ETCFLAG += -DFITSCUBE
endif

ifeq ($(v2), yes)
   TORUSMAINSOURCE= torusMainV2.F90
   INPUTSMODSOURCE= inputs_modV2.F90
else	
   TORUSMAINSOURCE= torusMain.F90
   INPUTSMODSOURCE= inputs_mod.F90
endif

#
# Source files
#

VERSIONSOURCE = torus_version_mod.f90 

F90SOURCE = kind_mod.f90 constants_mod.f90 atom_mod.f90 vector_mod.f90 image_utils_mod.f90 \
            sed_mod.f90 utils_mod.f90  phfit2.f90  bhmie_mod.f90 mieDistCrossSection_mod.f90 \
            gaussian_mod.f90 math_mod.f90 dimensionality_mod.f90 \
            phasematrix_mod.f90 photon_mod.f90 blob_mod.f90 \
            distortion_mod.f90  \
            disc_hydro_mod.f90 gas_opacity_mod.f90 \
            integratePath.f90 fillGridTio.f90 fillGridRayleigh.f90 \
            fillGridThomson.f90 amr_utils_mod.f90  \
            jets_mod.f90 h21cm_mod.f90 \
            source_mod.f90 spectrum_mod.f90 \
            wr104_mod.f90 density_mod.f90 \
            sph_data_class.f90 cluster_class.f90 vh1_mod.f90 \
            timing.f90  isochrone_class.f90 \
            filter_set_class.f90 ostar_mod.f90  \
            surface_mod.f90  math_mod2.f90 disc_class.f90 \
            hyd_col_coeff.f90 clump_mod.f90 \
            discwind_class.f90 zeus_data_class.f90 luc_cir3d_class.f90 \
            cmfgen_class.f90 modelatom_mod.f90  magField.f90 \
            romanova_class.f90 messages_mod.f90 starburst_mod.f90 \
            magnetic_mod.f90 units_mod.f90 interferometry_mod.f90 \
            qShep3D_mod.f90 qShep2D_mod.f90 $(VERSIONSOURCE)
            


CAPF90SOURCE =	 amr_mod.F90  octal_mod.F90 mpi_global_mod.F90 random_mod.F90 unix_mod.F90 photoion_utils_mod.F90 photoionAMR_mod.F90 photoion_mod.F90 \
	         mpi_amr_mod.F90 hydrodynamics_mod.F90 cmf_mod.F90  nbody_mod.F90 ion_mod.F90 gridtype_mod.F90 \
	         fits_utils_mod.F90 diffusion_mod.F90 formal_solutions.F90 lucy_mod.F90 molecular_mod.F90 \
	         parallel_mod.F90 stateq_mod.F90 image_mod.F90 datacube_mod.F90 vtk_mod.F90 grid_mod.F90 TTauri_mod.F90 \
                 phaseloop_mod.F90 gridio_mod.F90 angularImage_mod.F90  timedep_mod.F90 zlib_mod.F90 dust_mod.F90 mieDistPhaseMatrix.F90  \
	         setupamr_mod.F90 physics_mod.F90 outputs_mod.F90 memory_mod.F90 $(TORUSMAINSOURCE)

CAPF90SOURCELIB = mpi_global_mod.F90 random_mod.F90 unix_mod.F90  memory_mod.F90 setupamr_mod.F90 physics_mod.F90 outputs_mod.F90 \
	hydrodynamics_mod.F90 angularImage_mod.F90 \
	photoionAMR_mod.F90 photoion_mod.F90 mpi_amr_mod.F90 cmf_mod.F90 \
	          diffusion_mod.F90 formal_solutions.F90 lucy_mod.F90 molecular_mod.F90 \
	          parallel_mod.F90 stateq_mod.F90 torusMod.F90 image_mod.F90 datacube_mod.F90 vtk_mod.F90 \
                  phaseloop_mod.F90 gridio_mod.F90 

# Object files
#
INPUTSMODOBJ = $(INPUTSMODSOURCE:.f90=.o)
F90OBJ = $(F90SOURCE:.f90=.o)
CAPF90OBJ =  $(CAPF90SOURCE:.F90=.o)
CAPF90OBJLIB =  $(CAPF90SOURCELIB:.F90=.o)
ALLOBJ = $(CAPF90OBJ) $(F90OBJ)  $(INPUTSMODOBJ)
LIBOBJ = $(CAPF90OBJLIB) $(F90OBJ)  $(INPUTSMODOBJ)

#
# Compilers and compiler flags
#

F90 = $(F95COMP) $(F95FLAG) $(FASTFLAG) $(ETCFLAG) $(MATHTRAP) $(MPI_INC) $(MPIFLAG) $(XMLINCLUDE)

#
#


all: depends $(ALLOBJ)
    ifeq ($(KNOWN_SYSTEM), yes)
	    @echo ""
	    @echo "Linking object files and libraries..."
	    $(F90) $(TRACEFLAG) -o $(EXE) \
	    $(INPUTSMODOBJ) $(F90OBJ) $(CAPF90OBJ) \
	    $(CFITSIOLIBS) $(STARLIBS) $(F2CLIB) $(STATICLIBS) $(MKLIBS) $(XMLLIBS) $(ZLIBLIBS)
    else
	    @echo ""
	    @echo "make: WARNING: value of SYSTEM = $(SYSTEM) not recognised..."
	    @echo "               ...setting SOURCE_VARIANT to unknown"
    endif

lib: depends $(LIBOBJ)
    ifeq ($(KNOWN_SYSTEM), yes)
	    @echo ""
	    @echo "Linking object files making a library..."
	    $(AR) $(ARFLAGS) libtorus.a $(F90OBJ) $(CAPF90OBJLIB) $(INPUTSMODOBJ)
	    @echo "...done"
    else
	    @echo ""
	    @echo "make: WARNING: value of SYSTEM = $(SYSTEM) not recognised..."
	    @echo "               ...setting SOURCE_VARIANT to unknown"
    endif

preprocess:
	@echo ""
	@echo "====== Creating TORUS for $(SYSTEM) system ======"
	@echo ""
	@echo "BUILD OPTIONS: "
	@echo "OpenMP = $(openmp)"
	@echo "Debug  = $(debug)"


depends: preprocess
	@echo ""
	@echo "Making dependency file..."
	@touch -c make.depends
	@./makedepend
	@echo "...done"
	@echo ""

clean:
	@/bin/rm -f *.o *~ *.mod *.il *.pc *.d *.rnreg *.life *.cfg *.cse *.combine

distclean: clean
	@/bin/rm -f make.depends tags

ctags:
	@ctags --langmap='fortran:+(*.F90)' \
	       *.f90 *.f *.F90

svnver:
    ifeq ($(getsvnver), yes)
	    @echo "calling createsvnversion"
	    @./createsvnversion.csh 
	    @echo "...done"
    endif

torus_version_mod.o: svnver $(VERSIONSOURCE)

link: depends $(ALLOBJ)
    ifeq ($(KNOWN_SYSTEM), yes)
	    @echo ""
	    @echo "Linking object files and libraries..."
	    $(F90) $(TRACEFLAG) -o $(EXE) \
	    $(F90OBJ) $(CAPF90OBJ) \
	    $(CFITSIOLIBS) $(STARLIBS) $(F2CLIB) $(STATICLIBS)
	    @echo "...done"
    else
	    @echo ""
	    @echo "make: WARNING: value of SYSTEM = $(SYSTEM) not recognised..."
	    @echo "               ...setting SOURCE_VARIANT to unknown"
    endif
#
# General rules to make object files
#
.SUFFIXES:

%.o: %.f90
	$(F90) -c $<

%.o: %.F90
	$(F90) -c $<


#
# Auto-generated dependency file
#   - 'make depends' must be run before 'make' as the dependencies included
#     with the directive below are always one step behind the current version
#
-include make.depends

# DO NOT DELETE
