#!/bin/ksh

export DA_CORE=0
export EM_CORE=0
export NMM_CORE=0
export COAMPS_CORE=0
export EXP_CORE=0
export HDF4=${HDF4:-}
export HDF5=${HDF5:-}
export HDFEOS=${HDFEOS:-}  
export ZLIB=${ZLIB:-}  
export JPEG=${JPEG:-}  
export JASPER=${JASPER:-}  
export compileflags=""
export MAX_DOMAINS=21
export RWORDSIZE=${RWORDSIZE:-'$(NATIVE_RWORDSIZE)'}
export COREFLAGS="-DNEW_BDYS"

option=$1

# Change default options if not configuring a wrfvar target
# (wrfvar, be, all_wrfvar)

if [ "$option" = "em" ] ; then
    SOLVER=em
    REGISTRY=Registry.EM
    EM_CORE=1
    SOLVER=em
elif [ "$option" = "em_nl" ] ; then
    SOLVER=em
    REGISTRY=Registry.EM_NL
    EM_CORE=1
    COREFLAGS="$COREFLAGS -DWRF_NL"
elif [ "$option" = "em_chem" ] ; then
    SOLVER=em
    REGISTRY=Registry.EM_CHEM
    EM_CORE=1
    WRF_CHEM=1
elif [ "$option" = "nmm" ] ; then
    SOLVER=nmm
    REGISTRY=Registry.NMM  
    NMM_CORE=1
    export NMM_MAX_DIM=2600
    export COREFLAGS="-DNMM_MAX_DIM=$NMM_MAX_DIM"
elif [ "$option" = "nmm_chem" ] ; then
    SOLVER=nmm
    REGISTRY=Registry.NMM_CHEM
    NMM_CORE=1
    export NMM_MAX_DIM=2600
    export COREFLAGS="-DNMM_MAX_DIM=$NMM_MAX_DIM"
else
    SOLVER=em
    DA_CORE=1
    EM_CORE=1
    REGISTRY=Registry.wrfvar
    export RWORDSIZE="8"
    export PROMOTE_FLOAT="-DPROMOTE_FLOAT"
fi

# lifted from the configure file for mpich; 00/03/10 jm
#
# Check for perl and perl version
for p in perl5 perl
do
  # Extract the first word of "$p", so it can be a program name with args.
  set dummy $p; ac_word=$2
  if test -z "$ac_echo_n" ; then
    ac_echo_n=yes
    if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
      # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
      if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='        '
      else
        ac_n=-n ac_c= ac_t=
      fi
    else
      ac_n= ac_c='\c' ac_t=
    fi
    ac_echo_test=`echo foo 1>&1`
    if test -z "$ac_echo_test" ; then
       print_error "Your sh shell does not handle the output redirection"
       print_error "1>&1 correctly.  Configure will work around this problem,"
       print_error "but you should report the problem to your vendor."
    fi
  fi
  if test -z "$ac_echo_test" -a 1 = 1 ; then
    echo $ac_n "checking for $ac_word""... $ac_c"
  else
    echo $ac_n "checking for $ac_word""... $ac_c" 1>&1
  fi
  ac_prog_where=""
  if test -n "$PERL"; then
    ac_pg_PERL="$PERL" # Let the user override the test.
  else
    ac_first_char=`expr "$p" : "\(.\)"`
    if test "$ac_first_char" = "/" -a -x "$p" ; then
         ac_pg_PERL="$p"
         ac_prog_where=$p
    else
        IFS="${IFS=     }"; ac_save_ifs="$IFS"; IFS="${IFS}:"
        for ac_dir in $PATH; do
          test -z "$ac_dir" && ac_dir=.
          if test -f $ac_dir/$ac_word; then
            ac_pg_PERL="$p"
            ac_prog_where=$ac_dir/$ac_word
            break
          fi
        done
        IFS="$ac_save_ifs"
    fi
  fi;PERL="$ac_pg_PERL"
  if test -n "$ac_prog_where" ; then
    if test -z "$ac_echo_test" -a 1 = 1 ; then
      echo "$ac_t""found $ac_prog_where ($PERL)"
    else
      echo "$ac_t""found $ac_prog_where ($PERL)" 1>&1
    fi
    PERLFULLPATH=$ac_prog_where  
  else
    if test -z "$ac_echo_test" -a 1 = 1 ; then
      echo "$ac_t""no"
    else
      echo "$ac_t""no" 1>&1
    fi
  fi
  test -n "$PERL" && break
done

