#
# $Id: configure.in,v 1.2 1997/04/03 18:07:16 grubba Exp $
#
# Configure script for the mysql-module
#
# Henrik Grubbström
#

#
# NOTE: 
#   Prior to 3.20.0          		After 3.20.0
#   --------------------------------------------------------------
#   /usr/local/mysql/mach-lib-threads	/usr/local/lib/mysql
#   /usr/local/mysql/include		/usr/local/include/mysql
#   libmysql.a				libmysqllib.a
#   libstrings.a			libmystrings.a
#


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

sinclude(../module_configure.in)

OLD_LIBS=$LIBS
OLD_LDFLAGS=$LDFLAGS
OLD_CPPFLAGS=$CPPFLAGS
MYSQL_LIBS=""

AC_ARG_WITH(mysql,  [  --without-mysql       no support for the Mysql database],[],[with_mysql=yes])

if test x$with_mysql = xyes; then

  AC_MSG_CHECKING(Checking for Mysql lib-directory)

  AC_CACHE_VAL(pike_cv_mysql_lib_dir, [
    for pike_cv_mysql_lib_dir in /usr/local/lib/mysql /usr/local/mysql/lib/mysql /usr/gnu/lib/mysql /usr/lib/mysql /lib/mysql /usr/local/mysql/lib /usr/local/mysql/mach-lib-thread no; do
      if test -d $pike_cv_mysql_lib_dir/.; then
        break
      else
        :
      fi
    done
  ])

  AC_MSG_RESULT($pike_cv_mysql_lib_dir)

  if test x$pike_cv_mysql_lib_dir = xno; then :; else
    echo Adding $pike_cv_mysql_lib_dir to the library search path.
    LDFLAGS="-L$pike_cv_mysql_lib_dir ${LDFLAGS}"
  fi

  AC_MSG_CHECKING(Checking for Mysql include-directory)

  AC_CACHE_VAL(pike_cv_mysql_include_dir, [
    for pike_cv_mysql_include_dir in /usr/local/include/mysql /usr/local/mysql/include/mysql /usr/gnu/include/mysql /usr/include/mysql /include/mysql /usr/local/mysql/include no; do
      if test -d $pike_cv_mysql_include_dir/.; then
        break
      else
	:
      fi
    done
  ])

  AC_MSG_RESULT($pike_cv_mysql_include_dir)

  if test x$pike_cv_mysql_include_dir = xno; then :; else
    echo Adding $pike_cv_mysql_include_dir to the include search path.
    CPPFLAGS="-I$pike_cv_mysql_include_dir ${CPPFLAGS}"
  fi

  # Header file

  AC_CHECK_HEADERS(mysql.h mysql/mysql.h)

  # Mysql libs

  pike_cv_mysql="unknown"

  AC_CHECK_LIB(mystrings, bchange, [
    LIBS="-lmystrings $LIBS"
    MYSQL_LIBS="-lmystrings ${MYSQL_LIBS}"
    pike_cv_mysql="post3.20"
  ], [
    AC_CHECK_LIB(strings, bchange, [
      LIBS="-lstrings $LIBS"
      MYSQL_LIBS="-lstrings ${MYSQL_LIBS}"
      pike_cv_mysql="pre3.20"
    ], [])
  ])

  AC_MSG_CHECKING(Mysql version)

  AC_MSG_RESULT($pike_cv_mysql)

  # System libs which might be needed

  if echo $LIBS|grep -- -lsocket >&5 2>&5; then
    :
  else
    AC_CHECK_LIB(socket, socket, [
      LIBS="-lsocket $LIBS"
      MYSQL_LIBS="-lsocket ${MYSQL_LIBS}"
    ], [])
  fi
  if echo $LIBS|grep -- -lnsl >&5 2>&5; then
    :
  else
    AC_CHECK_LIB(nsl, gethostbyname, [
      LIBS="-lnsl $LIBS"
      MYSQL_LIBS="-lnsl ${MYSQL_LIBS}"
    ], [])
  fi
  if echo $LIBS|grep -- -lm >&5 2>&5; then
    :
  else
    AC_CHECK_LIB(m, floor, [
      LIBS="-lm $LIBS"
      MYSQL_LIBS="-lm ${MYSQL_LIBS}"
    ], [])
  fi

  # Pthreads is still needed in 3.20.0.
  AC_CHECK_FUNC(pthread_self, [], [
    AC_CHECK_LIB(pthread, pthread_self, [
      LIBS="-lpthread $LIBS"
      echo Warning added -lpthread to \$LIBS\!
    ], [
      AC_CHECK_LIB(pthreads, pthread_self, [
        LIBS="-lpthreads $LIBS"
	echo Warning added -lpthreads to \$LIBS\!
      ], [])
    ])
  ])

  AC_CHECK_LIB(dbug, _db_doprnt_, [
    LIBS="-ldbug $LIBS"
    MYSQL_LIBS="-ldbug ${MYSQL_LIBS}"
  ], [])

  AC_CHECK_LIB(mysys, my_init, [
    LIBS="-lmysys $LIBS"
    MYSQL_LIBS="-lmysys ${MYSQL_LIBS}"
  ], [])

  # Try a couple of mysqlclient libs
  # in order of age, newest first.

  AC_CHECK_LIB(mysqlclient, mysql_connect, [
    LIBS="-lmysqlclient $LIBS"
    MYSQL_LIBS="-lmysqlclient ${MYSQL_LIBS}"
  ], [ 
    AC_CHECK_LIB(mysqllib, mysql_connect, [
      LIBS="-lmysqllib $LIBS"
      MYSQL_LIBS="-lmysqllib ${MYSQL_LIBS}"
    ], [
      AC_CHECK_LIB(mysql, mysql_connect, [
	LIBS="-lmysql $LIBS"
	MYSQL_LIBS="-lmysql ${MYSQL_LIBS}"
      ], [ pike_cv_mysql="no" ])
    ])
  ])

  if test x$pike_cv_mysql = xno; then
    # Restore variables, so we don't link with unnessesary libs

    LIBS=$OLD_LIBS
    CPPFLAGS=$OLD_CPPFLAGS
    LDFLAGS=$OLD_LDFLAGS
    MYSQL_LIBS=""
  else
    AC_DEFINE(HAVE_MYSQL)

    AC_CHECK_FUNCS(mysql_real_query mysql_fetch_lengths)
  fi
else
  :
fi

AC_SUBST(MYSQL_LIBS)

AC_OUTPUT(Makefile,echo FOO >stamp-h )
