#!/bin/csh -f

set arglist=""
set compare_wrf = 1
set compare_var = 0
set compare_be = 0
setenv WRF_EM_CORE 1
setenv WRF_DA_CORE 1

if ( !  -e configure.wrf  ) then
  echo "You need to configure the code first."
  echo "Please do configure and then compile."
  exit 1
endif

foreach a ( $argv )
  if ( "$a" == "-h" ) then
    goto hlp
  else
#we probably needs to add consistent check here.

    set arglist = ( $arglist $a )

    if ( "$a" == "var" || "$a" == "pure_var" || "$a" == "k2n" || "$a" == "n2k" ) then
      set compare_var = 1
    else if ( "$a" == "be" | "$a" == "be_wrf" | "$a" == "pure_be_wrf") then
      set compare_var = 1
      set compare_be = 1
    endif
  endif
end

if( $compare_wrf != 1 ) then
  echo "You need to compile corresponding to your configure option."
  echo "Type <compile var> to compile 3dvar (and WRF frame)"
  echo "Type <compile pure_var> to compile 3dvar only (assume WRF frame has compiled)"
  echo "Type <compile be> to compile gen_be stage 1 - 4"
  echo "Type <compile be_wrf> to compile gen_be stage 0 - 4"
  echo "Type <compile pure_be_wrf> to compile gen_be stage 0 - 4 only (assume WRF frame has compiled)"
  echo "Type <compile k2n> to compile convetor for converting KMA data to NetCDF data format"
  echo "Type <compile n2k> to compile convetor for converting NetCDF data to KMA data format"

  exit 2
endif


if($compare_be == 1) then
    setenv WRF_GEN_BE 1
else
    setenv WRF_GEN_BE 0
endif

if ( $arglist == "" ) then
  goto hlp
else
  unsetenv A2DCASE
  setenv A2DCASE `echo $arglist | grep 2d`

  if ( ! (   $?WRF_EM_CORE  || $?WRF_NMM_CORE || $?WRF_DA_CORE \
          ||  $?WRF_COAMPS_CORE || $?WRF_EXP_CORE )) then 
    echo 'None of WRF_EM_CORE, WRF_NMM_CORE, WRF_DA_CORE, '
    echo '        specified in shell environment.... '
    setenv WRF_EM_CORE 1
    setenv WRF_NMM_CORE 0
    setenv WRF_COAMPS_CORE 0
    setenv WRF_DA_CORE 0
    setenv WRF_EXP_CORE 0
  endif