if test -z "$PERL" ; then
    # We have to set this outside of the loop lest the first failure in 
    # PROGRAM_CHECK set the value (which then terminates the effect of the
    # loop, since autoconf macros only set values that are null, they 
    # don't override them
    PERL=""
fi

if test -n "$PERL" ; then
    PERL="$PERL"
    
    perlversion=`$PERL -v | grep 'This is perl' | \
       sed -e 's/^.*v[a-z ]*\([0-9]\).*$/\1/'`

    # Should do a test first for ch_p4 etc.
    if test "$perlversion" -lt 5 ; then
        echo "Build requires perl version 5, which configure did not find."
        echo "You can set the environment variable PERL to contain the "
        echo "location of perl version 5."
        echo "Configure believes that $PERL is version $perlversion ."
        PERL=""
    fi
    
fi

# Look for netcdf
if test -z "$NETCDF" ; then
  for p in /usr/local/netcdf
  do
    if test -d $p ; then
      NETCDF=$p
      break
    fi
  done
fi
if  test -z "$NETCDF"  ; then
 
  if [ `hostname | cut -c 1-2` = "bb" -o `hostname | cut -c 1-2` = "bf" -o `hostname | cut -c 1-2` = "bs" -o \
       `hostname` = "dataproc" -o `hostname` = "ute" ] ; then
    echo 'Compiling on an NCAR system with weird paths to NetCDF'
    echo 'Setting up a local NetCDF directory with symlinks'
    if ( test -d ./netcdf_links ) ; then
      echo 'A directory ./netcdf_links already exists. Continuing.'
    else
      mkdir ./netcdf_links
      if [ -z "$OBJECT_MODE" ] ; then
         OBJECT_MODE=32 
         export OBJECT_MODE
      fi
      if [ $OBJECT_MODE -ne 64 -a \( `hostname | cut -c 1-2` = "bb" -o `hostname | cut -c 1-2` = "bf" -o `hostname | cut -c 1-2` = "bs" \) ] ; then
        ( cd ./netcdf_links ; ln -s /usr/local/lib32/r4i4 ./lib ; \
                              ln -s /usr/local/include ./include )
      else
        ( cd ./netcdf_links ; ln -s /usr/local/lib64/r4i4 ./lib ; \
                              ln -s /usr/local/include ./include )
      fi
    fi
    NETCDF=`pwd`/netcdf_links
    export NETCDF


  else
    bedone=""
    if [ -d ./netcdf_links ] ; then
      echo '** There is an existing ./netcdf_links file. Should I use? [y]'
      read resp
      if [ "$resp" = "y" ] ; then
        NETCDF=`pwd`/netcdf_links
        bedone="yes"
      else
        echo 'Removing existing ./netcdf_links directory'
        /bin/rm -fr ./netcdf_links
      fi
    else
      echo '** WARNING: No path to NETCDF and environment variable NETCDF not set.'
      echo '** would you like me to try to fix? [y]'
    fi
    netcdfipath=""
    netcdflpath=""
    while [ -z "$bedone" ] ; do 
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -d ./netcdf_links ] ; then
          echo 'There is already a ./netcdf_links directory. Okay to use links'
          echo 'in this directory for NetCDF include and lib dirs? [y]'
          read resp
          if [ "$resp" = "y" ] ; then
            NETCDF=`pwd`/netcdf_links
            export NETCDF
            bedone="yes"
            continue
          fi
        fi
        if [ -z "$netcdfipath" ] ; then
          echo 'Enter full path to NetCDF include directory on your system'
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          netcdfipath=$resp
        fi
        if  [ -z "$netcdflpath" ] ; then
          echo 'Enter full path to NetCDF library directory on your system'
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          netcdflpath=$resp
        fi

        if [ -n "$netcdflpath" -a -n "$netcdfipath" ] ; then
          if [ -d ./netcdf_links ] ; then
            echo 'Existing ./netcdf_links directory. Okay to remove. [y]'
            read resp
            if [ "$resp" = "y" ]  ; then
              /bin/rm -fr ./netcdf_links
            fi
          fi
          mkdir ./netcdf_links
          cd ./netcdf_links 
            ln -s "$netcdfipath" include
            ln -s "$netcdflpath" lib
          cd ..
          echo created new ./netcdf_links directory
          /bin/ls -lF ./netcdf_links
          NETCDF=`pwd`/netcdf_links
          export NETCDF
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
  fi
fi

