#!/bin/sh

arg1=$1

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

#=================================================================
# Look for support libraries, Parallel NetCDF (PNETCDF), W3 (LIBW3),
#sp (LIBSP), and sfcio (LIBSFCIO)

if  test -z "$PNETCDF"  ; then
    bedone=""
    echo '** WARNING: path to Parallel NetCDF library $PNETCDF 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 Parallel NetCDF 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
          PNETCDF=$path
          export PNETCDF
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$NETCDF"  ; then
    bedone=""
    echo '** WARNING: path to NetCDF library $NETCDF 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 NetCDF 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
          NETCDF=$path
          export NETCDF
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$LIB_NEMSIO_PATH"  ; then
    bedone=""
    echo '** WARNING: path to NEMSIO library $LIB_NEMSIO_PATH 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 NEMSIO 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
          LIB_NEMSIO_PATH=$path
          export LIB_NEMSIO_PATH
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$LIB_BACIO_PATH"  ; then
    bedone=""
    echo '** WARNING: path to BACIO library $LIB_BACIO_PATH 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
          LIB_BACIO_PATH=$path
          export LIB_BACIO_PATH
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$LIB_W3_PATH"  ; then
    bedone=""
    echo '** WARNING: path to W3 library $LIB_W3_PATH 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
          LIB_W3_PATH=$path
          export LIB_W3_PATH
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$LIB_SP_PATH"  ; then
    bedone=""
    echo '** WARNING: path to SP library $LIB_SP_PATH 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 SP 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
          LIB_SP_PATH=$path
          export LIB_SP_PATH
          bedone="yes"
        fi
      else
        bedone="yes"
      fi
    done
fi

if  test -z "$LIB_SFCIO_PATH"  ; then
    bedone=""
    echo '** WARNING: path to SFCIO library $LIB_SFCIO_PATH 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 SFCIO 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
          LIB_SFCIO_PATH=$path
          export LIB_SFCIO_PATH
          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

# we need a blas path if not on IBM AIX.
if [ "$os" != "AIX" ] ; then
   if  test -z "$LIB_BLAS_PATH"  ; then
       bedone=""
       echo '** WARNING: path to BLAS library $LIB_BLAS_PATH 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 BLAS 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
                LIB_BLAS_PATH=$path
                export LIB_BLAS_PATH
                bedone="yes"
             fi
          else
            bedone="yes"
          fi
       done
   fi
fi

up=$(dirname $( pwd ) )
hlib="$up/hwrf-utilities/libs"

# Check libraries for GRIB2
if [[ "Q${LIB_G2_PATH:-}" == Q ]] ; then
    LIB_G2_PATH="$hlib"
fi
if [[ "Q${LIB_JASPER_PATH:-}" == Q ]] ; then
    for ldir in ${JASPER:+$JASPER/lib} ${JASPERLIB:+$JASPERLIB} \
		/usrx/local/lib /gpfs/hps/nco/opts/nwprod/lib \
		/gpfs/hps/usrx/local/jasper/1.900.1/lib \
		/usr/lib64 /usr/local/lib /usr/lib ; do
	if [[ -s $ldir/libjasper.a || -s $ldir/libjasper.so ]] ; then
	    LIB_JASPER_PATH="$ldir"
	    break
	fi
    done
    if [[ "Q${LIB_JASPER_PATH:-}" == Q ]] ; then
	echo "Cannot find libjasper." 1>&2
	echo "Set LIB_JASPER_PATH to jasper lib path." 1>&2
	exit 1
    fi
fi
if [[ "Q${LIB_PNG_PATH:-}" == Q ]] ; then
    for ldir in ${LIB_PNG_PATH:-} \
		/usrx/local/lib /gpfs/hps/nco/opts/nwprod/lib \
		/gpfs/hps/usrx/local/libpng/1.2.49/cce/lib/ \
		/usr/lib64 /usr/local/lib /usr/lib ; do
	if [[ -s $ldir/libpng.a || -s $ldir/libpng.so ]] ; then
	    LIB_PNG_PATH="$ldir"
	    break
	fi
    done
    if [[ "Q${LIB_PNG_PATH:-}" == Q ]] ; then
	echo "Cannot find libpng." 1>&2
	echo "Set LIB_PNG_PATH to libpng path." 1>&2
	exit 1
    fi
fi
if [[ "Q${LIB_Z_PATH:-}" == Q ]] ; then
    for ldir in ${LIB_Z_PATH:-} $(dirname ${Z_LIB}:-) \
		/usrx/local/lib /gpfs/hps/nco/opts/nwprod/lib \
		/gpfs/hps/usrx/local/zlib/1.2.8/gnu/49/lib/ \
		/usr/lib64 /usr/local/lib /usr/lib; do
	if [[ -s $ldir/libz.a || -s $ldir/libz.so ]] ; then
	    LIB_Z_PATH="$ldir"
	    break
	fi
    done
    if [[ "Q${LIB_Z_PATH:-}" == Q ]] ; then
	echo "Cannot find zlib." 1>&2
	echo "Set LIB_Z_PATH to zlib path." 1>&2
	exit 1
    fi
fi

# so proceed with configuration
perl arch/Config.pl  -corepath=$CORE_DIR -libg2=$LIB_G2_PATH -libjasper=$LIB_JASPER_PATH -libpng=$LIB_PNG_PATH -libz=$LIB_Z_PATH -libnemsio=$LIB_NEMSIO_PATH -libbacio=$LIB_BACIO_PATH -libw3=$LIB_W3_PATH -libsp=$LIB_SP_PATH -libsfcio=$LIB_SFCIO_PATH -libblas=$LIB_BLAS_PATH -os=$os -mach=$mach -netcdf=$NETCDF -pnetcdf=$PNETCDF