#!/bin/bash

# step 1) specify your compilers
CC = gcc

FC = ifort
# NOTE: mesa needs version 11.1 or higher of ifort.

#FC = gfortran
# NOTE: mesa needs version 4.5.0 or higher of gfortran.

# if you need special flags for the compiler, define them here:
SPECIAL_FC_FLAGS = 
CC = gcc

# step 2) specify whether isnan is supported or not

# pick one of the following options:

UTILS_ISNAN = utils_isnan_okay
#UTILS_ISNAN = utils_isnan_nope

# If you aren't sure, try setting UTILS_ISNAN = utils_isnan_okay
# If this works, great.  Otherwise, the compilation of the
# mesa/utils module will complain when it tries to invoke isnan.
# In case that happens, you can simply change this makefile_header
# to the UTILS_ISNAN = utils_isnan_nope setting and redo the
# ./install command for mesa.


# step 3) specify which BLAS and LAPACK libraries to use for mesa/mtx

# these are the standard defaults
WHICH_LAPACK = USE_SRCS
LOAD_LAPACK = -lmesalapack
WHICH_BLAS = USE_SRCS
LOAD_BLAS = -lmesablas
MKL_INCLUDE = 

# step 4) do you want to use PGPLOT with mesa/star?                                                                                     
# you can have onscreen plots updated at each step,                                                                                     
# and you can save files for making movies.  very cool!                                                                                 

# NOTE: you need to have a PGPLOT built using the same fortran and c compiler as you                                                    
# are using for mesa.                                                                                                                   

# if you don't already have a compatible pgplot,                                                                                         
# follow the instructions in pgplot/README                                                                                               

# to access pgplot from mesa/star,                                                                                                       
# set USE_PGSTAR = YES and set LOAD_PGPLOT to load the libraries.                                                                       
# in mesa/star, set the control parameter pgstar_flag = .true. in &star_job                                                             
# when you run star it will open a window using X and draw plots using PGSTAR                                                           
# the plots will be updated each time a log entry is made.                                                                               
# see star/test/src/pgstar_defaults.dek for details.                                                                                     

# NOTE: if you have libpng, then include '-lpng' in the LOAD_PGPLOT line.                                                               
# the *.png files are excellent for making movies.                                                                                       
# but if you don't have libpng and can't get it, no problem; it isn't required.                                                         
# just remove '-lpng' from the LOAD_PGPLOT line.                                                                                         

# Similarly, if you don't have X windows and don't want to make onscreen plots,                                                         
# then you can load PGPLOT without it.  Just delete the X stuff from LOAD_PGPLOT.                                                       

# for users of Red Hat OS: Georgios Magkotsios reports success with these settings:                                                     
#LOAD_PGPLOT = -L${PGPLOT_DIR} -lpgplot -L/usr/lib64 -lX11 -lsvml -lpng                                                                 
#FCstatic = -shared-intel #-Bstatic -static-intel                                                                                       

# for Ubuntu, Philip Pinto found these libraries to be necessary:                                                                       
#LOAD_PGPLOT = -L${PGPLOT_DIR} -lpgplot  -lX11 -lxcb -lXau -lXdmcp -lXext -lpng -lz                                                    

# for Fedora 12 (x86_64), Mike Zingale reported that the following worked                                                               
# 1. at the terminal:                                                                                                                   
#  yum install pgplot pgplot-devel                                                                                                       
# 2. in the mesa/util/makefile_header                                                                                                   
# USE_PGSTAR = YES                                                                                                                       
# LOAD_PGPLOT = -lpgplot -L/usr/lib64 -lX11 -lpng                                                                                       
# 3. it is also important that the following environment variable be set:                                                               
# export PGPLOT_DIR=/usr/share/pgplot/  


# NOTE: a change in USE_PGSTAR doesn't take effect until you next recompile mesa/star


ifeq ($(FC),ifort)

#USE_PGSTAR = NO
#LOAD_PGPLOT = 

USE_PGSTAR = YES
LOAD_PGPLOT = -L/Users/bpaxton/mesa/utils/pgplot -lpgplot -L/usr/X11R6/lib -lX11 -lpng

