#!/bin/csh -f

set arglist=""
set compare_wrf = 0
setenv WRF_EM_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" == "wrf" ) then
      set compare_wrf = 1
    endif
  endif
end

if( $compare_wrf != 1 ) then
  echo "You need to compile corresponding to your configure option."
  echo "Type <compile wrf> to compile WRFPLUS model only"

  exit 2
endif

setenv WRF_EM_CORE 1

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

  if ( ! (   $?WRF_EM_CORE  || $?WRF_NMM_CORE \
          ||  $?WRF_COAMPS_CORE || $?WRF_EXP_CORE )) then 
    echo 'None of WRF_EM_CORE, WRF_NMM_CORE, '
    echo '        specified in shell environment.... '
    setenv WRF_EM_CORE 1
    setenv WRF_NMM_CORE 0
    setenv WRF_COAMPS_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

  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 ) then
    if ( ! -f Registry/Registry ) then
      echo "Registry/Registry did not exist so overwriting"
      set overwrite=1
    else
      head -2 Registry/Registry | tail -1 | grep EM > /dev/null
      if ( $status ) then
        echo overwriting because Registry did not contain EM string
        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 ) then
           set overwrite=1
           echo overwriting because Registry.EM is newer than Registry
        endif
      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_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 "
  echo "."
  echo " "

  make $arglist A2DCASE="$A2DCASE"

endif

exit 0

hlp:

echo ' '
echo 'Usage:'
echo ' '
echo '   compile wrf           compile wrfplus in run dir (NOTE: wrfplus.exe, nupdown.exe, nup_standlone.exe generated)'
echo ' '
echo ' '
echo '  compile -h                 help message'


