#exported variables
_readline_list="readline readline_version RLINCLUDE RLLIBS"
readline=
case "$with_readline" in
  yes|"");;
  *)
  if test -z "$with_readline_lib"; then
    with_readline_lib="$with_readline/lib"
  fi
  if test -z "$with_readline_include"; then
    with_readline_include="$with_readline/include"
  fi;;
esac

pth="$with_readline_include"
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
     ;;
esac

exe=$osname-$arch-rlv$$$exe_suff
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
  readline=$with_readline_lib
  rl="-L$readline -lreadline"
else
  rl="-lreadline"
fi

# try linking without locatelib (without -L except --with-xxx-lib)
for tlib in "" tinfow tinfo ncursesw 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

readline_version=
if test -r $exe; then
  readline_version=`env LD_LIBRARY_PATH="$LD_LIBRARY_PATH$dir_sep$readline" $RUNTEST $exe`;
fi
. cleanup_exe

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
