#! /bin/csh -f

setenv MACH phoenix

source env_conf         || "problem sourcing env_conf"    && exit -1  
source env_run          || "problem sourcing env_run"     && exit -1  
source env_mach.$MACH   || "problem sourcing env_mach.$MACH" && exit -1
source env_pes.$MACH    || "problem sourcing env_pes.$MACH" && exit -1
source /spin/home/hof/CCSM/ccsm3_1_bgcmip01/scripts/i01.02cn/Tools/env_setcomp  || "problem sourcing env_setcomp"   && exit -1

echo -------------------------------------------------------------------------
echo " Preparing $GRID component models for execution "
echo -------------------------------------------------------------------------

if !($?LID) setenv LID "`date +%y%m%d-%H%M%S`"   

# -------------------------------------------------------------------------
echo " - Create execution directories for atm,cpl,lnd,ice,ocn"
# -------------------------------------------------------------------------

foreach DIR ( $EXEROOT $LIBROOT $INCROOT $OBJROOT)
  if !(-d $DIR) mkdir -p $DIR || "cannot make $DIR" && exit -1
end
rm -rf  $EXEROOT/all; mkdir -p $EXEROOT/all  || "cannot make $EXEROOT/all" && exit -1

# -------------------------------------------------------------------------
echo " - If a restart run then copy restart files into executable directory "
# -------------------------------------------------------------------------

if ($MACH != 'moon') then
  $UTILROOT/Tools/ccsm_getrestart
endif

# -------------------------------------------------------------------------
echo " - Check validity of configuration"
# -------------------------------------------------------------------------

set reconfig = FALSE
cmp -s $CASEROOT/env_conf $CASEROOT/.cache/env_conf.cache || set reconfig = TRUE
if ($reconfig == 'TRUE') then
    echo "ERROR - env_conf has changed since initial configuration"
    echo "must invoke the following:"
    echo "   configure -cleanall"
    echo "   configure -mach $MACH"
    echo "Warning: this will delete Buildexe, Buildlib, Buildnml_Prestage"
    exit -1  
endif

set reconfig = FALSE
cmp -s $CASEROOT/env_pes.$MACH $CASEROOT/.cache/env_pes.$MACH.cache || set reconfig = TRUE
if ($reconfig == 'TRUE') then
    echo "ERROR - tasks and/or threads and/or component layout has changed since initial configuration"
    echo "must invoke the following:"
    echo "   configure -cleanmach $MACH"
    echo "   configure -mach $MACH "
    exit -1  
endif

# -------------------------------------------------------------------------
echo " - Determine if build must happen (env variable BLDTYPE)"
# -------------------------------------------------------------------------

if ($BLDTYPE != 'TRUE' && $BLDTYPE != 'FALSE') then
  echo "error in BLDTYPE: $BLDTYPE"
  exit 1
endif
echo " - Build flag (BLDTYPE) is $BLDTYPE"

# -------------------------------------------------------------------------
echo " - Build Libraries: mph, mct"
# -------------------------------------------------------------------------

cd $EXEROOT

foreach lib (mph mct)
   set exedir = $EXEROOT/$lib; if !(-d $exedir) mkdir -p $exedir
   set file = $lib.buildlib.$LID
   cd $exedir
   echo `date` $file | tee $file
   $CASEROOT/Buildlib/$lib.buildlib >>& $file
   if ($status != 0) then
     echo  ERROR: $lib.buildlib failed, see ${file}
     echo  ERROR: cat $cwd/${file}
     exit  99
   endif
end

# -------------------------------------------------------------------------
echo " - Create model directories for each platform"
# -------------------------------------------------------------------------

if   (-e $EXEROOT/all) rm -rf   $EXEROOT/all 
mkdir -p $EXEROOT/all

# -------------------------------------------------------------------------
echo " - Determine if models must be rebuilt"
# -------------------------------------------------------------------------

if !(-f $CASEROOT/Buildexe/Macros.$OS) then
   cp $BLDROOT/Macros.$OS  $CASEROOT/Buildexe/Macros.$OS.cache
endif
set rebuild = FALSE
cmp -s $BLDROOT/Macros.$OS $CASEROOT/Buildexe/Macros.$OS.cache || set rebuild = TRUE
if (($BLDTYPE == 'FALSE') && ($rebuild == 'TRUE')) then
  echo "bldtype is $BLDTYPE and rebuild is $rebuild"
  echo "Macros.$OS  files differ - must rebuild the ccsm models" 
  echo "invoke script $UTILROOT/Tools/cleanfiles before rebuilding"
  exit -1
