#!/bin/bash

# NOTE: this default version of makefile_header
# assumes you will be using the mesasdk.
# if you do not want that, you can use the
# makefile_header_non_mesasdk file as a template.


# NOTE: MESA is no longer supported on 32-bit systems.  This isn't by design;
# we simply haven't been able to get it to work!  So if you really-really want 
# to use an antique 32-bit system for mesa, you'll have to try to make it work yourself.
# But I think you'd be better off switching to a 64-bit machine.


# step 1) specify your compilers

# NOTE: recent versions of mesa have required reduced optimization 
# in order to work with ifort; we suggest using gfortran instead of ifort if possible.

FC = gfortran
CC = gcc


# step 2) specify whether isnan is supported or not

UTILS_ISNAN = utils_isnan_okay


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

WHICH_LAPACK =
WHICH_BLAS =
LOAD_LAPACK = `mesasdk_lapack_link`
LOAD_BLAS = `mesasdk_blas_link`


# step 4) do you want to use PGPLOT with mesa/star?                                                                                     

USE_PGSTAR = YES
LOAD_PGPLOT = `mesasdk_pgplot_link`


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

USE_SE = NO
#USE_SE = YES
LOAD_SE = `mesasdk_se_link`
INCLUDE_SE = -I${MESASDK_ROOT}/include


# step 6) do you want to use GYRE?

# if you don't plan to use GYRE and want to skip building it, then
#USE_GYRE = NO
# if you want to have GYRE built along with the rest of mesa, then
USE_GYRE = YES


# step 7) now do the mesa install

USE_OPENMP = YES

USE_STUB_FOR_CRLIBM = NO

#################################################################

# 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.

FCbasic = -fno-range-check -fmax-errors=100 $(SPECIAL_FC_FLAGS) -fprotect-parens -fno-sign-zero
# -Warray-temporaries
 
FCimpno = -fimplicit-none  
FCchecks = -fbounds-check -Wuninitialized -Warray-bounds
FCwarn = -Wunused-value -Werror -W -Wno-compare-reals -Wno-unused-parameter
# -Wno-unused-parameter
 
# note: -Werror causes warnings to be treated as errors
FC_fixed_preprocess = -x f77-cpp-input
FC_free_preprocess = -x f95-cpp-input
FCfixed = -ffixed-form -ffixed-line-length-132 $(FC_fixed_preprocess)
FCfixed72 = -ffixed-form $(FC_fixed_preprocess)
FCfree = -ffree-form $(FC_free_preprocess)
FCopt = -O2
FCdebug = -g
FCstatic =
ifeq ($(USE_OPENMP),YES)
FCopenmp = -fopenmp
else
FCopenmp = 
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 test makefiles and client makefiles

WORK_COMPILE = \
   $(FC) $(FCbasic) $(FCopenmp) $(FCchecks) $(FCdebug) \
   $(FCfree) -I$(MESA_INCLUDE_DIR) $(INCLUDE_SE) -c
 
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)

ifeq ($(USE_STUB_FOR_CRLIBM),NO)
LOAD_CRLIBM = -lcrlibm
endif

LOAD_MATRIX = -lf2crlibm $(LOAD_CRLIBM) -lmtx -lconst -lutils -lmesaklu $(LOAD_LAPACK) $(LOAD_BLAS)

LOAD_MESA_NUMERICS = -linterp_2d -linterp_1d -lnum $(LOAD_MATRIX)

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

# macro uses micro
LOAD_MESA_MACRO = \
   -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)
STAR_LOAD_LIST = $(LOAD_STAR_MODS) $(LOAD_PGPLOT) $(LOAD_SE)
LOAD_MESA_STAR = -L$(MESA_LIB_DIR) $(STAR_LOAD_LIST)
LOAD_MESA_BINARY = -L$(MESA_LIB_DIR) -lbinary $(STAR_LOAD_LIST)

# LOADER creates executables
LOADER = $(FC)

# LIB_TOOL creates libraries
LIB_TOOL = ar crs


STAR_WORK_OBJS = \
   run_star_extras.o run_star.o 

BINARY_WORK_OBJS = \
   run_star_extras.o run_binary_extras.o run_binary.o

#Test if we are on windows
BUILD_ON_WINDOWS=NO
ifeq ($(OS),Windows_NT)
   BUILD_ON_WINDOWS=YES
endif