#!/bin/csh -f

setenv START_OF_COMPILE "`date`"

cont1:

if ( !  -e configure.gen_be  ) then
   if ( $1 == "all_wrfvar" ) then
      ./configure wrfda
      set dontask
   else
      ./configure $1
      set dontask
   endif
endif


if ( ! $?dontask && $?prompt ) then
  echo "This script assumes you have configured the code already."
  echo "You only need to configure once."
  echo "If you wish to reconfigure, type c at the prompt below"
  echo " "
  echo "Ready to compile? [ync]"
  set resp=$<

  if ( "$resp" == "c" ) then
    ./configure
    goto cont1
  endif

  if ( "$resp" == "n" ) then
    exit 2
  endif
endif

set arglist=""
foreach a ( $argv )
  if ( "$a" == "-h" ) then
    goto hlp
  else
    if ( "$a" != "-d" ) then
      set arglist = ( $arglist $a )
    endif
    if ( "$a" == "all_wrfvar" ) then
      grep "DA_CORE=1" configure.wrf > /dev/null
      if ( ! $status ) then 
         # If configuration file has DA_CORE=1 hardwired, ok to set WRF_DA_CORE to 1
         setenv WRF_DA_CORE 1
      endif
    endif
    if ( "$a" == "nmm_real" ) then
      grep "NMM_CORE=1" configure.wrf > /dev/null
      if ( ! $status ) then 
         # If configuration file has NMM_CORE=1 hardwired, ok to set WRF_NMM_CORE to 1
         if ( ! $?WRF_NMM_CORE ) setenv WRF_NMM_CORE 1
      endif
    endif
    if ( "$a" == "-j" ) then
      shift argv
      setenv J "-j $argv[1]"
    endif
  endif
end

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

# these settings get passed down through the environment in the
# calls to Make
  if ( ! $?WRF_DA_CORE )     setenv WRF_DA_CORE 0
  if ( ! $?WRF_EM_CORE )     setenv WRF_EM_CORE 0
  if ( ! $?WRF_NMM_CORE )    setenv WRF_NMM_CORE 0
  if ( ! $?WRF_NMM_NEST )    setenv WRF_NMM_NEST 0
  if ( ! $?WRF_COAMPS_CORE ) setenv WRF_COAMPS_CORE 0
  if ( ! $?WRF_EXP_CORE )    setenv WRF_EXP_CORE 0
  if ( ! $?WRF_CHEM )        setenv WRF_CHEM 0
  if ( ! $?WRF_DFI_RADAR )   setenv WRF_DFI_RADAR 0
  if ( ! $?HWRF )            setenv HWRF 0
  if ( ! $?WRF_CONVERT ) then
     if ( "$arglist" == "convert_em" ) then
       setenv WRF_CONVERT 1
       setenv WRF_EM_CORE 0
     else
       setenv WRF_CONVERT 0
     endif
  endif

  if ( ! $?DA_ARCHFLAGS )     setenv DA_ARCHFLAGS ""

  if ( ( $WRF_DA_CORE == 1 )  && ( ! -d var ) ) then
    echo "  "
    echo "You need to download and untar the Var code, or"
    echo "unset the WRF_DA_CORE env variable."
    echo "  "
    exit
  endif

  set overwrite=0

  echo " "
  echo -n "Compiling: "
  if ( $WRF_DA_CORE ) echo -n "WRF_DA_CORE "
  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 " "

  if ( ! $?GEN_BE_SRC_ROOT_DIR ) setenv GEN_BE_SRC_ROOT_DIR `pwd`

# new dec 2009.  check to see if make supports parallel -j option
  make -j 2 >& /dev/null
  if ( $status == 0 ) then              # Parallel make ok
    if ( ! $?J ) then                   # J not defined
      echo setting parallel make -j 2   # Set default to 2
      setenv J "-j 2"
    else
#     J is defined, check that it is a correctly formed variable
      set first2chars = `echo $J | cut -c 1-2`         # Are 1st two chars are -j?
      set second_word = `echo $J | cut -d" " -f2`      # Is second word a number?
      if    ( "$first2chars" == "-j" ) then
         if ( ( "$second_word" >= "2"  )  && \
              ( "$second_word" <= "20" ) ) then
            echo setting parallel make $J
         else if ( "$second_word" == "1" ) then
            echo setting serial make $J
         else
            echo "badly formed -j option for parallel make: $J"
            echo "or you set the number of processors above 20 "
            echo setting parallel make -j 2            # Set default to 2
            setenv J "-j 2"
         endif
      else
         if ( "$J" == "" ) then     # J blank is OK
            echo setting serial make $J
         else
            echo "parallel option for make is -j, you entered: $first2chars"
            echo setting parallel make -j 2               # Set default to 2
            setenv J "-j 2"
         endif
      endif
    endif
  else
    echo not setting parallel make
  endif

  make $arglist A2DCASE="$A2DCASE" GEN_BE_SRC_ROOT_DIR="$GEN_BE_SRC_ROOT_DIR"

endif

exit 0

hlp:

echo ' '
echo 'Usage:'
echo ' '
echo '   compile [-d] [-j n] gen_be   compile gen_be in src dir '
echo ' '
foreach d ( `/bin/ls test` )
  if ( "$d" != "CVS" ) then
    echo "      compile $d"
  endif
end
echo ' '
echo '  compile -d                 compile without optimization and with debugging'
echo '  compile -j n               parallel make using n tasks if supported (default 2)'
echo '  compile -h                 help message'


