#!/bin/sh
#*=====================================================================*/
#*    serrano/out/bigloo1.9e/autoconf/readline                         */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Fri Jan 15 09:12:32 1999 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check if readline exits. Return 1 or 0.                          */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cc=gcc
cflags=
user=`whoami`
file=/tmp/actest$user
aout=/tmp/actest$user
readline=-lreadline
termcap=-ltermcap

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;
    --cc=*|-cc=*)
      cc="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --readline*|-readline=*)
      readline="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --termcap*|-termcap=*)
      termcap="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$cc $cflags $file.c -o $aout $readline >/dev/null 2> /dev/null"
compilet="$cc $cflags $file.c -o $aout $termcap $readline $termcap >/dev/null 2> /dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <readline/readline.h>

char *command_generator( char *s, int x ) {
   static char *tab[] = { "toto", "tutu", "tata" };
   return tab[ x ];
}

int read_cmd_line( char *prompt ) {
   char *line_read;
   if( !(line_read = readline( prompt )) )
      return 0;
   if( line_read && *line_read )
      add_history( line_read );
   free( line_read );
   return 1;
}

char *stub_command_generator( char *text, int state ) {
   return command_generator( text, state );
}

static char **bdb_completion( char *text, int start, int end ) {
   char **matches;
   matches = (char **)NULL;
   matches = completion_matches( rl_line_buffer, stub_command_generator );
   return matches;
}

char **
bdb_nofilename_completion() {
   return NULL;
}

void initialize_readline() {
   rl_completion_entry_function = (Function *)bdb_nofilename_completion;
   rl_attempted_completion_function  = (CPPFunction *)bdb_completion;
}

int main( int argc, char *argv[] ) {
   printf( "%d\n", read_cmd_line( argv[ 0 ] ) );
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval $compile; then
   \rm -f $file.*
   rm -f $aout
   echo $readline
   exit 1
else
  if eval $compilet; then
     \rm -f $file.*
     rm -f $aout
     echo "$termcap $readline $termcap"
     exit 1
  else
     \rm -f $file.*
     rm -f $aout
     exit 0
  fi
fi

   