if test -z "$PDHF5" ; then 
  if [ `hostname | cut -c 1-2` = "bb" -o `hostname | cut -c 1-2` = "bf" -o `hostname | cut -c 1-2` = "bs" -o \
       `hostname` = "dataproc" -o `hostname` = "ute" ] ; then
      if [ -d ~michalak/hdf5pbin ] ; then
        PHDF5=~michalak/hdf5pbin
        export PHDF5
      fi
      if [ "$OBJECT_MODE" -eq 64 ] ; then
            if [ -d ~michalak/hdf5pbin-64 ] ; then
              PHDF5=~michalak/hdf5pbin-64
              export PHDF5
            fi
      fi
  fi
fi

if [ -n "$NETCDF" ] ; then
  echo "Will use NETCDF in dir:  $NETCDF"
else
  echo "Will configure for use without NetCDF"
fi

if [ -n "$PNETCDF" ] ; then
  echo "Will use PNETCDF in dir: $PNETCDF"
# experimental, so don't tease the user if it is not there
#else
#  echo "Will configure for use without Parallel NetCDF"
fi

if [ -n "$MPIHOME" ] ; then
  echo "Will use MPI in dir:     $MPIHOME"
fi

if [ -n "$PHDF5" ] ; then
  echo "Will use PHDF5 in dir:   $PHDF5"
else
  echo "PHDF5 not set in environment. Will configure for use without."
fi

if [ -n "$HDF4" ] ; then
  echo "Will use HDF4 in dir:    $HDF4"
#else
  # No-one cares
  # echo "HDF4 not set in environment. Will configure for use without."
fi

if [ -n "$HDFEOS" ] ; then
  echo "Will use HDFEOS in dir:  $HDFEOS"
#else
  # No-one cares
  # echo "HDFEOS not set in environment. Will configure for use without."
fi

if [ -n "$ZLIB" ] ; then
  echo "Will use ZLIB in dir:    $ZLIB"
#else
  # No-one cares
  # echo "ZLIB not set in environment. Will configure for use without."
fi

if [ -n "$JPEG" ] ; then
  echo "Will use JPEG in dir:    $JPEG"
#else
  # No-one cares
  # echo "JPEG not set in environment. Will configure for use without."
fi

if [ -n "$JASPER" ] ; then
  echo "Will use JASPER in dir:  $JASPER"
#else
  # No-one cares
  # echo "JASPER not set in environment. Will configure for use without."
fi

if [ -n "$BLAS" ] ; then
  echo "Will use BLAS in dir:    $BLAS"
else
  echo "BLAS not set in environment, assume in standard libraries"
fi

if [ -n "$LAPACK" ] ; then
  echo "Will use LAPACK in dir:  $LAPACK"
else
  echo "LAPACK not set in environment, assume in standard libraries"
fi

if [ -n "$FFTPACK" ] ; then
  echo "Will use FFTPACK in dir: $FFTPACK"
else
  echo "Will configure for use without FFTPACK"
fi

if [ -n "$BUFR" ] ; then
  echo "Will use BUFR in dir:    $BUFR"
else
  echo "Will configure for use without BUFR"
fi

# Users who are cross-compiling can set environment variable 
# $WRF_OS to override the value normally obtained from `uname`.  
# If $WRF_OS is set, then $WRF_MACH can also be set to override 
# the value normally obtained from `uname -m`.  If $WRF_OS is 
# set and $WRF_MACH is not set, then $WRF_MACH defaults to "ARCH".  
# If $WRF_OS is not set then $WRF_MACH is ignored.  
if [ -n "$WRF_OS" ] ; then
  echo "${0}:  Target operating system set to \"${WRF_OS}\" via environment variable \$WRF_OS"
  os=$WRF_OS
  mach="ARCH"
  if [ -n "$WRF_MACH" ] ; then
    echo "${0}:  Target machine set to \"${WRF_MACH}\" via environment variable \$WRF_MACH"
    mach=$WRF_MACH
  fi
else
  # if the uname command exists, give it a shot and see if
  # we can narrow the choices; otherwise, spam 'em
  os="ARCH"
  mach="ARCH"
  type uname > /dev/null
  if [ $? -eq 0 ] ; then
    os=`uname`
    if [ "$os" = "AIX" -o "$os" = "IRIX" -o "$os" = "IRIX64" -o "$os" = "SunOS" -o "$os" = "HP-UX"  -o "$os" = "Darwin" ] ; then
      mach="ARCH"
    else
      if [ "$os" = "OSF1" -o "$os" = "Linux" -o "$os" = "UNICOS/mp" -o "$os" = "UNIX_System_V" ] ; then
        mach=`uname -m`
      else
        os="ARCH"
        mach="ARCH"
      fi
    fi
  fi