else
ifeq ($(FC),gfortran)

#USE_PGSTAR = NO
#LOAD_PGPLOT = 
USE_PGSTAR = YES
LOAD_PGPLOT = -L/Users/bpaxton/mesa/utils/pgplot_gfortran -lpgplot -L/usr/X11R6/lib -lX11 -lpng

else

USE_PGSTAR = NO
LOAD_PGPLOT = 

endif
endif



# step 5) do you want to use SE with mesa/star?   (ask Falk about it)

ifeq ($(FC),ifort)

USE_SE = NO
LOAD_SE = 
INCLUDE_SE = 

#USE_SE = YES
#SEHOME=/rpod2/opt/helix_falk/se
#LOAD_SE = -Wl,-rpath=$(SEHOME)/lib -L$(SEHOME)/lib -lse
#INCLUDE_SE = -I$(SEHOME)/include

else
ifeq ($(FC),gfortran)

USE_SE = NO
LOAD_SE = 
INCLUDE_SE = 

#USE_SE = YES
#LOAD_SE =  -L/opt/se-1.2_gfortran/lib  -lse
#INCLUDE_SE = -I/opt/se-1.2_gfortran/include

else

USE_SE = NO
LOAD_SE = 
INCLUDE_SE = 

endif
endif


# step 6) now do the mesa install

# do install at the top level mesa directory

# NOTE: do NOT run the install scripts at the subdirectory level
# those are for use by the top level script

# Unless you are adding a new compiler to the list of options,
# you shouldn't need to change any of the following stuff.
# It simply defines things for use by the module makefiles.


ifeq ($(FC),ifort)

FCbasic = $(SPECIAL_FC_FLAGS) -vec-report0 -traceback -error-limit 6
FCimpno = -implicitnone 
FCchecks = -check uninit -check pointers -check bounds -check all
FCwarn = -warn all -warn nounused
FCwarn_unused = -warn unused
FCfixed = -fixed -132
FCfixed72 = -fixed
FCfree = -free
#FCopt = -O3
FCopt = -O2
FCdebug = -g
FCopenmp = -openmp -threads
#FCopenmp = 
FC_fixed_preprocess = -fpp
FC_free_preprocess = -fpp
FCstatic =
#FCstatic = -Bstatic -static-intel
# BTW: -Bstatic -static-intel makes it possible to use the executable stand-alone

else
ifeq ($(FC),gfortran)

FCbasic = -fno-range-check $(SPECIAL_FC_FLAGS)
FCimpno = -fimplicit-none  
FCchecks = -fbounds-check
FCwarn = -Wunused-value -Werror -W
FCfixed = -ffixed-form -ffixed-line-length-132
FCfixed72 = -ffixed-form
FCfree = -ffree-form
FCopt = -O2
FCdebug = -g
FCstatic =

FCopenmp = -fopenmp
#FCopenmp = 
FC_fixed_preprocess = -x f77-cpp-input
FC_free_preprocess = -x f95-cpp-input

else

FCbasic = UNKNOWN COMPILER
FCchecks =
FCwarn = 
FCfixed =
FCfree =
FCopt = 
FCdebug = 
FCopenmp = 

endif
endif

# some definitions used in the module makefiles
MODULE_DIR = ..
MOD_PUBLIC_DIR = $(MODULE_DIR)/public
MOD_PRIVATE_DIR = $(MODULE_DIR)/private
MODULE_INCLUDES = -I$(MOD_PUBLIC_DIR) -I$(MOD_PRIVATE_DIR)
OTHER_INCLUDES = -I$(MESA_DIR)/include
INCLUDES = $(MODULE_INCLUDES) $(OTHER_INCLUDES)

COMPILE_BASIC_FLAGS = $(FCbasic) $(FCopenmp) $(FCstatic) $(INCLUDES)
COMPILE_BASIC = $(FC) $(COMPILE_BASIC_FLAGS)

COMPILE_TO_TEST   = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCopt) $(FCdebug) -c
COMPILE_TO_DEPLOY = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCopt) -c

