#!/bin/sh

arg1=$1

CORE_DIR=`pwd`
echo "$CORE_DIR"
export CORE_DIR

# 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
 
    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 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

USENETCDFF=""  # see below
if [ -n "$NETCDF" ] ; then
  echo "Will use NETCDF in dir: $NETCDF"
# for 3.6.2 and greater there might be a second library, libnetcdff.a .  Check for this and use
# if available
  if [ -f "$NETCDF/lib/libnetcdff.a" ] ; then
    USENETCDFF="-lnetcdff"
  fi
else
  echo "NETCDF is required to compile GSI; please re-configure and try again"
  exit
fi

# Look for wrf libs
if test -z "$WRF_DIR" ; then
  for p in `pwd`/../WRFV3
  do
    if test -d $p ; then
      WRF_DIR=$p
      break
    fi
  done
fi

if  test -z "$WRF_DIR"  ; then
 
    bedone=""
    echo '** WARNING: No path to WRF_DIR and environment variable WRF_DIR not set.'
    echo '** would you like to try to fix? [y]'
    wrfpath=""
    while [ -z "$bedone" ] ; do 
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -z "$wrfpath" ] ; then
          echo 'Enter full path to WRF install directory on your system (i.e. $HOME/WRFV3)'
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          wrfpath=$resp
        fi

        if [ -n "$wrfpath" ] ; then
          WRF_DIR=$wrfpath
          export WRF_DIR
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if [ -n "$WRF_DIR" ] ; then
  echo "Will use WRF in dir: $WRF_DIR"
else
  echo "WRF is required to compile GSI; please re-configure and try again"
  exit
fi

  # 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`
        if [ "$mach" = "ia64" -a -f /etc/sgi-release ] ; then
            mach="Altix"
        fi
      else
        os="ARCH"
        mach="ARCH"
      fi
    fi
fi

# so proceed with configuration
perl arch/Config.pl  -corepath=$CORE_DIR -wrfpath=$WRF_DIR -netcdf=$NETCDF -USENETCDFF=$USENETCDFF -os=$os -mach=$mach 
