#!/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 hycom; please re-configure and try again"
  exit
fi



#=================================================================
# Look for support libraries, W3 (EMC and NCO) BACIO and G2

if  test -z "$W3EMC_LIB4"  ; then
    bedone=""
    echo '** WARNING: path to W3 library $W3EMC_LIB4 not set.'
    echo '** would you like to try to fix? [y]'
    path=""
    while [ -z "$bedone" ] ; do 
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -z "$path" ] ; then
          echo 'Enter full path to W3 library on your system'
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          path=$resp
        fi

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

if  test -z "$W3NCO_LIB4"  ; then
    bedone=""
    echo '** WARNING: path to W3 library $W3NCO_LIB4 not set.'
    echo '** would you like to try to fix? [y]'
    path=""
    while [ -z "$bedone" ] ; do
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -z "$path" ] ; then
          echo 'Enter full path to W3 library on your system'
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          path=$resp
        fi

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

if  test -z "$BACIO_LIB4"  ; then
    bedone=""
    echo '** WARNING: path to BACIO library $BACIO_LIB not set.'
    echo '** would you like to try to fix? [y]'
    path=""
    while [ -z "$bedone" ] ; do 
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -z "$path" ] ; then
          echo 'Enter full path to BACIO library on your system '
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          path=$resp
        fi

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

if  test -z "$G2_LIB4"  ; then
    bedone=""
    echo '** WARNING: path to G2 library $G2_LIB not set.'
    echo '** would you like to try to fix? [y]'
    path=""
    while [ -z "$bedone" ] ; do
      read resp
      if [ "$resp" = "y" -o -z "$resp" ]  ; then
        if [ -z "$path" ] ; then
          echo 'Enter full path to G2 library on your system '
          read resp
          if [ ! -d "$resp" ] ; then
            echo "invalid path: $resp. Try again? [y]" ; continue
          fi
          path=$resp
        fi

        if [ -n "$path" ] ; then
          G2_LIB=$path
          export G2_LIB
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
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`
      else
        os="ARCH"
        mach="ARCH"
      fi
    fi
fi

# so proceed with configuration
perl arch/Config.pl  -corepath=$CORE_DIR -libw3=$LIB_W3_PATH -libbacio=$LIB_BACIO_PATH -netcdf=$NETCDF -USENETCDFF=$USENETCDFF -os=$os -mach=$mach