# Build the land_atlantes library, derived from land_lad

# check if USE_LPJ is set in cppDefs. if so, build with different name
if ( "$cppDefs" =~ "*USE_LPJ*" ) then
  set lib_name = "lib_land_atlantes+lpj"
else
  set lib_name = "lib_land_atlantes"
# for now, we want CLIMBER2_SNOWAGE only without LPJ
  set cppDefs = ( $cppDefs -DPIK_COUPLER_COSZEN_TO_LAND -DUSE_CLIMBER2_SNOWAGE )
endif

# land_model.F90 must be compiled without vectorization!
# ifort 15.0.3.187 apparently has a bug.
# With ifort 17.0.1.132 land_model.F90 can be compiled with vectorization.
set pathnames = $executable:h:h/$lib_name/land_model_command-$$

set srcList = ( land_param land_atlantes/vegetation land_atlantes/soil land_atlantes/numerics.F90 land_atlantes/land_types.F90 $pathnames land_atlantes/lpj_driver.F90 atmos_aeolus/fmsclocks.F90 )


mkdir -p $executable:h:h/$lib_name

if ( `ifort --version` =~ 15.0.3 ) then
    echo compiling land_model.F90 without vectorization because of compiler bug
    echo '$(FC) -no-vec $(CPPDEFS) $(CPPFLAGS) $(FPPFLAGS) $(FFLAGS) $(OTHERFLAGS) -no-vec -c land_atlantes/land_model.F90' > $pathnames
else
    echo land_atlantes/land_model.F90 > $pathnames
endif

cd $executable:h:h/$lib_name
$mkmf_lib -p $lib_name.a -c "$cppDefs"  -o "-I$executable:h:h/lib_FMS" $srcList $lib_include_dirs
rm -f $pathnames
make

if( $status ) then
    echo "Make failed to create $lib_name.a"
    exit 1
endif
