AC_INIT(aalib.c)
AC_CONFIG_HEADER(config.h)

dnl Checks for programs.

x_driver_test=yes
curses_driver_test=yes
linux_driver_test=yes
slang_driver_test=yes
force_curses_driver=false
x_kbddriver_test=yes
curses_kbddriver_test=yes
slang_kbddriver_test=yes
curses_mousedriver_test=yes
gpm_mousedriver_test=yes
x_mousedriver_test=yes

use_cc_g_flag=no
CCOPTS="$CFLAGS"
AC_PROG_CC
AC_PROG_MAKE_SET
CFLAGS="$CCOPTS"
AC_PROG_INSTALL

dnl detect OS

AC_CHECK_PROG(system,uname,`uname`,unknown)
if test "x$system" = xLinux; then
AC_CHECK_PROG(machine,uname,`uname --machine`,unknown)
fi
AC_HEADER_STDC

cc_uses_g=yes
if test x$GCC = xyes; then
    if test x$ac_cv_prog_gcc_g = xyes; then
	:
    else
	cc_uses_g=no
    fi
fi
if test "x$CCOPTS" = x; then
    if test x$GCC = xyes; then
	if test x$machine = xi486; then
	  mflag=486
	fi
	if test x$machine = xi586; then
	  mflag=486
	fi
        if test x$mflag = x486; then
            CCOPTS='-m486 -O2 -m486 -fstrength-reduce -ffast-math -fomit-frame-pointer' 
	    if test x$use_cc_g_flag = xyes; then
		if test $cc_uses_g = yes; then
		    CCOPTS='-m486 -g -O'
		fi
	    fi
	else
            CCOPTS='-O2 -fstrength-reduce -ffast-math -fomit-frame-pointer' 
	    if test x$use_cc_g_flag = xyes; then
		if test $cc_uses_g = yes; then
		    CCOPTS='-g -O'
		fi
	    fi
	fi
    else
	if test x$use_cc_g_flag = xyes; then
	    CCOPTS="$CFLAGS"
	else
	    CCOPTS=
	fi
    fi
fi

CFLAGS="$CCOPTS"
dnl command line options

AC_ARG_WITH(x11-driver,
        [  --with-x11-driver=no    Used to disable x11 driver],[
	if test x$withval = xno; then
	  x_driver_test=no
	fi
])
AC_ARG_WITH(slang-driver,
        [  --with-slang-driver=no   Used to disable slang driver],[
	if test x$withval = xno; then
	  vga_driver_test=no
	fi
])
AC_ARG_WITH(curses-driver,
        [  --with-curses-driver=no/yes Used to disable/force curses driver],[
	if test x$withval = xno; then
	  curses_driver_test=no
	fi
	if test x$withval = xyes; then
	  force_curses_driver=true
	fi
])
AC_ARG_WITH(gpm-mouse, 
	[  --with-gpm-mouse=base-dir  Specifies the base gpm directory],
	if test x$withval = xyes
	then 
		AC_MSG_WARN(Usage is: --with-gpm-mouse=base-dir)
	else
		LIBS="$LIBS -L$withval/lib -lgpm"
                AC_DEFINE(GPM_MOUSEDRIVER)
	        gpm_mousedriver=true
		CFLAGS="$CFLAGS -I$withval/include"
		AC_MSG_RESULT(defining use_gpm)
	fi
)

if test  "$x_driver_test" = yes; then
AC_PATH_X
AC_PATH_XTRA
if test "x$no_x" = x; then
    x_driver=true
    AC_DEFINE(X11_DRIVER)
    LIBS="$LIBS $X_EXTRA_LIBS $X_LIBS -lX11"
    CFLAGS="$CFLAGS $X_CFLAGS"
fi

fi
if test  "$gpm_mousedriver_test" = yes; then
  AC_CHECK_LIB(gpm, Gpm_Repeat,[
          AC_DEFINE(GPM_MOUSEDRIVER)
	  gpm_mousedriver=true
          LIBS="$LIBS -lgpm"])
fi

dnl
dnl This part supplies reasonable defaults for CFLAGS, if they weren't
dnl specified by ''CFLAGS=flags ./configure''
dnl




search_ncurses=true
AC_DEFUN(AC_USE_SUNOS_CURSES, [
	search_ncurses=false
	curses_driver=true
	screen_manager="SunOS 4.x /usr/5include curses"
	AC_MSG_RESULT(Using SunOS 4.x /usr/5include curses)
	AC_DEFINE(SUNOS_CURSES)
	AC_DEFINE(NO_COLOR_SUPPORT)
	AC_DEFINE(USE_SYSV_CURSES)
	CFLAGS="$CFLAGS -I/usr/5include"
	XCURSES="xcurses.o /usr/5lib/libcurses.a /usr/5lib/libtermcap.a"
	AC_MSG_RESULT(Please note that some screen refreshs may fail)
	AC_WARN(Reconsider using Slang)
])