fi

# an IBM specific hack to adjust the bmaxstack and bmaxdata options if addressing is 32-bit
if [ "$os" = "AIX" ] ; then
      if [ -z "$OBJECT_MODE" ] ; then
         OBJECT_MODE=32
         export OBJECT_MODE
      fi
      if [ "$OBJECT_MODE" = "32" ] ; then
# the bang means nothing to sh in this context; use to represent spaces (perl will unbang)
         ldflags=-bmaxstack:256000000!-bmaxdata:2048000000
      fi
fi

# compile options that come from the environment, such as chemistry
# the "!" is removed by Config.pl
if [ -n "$WRF_CHEM" ] ; then
  if [ $WRF_CHEM = 1 ] ; then
    echo building with chemistry option
    compileflags="${compileflags}!-DWRF_CHEM"
  fi
fi
if [ -n "$WRF_PNETCDF" ] ; then
  if [ $WRF_PNETCDF = 1 ] ; then
    echo building with parallel netcdf option
    compileflags="${compileflags}!-DPNETCDF"
  fi
fi
if [ -n "$WRF_PHDF5" ] ; then
  if [ $WRF_PHDF5 = 1 ] ; then
    echo building with parallel hdf5 option
    compileflags="${compileflags}!-DPHDF5"
  fi
fi
if [ -n "$WRF_GRIB1" ] ; then
  if [ $WRF_GRIB1 = 1 ] ; then
    echo building with GRIB1 option
    compileflags="${compileflags}!-DGRIB1"
  fi
fi
if [ -n "$WRF_GRIB2" ] ; then
  if [ $WRF_GRIB2 = 1 ] ; then
    echo building with GRIB2 option
    compileflags="${compileflags}!-DGRIB2"
  fi
fi

# Found perl, so proceed with configuration
if test -n "$PERL" ; then
  $PERL arch/Config.pl -perl=$PERL -netcdf=$NETCDF -phdf5=$PHDF5 -hdf4=$HDF4 \
     -hdfeos=$HDFEOS -os=$os -mach=$mach \
     -ldflags=$ldflags -pnetcdf=$PNETCDF -zlib=$ZLIB -jpeg=$JPEG \
     -compileflags=$compileflags -registry=$REGISTRY -da_core=$DA_CORE \
     -em_core=$EM_CORE -nmm_core=$NMM_CORE -max_domains=$MAX_DOMAINS \
     -coamps_core=$COAMPS_CORE -exp_core=$EXP_CORE -coreflags="$COREFLAGS" \
     -lapack=$LAPACK -blas=$BLAS -fftpack=$FFTPACK -bufr=$BUFR \
     -rwordsize=$RWORDSIZE -solver=$SOLVER -promote_float="$PROMOTE_FLOAT"
fi

export CONFIG=configure.wrfvar

touch -t01010101 depend.txt

# new feb 2005.  test whether MPI-2
#if test -e $CONFIG ; then
#  grepresult=`grep DM_PARALLEL $CONFIG`
#  if [ "$grepresult" != "" ] ; then
#    echo testing for MPI_Comm_f2c and MPI_Comm_c2f 
#    /bin/rm -f tools/mpi2_test
#    make mpi2_test
#    if test -x tools/mpi2_test ; then
#      echo "  " MPI_Comm_f2c and MPI_Comm_c2f are supported
#      sed '/^CC .*=/s/$/ -DMPI2_SUPPORT/' $CONFIG > xx$$ ; /bin/mv xx$$ $CONFIG
#    else
#      echo "  " MPI_Comm_f2c and MPI_Comm_c2f are not supported
#    fi 
#    /bin/rm -f tools/mpi2_test
#  fi
#fi

if test $NMM_CORE = 1; then
#  grep -q DM_PARALLEL $CONFIG
#  if test $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
#  fi
  # integrity check for a kludge where a hard coded value in the 
  # registry must match the same value in arch/preamble
  registryvalue=`grep 'dimspec.* q ' ../Registry/Registry.NMM | sed -e 's/..*constant=//' -e 's/ ..*$//'`
  if test $registryvalue -ne $NMM_MAX_DIM; then
    echo "Harded coded value of dimspec q in Registry ($registryvalue) does not"
    echo "equal the hard coded value of NMM_MAX_DIM in configure ($NMM_MAX_DIM)"
    echo "Please fix and try again."
    exit 2
  fi
fi

