#! /bin/csh -f 

source $CASEROOT/env_conf || exit -1
source $CASEROOT/env_run  || exit -1
source $CASEROOT/env_mach.$MACH  || exit -1
source /spin/home/hof/CCSM/ccsm3_1_bgcmip01/scripts/i01.04casa/env_pes.phoenix || exit -1 

set exedir = $EXEROOT/ocn
set objdir = $OBJROOT/ocn/obj

cd $objdir

# Filepath: List of source code directories (in order of importance).
#--------------------------------------------------------------------
cat >! Filepath << EOF
$CASEROOT/SourceMods/src.share
$CASEROOT/SourceMods/src.xocn
/spin/home/hof/CCSM/ccsm3_1_bgcmip01/models/dead/dead6
/spin/home/hof/CCSM/ccsm3_1_bgcmip01/models/csm_share
/spin/home/hof/CCSM/ccsm3_1_bgcmip01/models/csm_share/shr
/spin/home/hof/CCSM/ccsm3_1_bgcmip01/models/csm_share/cpl
EOF

# preproc.h
#--------------------------------------------------------------------
\cat >! preproc.h << EOF
#ifndef PREPROC_SET
#define PREPROC_SET
#define MODELNUMBER 4
#endif
EOF

# run make
#--------------------------------------------------------------------

if ($BLDTYPE == 'TRUE') then
if ($OS == 'UNICOS') then
  if (`uname` == 'Linux') then
    # cross compiler needs to run dependency program locally
    module purge
    gcc -o makdep $BLDROOT/makdep.c || exit 2
    source $CASEROOT/env_mach.$MACH  || exit -1
  else
    # makes the build run faster on the Cray X1E
    cc -h command -o makdep $BLDROOT/makdep.c || exit 2
  endif
else
  cc -o makdep $BLDROOT/makdep.c || exit 2
endif
  set THREAD = FALSE ;  if ($NTHRDS_OCN > 1) set THREAD = TRUE
  gmake              VPFILE=Filepath MODEL=xocn EXEC=$exedir/xocn \
             THREAD=$THREAD \
             -f  $BLDROOT/Makefile MACFILE=$CASEROOT/Macros.$MACH deps   || exit 2
  gmake -j $GMAKE_J VPFILE=Filepath MODEL=xocn EXEC=$exedir/xocn \
             THREAD=$THREAD \
             -f  $BLDROOT/Makefile MACFILE=$CASEROOT/Macros.$MACH    || exit 2
endif

if !(-f $exedir/xocn) then
  echo "ERROR: xocn executable not available, check SETBLD"
  exit -1
endif