endif
if (-f $CASEROOT/Buildexe/Macros.$OS.cache) rm $CASEROOT/Buildexe/Macros.$OS.cache

# -------------------------------------------------------------------------
echo " - Build model executables, create namelist files, prestage input data"
# -------------------------------------------------------------------------

foreach n (1 2 3 4 5)

  #--- Set component specific variables

  set model = $MODELS[$n]
  set comp =  $COMPONENTS[$n]
  set nthrd = $NTHRDS[$n]          

  #--- Make necessary directories

  set objdir = $OBJROOT/$model/obj ; if !(-d $objdir) mkdir -p $objdir
  set exedir = $EXEROOT/$model     ; if !(-d $exedir) mkdir -p $exedir

  #--- Go into executable directory

  cd   $exedir
  set file_prestage = $model.log.$LID
  set file_build = $model.buildexe.$LID
  echo `date` $exedir/${file_prestage} | tee ${file_prestage}
  echo `date` $exedir/${file_build} | tee ${file_build}

  #--- Build Namelists/Prestage component input data

  $CASEROOT/Buildnml_Prestage/$comp.buildnml_prestage.csh >>& ${file_prestage}
  if ($status != 0) then
    echo  ERROR: $comp.buildnml_prestage.csh failed, see ${file_prestage}
    echo  ERROR: cat $cwd/${file_prestage}
    exit  99
  endif

  #--- Build Model Executables (save the latest executables to the active exe.tar)

  $CASEROOT/Buildexe/$comp.buildexe.csh  >>& ${file_build}
  if ($status != 0) then
    echo  ERROR: $comp.buildexe.csh failed, see ${file_build}
    echo  ERROR: cat $cwd/${file_build}
    exit  99
  endif
  if ($BLDTYPE == 'TRUE') then
     if ($n == 1) then
       tar -cf $EXEROOT/$CASE.exe.$LID.tar $comp
     else
       tar -rf $EXEROOT/$CASE.exe.$LID.tar $comp
     endif
     if ($n == 5) then  
       cmp -s $EXEROOT/$CASE.exe.tar $EXEROOT/$CASE.exe.$LID.tar
       if ($status == 0) then
         rm -f $EXEROOT/$CASE.exe.$LID.tar
       else
         cp $EXEROOT/$CASE.exe.$LID.tar $EXEROOT/$CASE.exe.tar
       endif
     endif
  endif

end

# -------------------------------------------------------------------------
echo " - Create MPH input file and link into all model dirs"
# -------------------------------------------------------------------------

cd $EXEROOT/all
set MPHFILE = mph_processors_map.in
cat >! $MPHFILE <<EOF
PROCESSORS_MAP
BEGIN
atm
ice
lnd
ocn
cpl
END
EOF
foreach n (1 2 3 4 5)
   set exedir = $EXEROOT/$MODELS[$n]
   rm -f  $exedir/$MPHFILE  
   ln -s  $EXEROOT/all/$MPHFILE   $exedir
end

# -------------------------------------------------------------------------
echo " - Create shr_msg_stdio chdir/stdin/stdout data file"
# -------------------------------------------------------------------------

foreach n (1 2 3 4 5)
   set model      = $MODELS[$n]
   set exedir     = $EXEROOT/$model
   set rundir     = $exedir
   if ($MACH == 'moon') set rundir = $RUNROOT/$model
   set stdiofile  = ${model}_stdio.nml
   set stdinfile  = ${model}.stdin
   set stdoutfile = ${model}.log.$LID
   cat >! $stdiofile << EOF
&stdio
   dir    = "$rundir    "
   stdin  = "$stdinfile "
   stdout = "$stdoutfile   "
   /
EOF
   rm -f  $exedir/$stdiofile
   ln -s  $EXEROOT/all/$stdiofile   $exedir
end

# -------------------------------------------------------------------------
# Save model output stdout and stderr 
# -------------------------------------------------------------------------

if ($LOGDIR != "") then
  cd $EXEROOT
  gzip */*.buildexe*$LID
  if (! -d $LOGDIR) mkdir -p $LOGDIR
  if (! -d $LOGDIR/bld) mkdir -p $LOGDIR/bld
  cp -p */*buildexe*$LID.* $LOGDIR/bld || echo "Error in copy of build logs " && exit -1
endif

echo "-------------------------------------------------------------------------"
echo " - CCSM BUILD HAS FINISHED SUCCESSFULLY                                  "
echo "-------------------------------------------------------------------------"