AC_DEFUN(AC_USE_OSF1_CURSES, [
       AC_MSG_RESULT(Using OSF1 curses)
       search_ncurses=false
       curses_driver=true
       screen_manager="OSF1 curses"
       AC_DEFINE(NO_COLOR_SUPPORT)
       AC_DEFINE(USE_SYSV_CURSES)
       XCURSES="xcurses.o"
       LIBS="$LIBS -lcurses"
])

AC_DEFUN(AC_USE_SYSV_CURSES, [
	AC_MSG_RESULT(Using SysV curses)
        curses_driver=true
	AC_DEFINE(USE_SYSV_CURSES)
	XCURSES=""
	search_ncurses=false
	screen_manager="SysV/curses"
	LIBS="$LIBS -lcurses"
])

XCURSES=""
AC_SUBST(XCURSES)



AC_ARG_WITH(sunos-curses,
	[  --with-sunos-curses     Used to force SunOS 4.x curses],[
	if test x$withval = xyes; then
		AC_USE_SUNOS_CURSES
	fi
])

AC_ARG_WITH(osf1-curses,
	[  --with-osf1-curses	Used to force OSF/1 curses],[
	if test x$withval = xyes; then
		AC_USE_OSF1_CURSES
	fi
])

AC_ARG_WITH(vcurses,
	[  --with-vcurses[=incdir] Used to force SysV curses],
	if test x$withval = xyes; then
		CFLAGS="$CFLAGS"
        else
		CFLAGS="$CFLAGS -I$withval"
	fi
	AC_USE_SYSV_CURSES
)
        	
AC_ARG_WITH(ncurses,
	[  --with-ncurses[=base-dir] Compile with ncurses/locate base dir],
	if test x$withval = xyes
	then
		search_ncurses=true
	else
		LIBS="$LIBS -L$withval/lib -lncurses"
		CFLAGS="$CFLAGS -I$withval/include"
		search_ncurses=false
                curses_driver=true
		screen_manager="ncurses"
		AC_DEFINE(USE_NCURSES)
	fi
)

if test  "$slang_driver_test" = yes; then
  AC_DEFUN(AC_SLANG, [
    if $search_slang
    then
        if test -f $1/$2
	then
	    LIBS1="$LIBS"
 	    LIBS="$LIBS $3"
            AC_CHECK_LIB(slang, SLsmg_init_smg)
            if test "x$ac_cv_lib_slang_SLsmg_init_smg" = xyes; then 
	      AC_MSG_RESULT(Found slang on $1/$2)
 	      LIBS="$LIBS $3"
	      CFLAGS="$CFLAGS $4"
              slang_driver=true
	      search_slang=false
	      AC_DEFINE(SLANG_DRIVER)
	    else
             unset ac_cv_lib_slang_SLsmg_init_smg
	     LIBS="$LIBS1"
	    fi
	fi
     fi
  ])
  AC_SLANG(/usr/include, slang.h, , -I/usr/include/slang , "slang on /usr/include")
  AC_SLANG(/usr/include/slang, slang.h, , -I/usr/include/slang , "slang on /usr/include/slang")
  AC_SLANG(/usr/local/include, slang.h, , -I/usr/local/include/slang , "slang on /usr/local/include")
  AC_SLANG(/usr/local/include/slang, slang.h, , -I/usr/local/include/slang , "slang on /usr/local/include/slang")
  if test "$slang_driver" = true; then
    if test "$force_curses_driver" = false; then
      echo "Disabling curses driver since slang is available"
      curses_driver_test=no
    fi
  fi
fi

dnl
dnl Parameters: directory filename LIBS_append CFLAGS_append nicename
dnl
AC_DEFUN(AC_NCURSES, [
    if $search_ncurses
    then
        if test -f $1/$2
	then
	    LIBS1="$LIBS"
 	    LIBS="$LIBS $3"
            AC_CHECK_LIB(ncurses, initscr)
            if test "x$ac_cv_lib_ncurses_initscr" = xyes; then 
	      AC_MSG_RESULT(Found ncurses on $1/$2)
 	      LIBS="$LIBS $3"
	      CFLAGS="$CFLAGS $4"
	      search_ncurses=false
              curses_driver=true
	      screen_manager=$5
	      AC_DEFINE(USE_NCURSES)
	    else
             unset ac_cv_lib_ncurses_initscr
	     LIBS="$LIBS1"
	    fi
	fi
    fi
])

dnl
dnl Parameters: directory filename LIBS_append CFLAGS_append nicename
dnl
AC_DEFUN(AC_CURSES, [
    if $search_ncurses
    then
        if test -f $1/$2
	then
	    LIBS1="$LIBS"
 	    LIBS="$LIBS $3"
            AC_CHECK_LIB(curses, initscr)
            if test "x$ac_cv_lib_curses_initscr" = xyes; then 
	      AC_MSG_RESULT(Found generic curses on $1/$2)
	      CFLAGS="$CFLAGS $4"
	      search_ncurses=false
              curses_driver=true
	      AC_DEFINE(CURSES_DRIVER)
	      screen_manager=$5
	      AC_DEFINE(USE_CURSES)
	    else
             unset ac_cv_lib_curses_initscr
	     LIBS="$LIBS1"
	    fi
	fi
    fi
])