# these settings get passed down through the environment in the
# calls to Make
  if ( ! $?WRF_EM_CORE )     setenv WRF_EM_CORE 0
  if ( ! $?WRF_NMM_CORE )    setenv WRF_NMM_CORE 0
  if ( ! $?WRF_COAMPS_CORE ) setenv WRF_COAMPS_CORE 0
  if ( ! $?WRF_EXP_CORE )    setenv WRF_EXP_CORE 0
  if ( ! $?WRF_DA_CORE )     setenv WRF_DA_CORE 0

  set overwrite=0
  if ($WRF_EM_CORE == 1 && WRF_NMM_CORE == 1 ) then
    echo "Cannot compile both EM and NMM cores in same executable yet."
    exit 2
  endif
  if ($WRF_EM_CORE == 0 && WRF_NMM_CORE == 0 ) then
    echo "Cannot compile because both EM and NMM cores are set to 0."
    exit 2
  endif
  if (($WRF_EM_CORE == 1) && ($WRF_DA_CORE != 1)) then
    if ( ! -f Registry/Registry ) then
      set overwrite=1
    else
      head -2 Registry/Registry | tail -1 | grep EM > /dev/null
      if ( $status ) then
        set overwrite=1
      else
        set em_time=`ls -1tr Registry | cat -n | grep -w 'Registry\.EM' | grep -v 'Registry.EM.' | awk '{print $1}'`
        set rg_time=`ls -1tr Registry | cat -n | grep -w 'Registry' | grep -v 'Registry.' | awk '{print $1}'`
        if ( $em_time > $rg_time ) set overwrite=1
      endif
    endif
    if ( $overwrite ) then
      echo copying Registry/Registry.EM to Registry/Registry
      echo '## WARNING: this file is autogenerated from Registry/Registry.EM. Changes may be lost' > Registry/Registry
      /bin/cat Registry/Registry.EM >> Registry/Registry
    endif
  endif
  if ($WRF_DA_CORE == 1)  then
    if ( ! -f Registry/Registry ) then
      set overwrite=1
    else
      head -2 Registry/Registry | tail -1 | grep 3DVAR > /dev/null
      if ( $status ) then
        set overwrite=1
      else
        set em_time=`ls -1tr Registry | cat -n | grep -w 'Registry\.3DVAR' | grep -v 'Registry.3DVAR' | awk '{print $1}'`
        set rg_time=`ls -1tr Registry | cat -n | grep -w 'Registry' | grep -v 'Registry.' | awk '{print $1}'`
        if ( $em_time > $rg_time ) set overwrite=1
      endif
    endif
    if ( $overwrite ) then
      echo copying Registry/Registry.3DVAR to Registry/Registry
      echo '## WARNING: this file is autogenerated from Registry/Registry.3DVAR. Changes may be lost' > Registry/Registry
      /bin/cat Registry/Registry.3DVAR >> Registry/Registry
    endif
  endif

  echo "WRF_GEN_BE = $WRF_GEN_BE"

  if ($WRF_GEN_BE == 1)  then
    if ( ! -f Registry/Registry ) then
      set overwrite=1
    else
      head -2 Registry/Registry | tail -1 | grep gen_be > /dev/null
      if ( $status ) then
        set overwrite=1
      else
        set em_time=`ls -1tr Registry | cat -n | grep -w 'Registry\.gen_be' | grep -v 'Registry.gen_be.' | awk '{print $1}'`
        set rg_time=`ls -1tr Registry | cat -n | grep -w 'Registry' | grep -v 'Registry.' | awk '{print $1}'`
        if ( $em_time > $rg_time ) set overwrite=1
      endif
    endif

    if ( $overwrite ) then
      echo copying Registry/Registry.gen_be to Registry/Registry
      echo '## WARNING: this file is autogenerated from Registry/Registry.gen_be. Changes may be lost' > Registry/Registry
      /bin/cat Registry/Registry.gen_be >> Registry/Registry
    endif
  endif

  if ($WRF_NMM_CORE == 1) then
    grep -q DM_PARALLEL configure.wrf
    if ( $status == 1 ) then
      echo NMM_CORE must be configured for DM parallel
      echo Please rerun the configure script and chose a DM parallel option
      exit 3
    endif
    if ( ! -f Registry/Registry ) then
      set overwrite=1
    else
      head -2 Registry/Registry | tail -1 | grep NMM > /dev/null
      if ( $status ) then
        set overwrite=1
      else
        set nmm_time=`ls -1tr Registry | cat -n | grep -w 'Registry\.NMM' | grep -v 'Registry.NMM.' | awk '{print $1}'`
        set rg_time=`ls -1tr Registry | cat -n | grep -w 'Registry' | grep -v 'Registry.' | awk '{print $1}'`
        if ( $nmm_time > $rg_time ) set overwrite=1
      endif
    endif
    if ( $overwrite ) then
      echo copying Registry/Registry.NMM to Registry/Registry
      echo '## WARNING: this file is autogenerated from Registry/Registry.NMM. Changes may be lost' > Registry/Registry
      /bin/cat Registry/Registry.NMM >> Registry/Registry
    endif
# integrity check for a kludge where a hard coded value in the 
# registry must match the same value in arch/preamble
    set registryvalue=`grep 'dimspec.* q ' Registry/Registry.NMM | sed -e 's/..*constant=//' -e 's/ ..*$//'`
    set preamblevalue=`grep 'DNMM_MAX_DIM=.*' arch/preamble | sed -e 's/..*-DNMM_MAX_DIM=//' -e 's/ ..*$//'`
    if ( $registryvalue != $preamblevalue ) then
      echo "Harded coded value of dimspec q in Registry ($registryvalue) does not"
      echo "equal the hard coded value of NMM_MAX_DIM in arch/preamble ($preamblevalue)"
      echo "Please fix and try again."
      exit 2
    endif
  endif

  echo " "
  echo -n "**** Compiling: "
  if ( $WRF_EM_CORE ) echo -n "WRF_EM_CORE "
  if ( $WRF_NMM_CORE ) echo -n "WRF_NMM_CORE "
  if ( $WRF_COAMPS_CORE ) echo -n "WRF_COAMPS_CORE "
  if ( $WRF_EXP_CORE ) echo -n "WRF_EXP_CORE "
  if ( $WRF_DA_CORE ) echo -n "WRF_DA_CORE "
  echo "."
  echo " "

  make $arglist A2DCASE="$A2DCASE"

endif

exit 0

hlp:

echo ' '
echo 'Usage:'
echo ' '
echo '   compile var'
echo ' '
echo '  compile -h                 help message'

