#exported variables
_readline_list="readline readline_version RLINCLUDE RLLIBS"

readline=
if test -n "$with_readline"; then
  with_readline_lib="$with_readline_lib $with_readline/lib"
  with_readline_include="$with_readline_include $with_readline/include"
fi

# Readline -- Headers
pth="$with_readline_include $basic_include_path"
x=`./locate 'readline/readline.h' '' $pth`
case $x in
 ?:/*|/*) rl_include=`dirname $x`
    echo ..."Found readline header in $rl_include"
    if (echo $rl_include | grep "readline$" > /dev/null); then
      rl_include=`dirname $rl_include`
      RLINCLUDE="-I$rl_include"
    fi
     ;;
  *) echo ..."readline header file not found by Configure, trying to proceed"
     readline=;;
esac

exe=$osname-$arch-rlv$$
cmd0="$CC $CFLAGS $extraflag $RLINCLUDE -o $exe rl_version.c"
try() { RLLIBS=$1; cmd="$cmd0 $1"; . log_cmd; }

if test -n "$with_readline_lib"; then
  rl="-L$with_readline_lib -lreadline"
else
  rl="-lreadline"
fi

# try linking without locatelib (without -L except --with-xxx-lib)
for tlib in "" tinfo ncurses termcap; do
  t=$rl
  if test -n "$tlib"; then # need a termcap compatible library?
    eval with="\$with_${tlib}_lib"
    if test -n "$with"; then
      t="$t -L$with -l$tlib"
    else
      t="$t -l$tlib"
    fi
  fi
  try "$t"
  if test -r $exe; then break; fi
done

# failed; retry with -Lxxx using locatelib
if test ! -r $exe; then
  pth="$with_readline_lib $libpth"
  lib=readline; . ./locatelib
  if test -n "$readline"; then
    rl="-L$readline -lreadline"
  else
    rl="-lreadline" # not found, sequel will almost certainly fail
  fi
  for tlib in "" tinfo ncurses termcap; do
    t=$rl
    if test -n "$tlib"; then # termcap compatible library, using locatelib
      eval pth="\$with_${tlib}_lib $libpth"
      lib=$tlib; . ./locatelib
      eval p="\$$tlib"
      if test -n "$p"; then
        t="$t -L$p -l$tlib"
      else
        t="$t -l$p"
      fi
    fi
    try "$t"
    if test -r $exe; then break; fi
  done
fi

readline_version=
if test -r $exe; then
  readline_version=`env LD_LIBRARY_PATH="$LD_LIBRARY_PATH$dir_sep$readline" $RUNTEST $exe`;
fi
rm -f $exe $exe$exe_suff;

case "$readline_version" in
*Editline*|*EditLine*) readline=
  echo "###"
  echo "### Editline wrapper detected, building without readline support"
  echo "###";;
"") readline=
  echo "###"
  echo "### Readline library does not seem to work. Maybe install libncurses?"
  echo "###";;
*) if test -z "$readline"; then readline=yes; fi;;
esac

if test -n "$readline"; then
  if test "$fastread" != yes; then
    cat << EOM
==========================================================================
GNU readline provides line editing in the gp shell, with history and
context-dependent completions. You should really answer 'yes' to the
following question, unless you are trying to overcome a problem in
the default build.
EOM
    echo $n "Do you want to use GNU readline library within GP ? $c"
    rep='y n'; dflt=y; . ./myread
    case $ans in
      n) readline=;;
    esac
  fi
fi

if test -z "$readline"; then
  echo "### Building without GNU readline support"
  RLLIBS=
  RLINCLUDE=
else
  echo "Using GNU readline, version $readline_version"
fi