if test  "$curses_driver_test" = yes; then
if $search_ncurses
then
    AC_CHECKING("location of curses")

    AC_NCURSES(/usr/include, ncurses.h, , -I/usr/include/ncurses , "ncurses on /usr/include")
    AC_NCURSES(/usr/include/ncurses, ncurses.h, , -I/usr/include/ncurses, "ncurses on /usr/include/ncurses")
    AC_NCURSES(/usr/local/include, ncurses.h, ,-L/usr/local/lib, -I/usr/local/include, "ncurses on /usr/local")
    AC_NCURSES(/usr/local/include/ncurses, ncurses.h, -L/usr/local/lib -L/usr/local/lib/ncurses, -I/usr/local/include/ncurses, "ncurses on /usr/local/include/ncurses")

    AC_NCURSES(/usr/local/include/ncurses, curses.h, -L/usr/local/lib, -I/usr/local/include/ncurses -DRENAMED_NCURSES, "renamed ncurses on /usr/local/.../ncurses")

    AC_NCURSES(/usr/include/ncurses, curses.h, , -I/usr/include/ncurses -DRENAMED_NCURSES, "renamed ncurses on /usr/include/ncurses")
    AC_CURSES(/usr/include, curses.h, , , "curses on /usr/include")

    dnl
    dnl We couldn't find ncurses, try SysV curses
    dnl
    if $search_ncurses 
    then
        AC_EGREP_HEADER(init_color, /usr/include/curses.h,
	    AC_USE_SYSV_CURSES)
    fi

    dnl
    dnl Try SunOS 4.x /usr/5{lib,include} ncurses
    dnl The flags SUNOS_CURSES, USE_BSD_CURSES and BUGGY_CURSES
    dnl should be replaced by a more fine grained selection routine
    dnl
    if $search_ncurses
    then
	if test -f /usr/5include/curses.h
	then
	    AC_USE_SUNOS_CURSES
        fi
    fi
fi
AC_CHECK_FUNCS(mousemask termattrs wredrawln redrawwin curs_set)
fi



#dnl Checks for libraries.
#dnl Replace `main' with a function in -lX11:
#AC_CHECK_LIB(X11, main)
#dnl Replace `main' with a function in -lefence:
#AC_CHECK_LIB(efence, main)
#dnl Replace `main' with a function in -lgpm:
#AC_CHECK_LIB(gpm, main)
#dnl Replace `main' with a function in -lm:
#AC_CHECK_LIB(m, main)
#dnl Replace `main' with a function in -lncurses:
#AC_CHECK_LIB(ncurses, main)
#dnl Replace `main' with a function in -lslang:
#AC_CHECK_LIB(slang, main)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sys/ioctl.h sys/time.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_STRUCT_ST_RDEV
AC_HEADER_TIME

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(strdup)
LIBS="libaa.a $LIBS"
echo "-----------------------------------------------------"
echo "CFLAGS  $CFLAGS"
echo "LDFLAGS $LDFLAGS"
echo "CC      $CC"
echo "-----------------------------------------------------"
echo "Display drivers:"
if test  "$slang_driver" = true; then
  echo " slang"
  if test  "$slang_kbddriver_test" = yes; then
     AC_DEFINE(SLANG_KBDDRIVER)
     slang_kbddriver=true
  fi
fi
if test  "$curses_driver" = true; then
  AC_DEFINE(CURSES_DRIVER)
  echo " curses"
  if test  "$curses_kbddriver_test" = yes; then
     AC_DEFINE(CURSES_KBDDRIVER)
     curses_kbddriver=true
  fi
  if test  "$curses_kbddriver_test" = yes; then
    if test "x$ac_cv_func_mousemask" = xyes; then
     AC_DEFINE(CURSES_MOUSEDRIVER)
     curses_mousedriver=true
    fi
  fi
fi
if test  "$linux_driver_test" = yes; then
 if test "x$system" = xLinux; then
   echo " Linux console"
   AC_DEFINE(LINUX_DRIVER)
 fi
fi
if test  "$x_driver" = true; then
  echo " x11"
  if test  "$x_kbddriver_test" = yes; then
     AC_DEFINE(X11_KBDDRIVER)
     x_kbddriver=true
  fi
  if test  "$x_mousedriver_test" = yes; then
     AC_DEFINE(X11_MOUSEDRIVER)
     x_mousedriver=true
  fi
fi
echo "Keyboard drivers:"
if test  "$slang_kbddriver" = true; then
  echo " slang"
fi
if test  "$curses_kbddriver" = true; then
  echo " curses"
fi
if test  "$x_kbddriver" = true; then
  echo " X11"
fi
echo "Mouse drivers:"
if test  "$curses_mousedriver" = true; then
  echo " curses"
fi
if test  "$gpm_mousedriver" = true; then
  echo " gpm"
fi
if test  "$x_mousedriver" = true; then
  echo " X11"
fi
echo "-----------------------------------------------------"


AC_OUTPUT(Makefile)