COMPILE_NO_OPENMP_NO_OPT = \
   $(FC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_ASAP = \
   $(FC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCdebug) -c -O

COMPILE_FAST = $(COMPILE_BASIC) -c
COMPILE_NO_CHECKS = $(COMPILE_BASIC) $(FCopt) -c
COMPILE_NO_OPT    = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_DEVEL     = $(COMPILE_NO_OPT)


# some definitions used in the module makefiles
MODULE_DIR = ..
MOD_PUBLIC_DIR = $(MODULE_DIR)/public
MOD_PRIVATE_DIR = $(MODULE_DIR)/private
MODULE_INCLUDES = -I$(MOD_PUBLIC_DIR) -I$(MOD_PRIVATE_DIR)
OTHER_INCLUDES = -I$(MESA_DIR)/include
INCLUDES = $(MODULE_INCLUDES) $(OTHER_INCLUDES)

COMPILE_BASIC_FLAGS = $(FCbasic) $(FCopenmp) $(FCstatic) $(INCLUDES)
COMPILE_BASIC = $(FC) $(COMPILE_BASIC_FLAGS)

COMPILE_TO_TEST   = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCopt) $(FCdebug) -c
COMPILE_TO_DEPLOY = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCopt) -c

COMPILE_NO_OPENMP_NO_OPT = \
   $(FC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_ASAP = \
   $(FC) $(FCbasic) $(INCLUDES) $(FCwarn) $(FCimpno) $(FCdebug) -c -O

COMPILE_FAST = $(COMPILE_BASIC) -c
COMPILE_NO_CHECKS = $(COMPILE_BASIC) $(FCopt) -c
COMPILE_NO_OPT    = $(COMPILE_BASIC) $(FCwarn) $(FCimpno) $(FCchecks) $(FCdebug) -c -O
COMPILE_DEVEL     = $(COMPILE_NO_OPT)

# some definitions used in the test makefiles and client makefiles
TEST_DIR = ..
TEST_SRC_DIR = $(TEST_DIR)/src
PACKAGE_DIR = ../..
LOCAL_LIB_DIR = $(PACKAGE_DIR)/make
MESA_LIB_DIR = $(MESA_DIR)/lib
MESA_INCLUDE_DIR = $(MESA_DIR)/include
TEST_INCLUDES = -I$(LOCAL_LIB_DIR) -I$(PACKAGE_DIR)/public -I$(MESA_INCLUDE_DIR) $(INCLUDE_SE)
TEST_COMPILE_FLAGS = $(FCbasic) $(FCopenmp) $(TEST_INCLUDES) $(FCchecks) $(FCdebug) -c
TEST_COMPILE = $(FC) $(TEST_COMPILE_FLAGS)

LOAD_MATRIX = -lmtx -lmesaklu $(LOAD_LAPACK) $(LOAD_BLAS)
LOAD_MESA_NUMERICS = -linterp_2d -linterp_1d -lnum -lutils -lalert -lconst $(LOAD_MATRIX)

# micro uses numerics
LOAD_MESA_MICRO = -leos -lkap \
   -lnet -lscreen -lrates -lreaclib -lweak -lneu -lchem $(LOAD_MESA_NUMERICS)

# macro uses micro
LOAD_MESA_MACRO = \
   -ldiffusion -lionization -latm -lcolors -lmlt $(LOAD_MESA_MICRO)

# star_support uses macro (for now, LOAD_MESA_STAR_SUPPORT just = LOAD_MESA_MACRO)
LOAD_MESA_STAR_SUPPORT = $(LOAD_MESA_MACRO)

# star uses star_support
LOAD_STAR_MODS = -lstar $(LOAD_MESA_STAR_SUPPORT)

# this does the -L for mesa star
LOAD_MESA_STAR = -L$(MESA_LIB_DIR) $(LOAD_STAR_MODS) $(LOAD_PGPLOT) $(LOAD_SE)

# LOADER creates executables
ifeq ($(USE_MPI),YES)
   LOADER = mpif77
else
   LOADER = $(FC)
endif

# LIB_TOOL creates libraries
LIB_TOOL = ar crs


STAR_WORK_OBJS = \
   run_star_extras.o run_star.o 
