#! /bin/csh -f 

source $CASEROOT/env_conf || exit -1
source $CASEROOT/env_run  || exit -1
source $CASEROOT/env_mach.$MACH  || exit -1
source $CASEROOT/env_pes.$MACH  || exit -1

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

cd $objdir

# Filepath: List of source code directories (in order of importance).
#--------------------------------------------------------------------
\cat >! Filepath << EOF
$CASEROOT/SourceMods/src.share
$CASEROOT/SourceMods/src.cpl
$CODEROOT/cpl/cpl6
$SHAREROOT
$SHAREROOT/shr
$SHAREROOT/cpl
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_CPL > 1) set THREAD = TRUE
  gmake VPFILE=Filepath MODEL=cpl6 EXEC=$exedir/cpl \
             THREAD=$THREAD \
             -f $BLDROOT/Makefile MACFILE=$CASEROOT/Macros.$MACH deps  || exit 2
  gmake -j $GMAKE_J VPFILE=Filepath MODEL=cpl6 EXEC=$exedir/cpl \
             THREAD=$THREAD \
             -f $BLDROOT/Makefile MACFILE=$CASEROOT/Macros.$MACH   || exit 2
endif
                                         
# document the source code used, cleanup $objdir files
#--------------------------------------------------------------------

grep 'CVS' *.[hf]*                       

wait

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

exit 0
