dnl Process this file with autoconf to produce a configure script.
AC_INIT(README)
AC_CONFIG_HEADER(config.h)

dnl This is the default destination
AC_PREFIX_DEFAULT($HOME/oer+MySQL)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_INSTALL

dnl we need uname
AC_CHECK_PROG(UNAME,uname,uname)

AC_ARG_ENABLE(debug, [  --enable-debug          switch debug on (off by default)], debug=$enableval)
AC_ARG_ENABLE(opt, [  --enable-opt            switch optimization on (on by default)], opt=$enableval)
AC_ARG_ENABLE(profile, [  --enable-profile        switch optimization on (off by default)], profile=$enableval)
AC_ARG_WITH(mysql,    [  --with-mysql=PATH       full path to MySQL], mysql="$withval", mysql="/usr/local/mysql")
AC_ARG_WITH(mysqlinc, [  --with-mysqlinc=PATH    full path to MySQL includes], mysqlinc="$withval")
AC_ARG_WITH(mysqllib, [  --with-mysqllib=PATH    full path to MySQL libraries], mysqllib="$withval")
AC_ARG_WITH(mysqlbin, [  --with-mysqlbin=PATH    full path to MySQL binaries], mysqlbin="$withval")

AC_SUBST(mysql)
AC_SUBST(mysqlinc)
AC_SUBST(mysqllib)
AC_SUBST(mysqlbin)

AC_MSG_CHECKING(system type)
SYSTEM_TYPE=`${UNAME}`
AC_MSG_RESULT($SYSTEM_TYPE)

STRIP=yes
if test x${SYSTEM_TYPE} = "xIRIX"; then
  STRIP=no
fi

dnl compile defaults
MYCFLAGSADDCC="-O"
MYCFLAGSADDGCC="-O2 -Wstrict-prototypes -Wall"
MYLDFLAGSADDCC="-s"
MYLDFLAGSADDGCC="-s"
if test "$opt" = "no"; then
    MYCFLAGSADDCC=""
    MYCFLAGSADDGCC="-Wstrict-prototypes -Wall"
fi
if test "$debug" = "yes"; then
    MYCFLAGSADDCC="-DDEBUG -g"
    MYCFLAGSADDGCC="-DDEBUG -Wstrict-prototypes -Wall -g"
    MYLDFLAGSADDCC=""
    MYLDFLAGSADDGCC=""
fi
if test "$profile" = "yes"; then
    MYCFLAGSADDCC="${MYCFLAGSADDCC} -p"
    MYCFLAGSADDGCC="${MYCFLAGSADDGCC} -Wstrict-prototypes -Wall -pg"
    MYLDFLAGSADDCC="-p"
    MYLDFLAGSADDGCC="-pg"
fi

dnl system specific compile settings
if test "x${SYSTEM_TYPE}" = "xHP-UX"; then
  MYCFLAGSADDCC="${MYCFLAGSADDCC} -Ae -D_NO_H_ERRNO"
  MYCFLAGSADDGCC="${MYCFLAGSADDGCC} -D_NO_H_ERRNO"
fi
if test "x${SYSTEM_TYPE}" = "xOSF1"; then
  MYCFLAGSADDCC="${MYCFLAGSADDCC} -std -no_intrinsics"
  MYCFLAGSADDGCC="${MYCFLAGSADDGCC}"
fi
dnl newer SunOS/Solaris have different signal() behaviour
if test "x${SYSTEM_TYPE}" = "xSunOS"; then
  MYCFLAGSADDCC="${MYCFLAGSADDCC} -DNEW_SIGNALS"
  MYCFLAGSADDGCC="${MYCFLAGSADDGCC} -DNEW_SIGNALS"
fi
IRIX=no
if test "x${SYSTEM_TYPE}" = "xIRIX"; then
  MYCFLAGSADDCC="${MYCFLAGSADDCC} -std -no_intrinsics"
  MYCFLAGSADDGCC="${MYCFLAGSADDGCC}"
  MYLDFLAGSADDCC=""
  MYLDFLAGSADDGCC=""
  IRIX=yes
fi

dnl assume gcc
MYCFLAGS="${MYCFLAGSADDGCC}"
MYLDFLAGS="${MYLDFLAGSADDGCC}"
if test "x$CC" != "xgcc"; then
  MYCFLAGS="${MYCFLAGSADDCC}"
  MYLDFLAGS="${MYLDFLAGSADDCC}"
fi

dnl Set compiler CFLAGS
CFLAGS=${MYCFLAGS}

dnl Set linker LDFLAGS
LDFLAGS=${MYLDFLAGS}

dnl Find out source code size
LINES=`wc -l *.h *.c | tail -1 | sed 's/total/\ /' | tr -d ' '`
CHARS=`wc -c *.h *.c | tail -1 | sed 's/total/\ /' | tr -d ' '`

dnl export to Makefile
AC_SUBST(SOURCE_LINES)
SOURCE_LINES=${LINES}

dnl export to Makefile
AC_SUBST(SOURCE_CHARS)
SOURCE_CHARS=${CHARS}

dnl Checks for libraries (except IRIX)
if test "${IRIX}" = "no"; then
  AC_CHECK_LIB(crypt,crypt)
  AC_CHECK_LIB(socket,socket)
  AC_CHECK_LIB(nsl,connect)
  AC_CHECK_LIB(dns,gethostbyname)
  AC_CHECK_LIB(dl,dlopen)
fi

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h strings.h crypt.h)

dnl FreeBSD doesn't like ctype.h to be included, why only The Red Devil knows 
if test "x${SYSTEM_TYPE}" != "xFreeBSD"; then  
  AC_CHECK_HEADERS(ctype.h)
else
  echo "" 
  echo "  Disabling ctype.h because of FreeBSD, you can safely" 
  echo "  ignore the implicit declaration of function isxxxxx messages" 
  echo "" 
fi

dnl check for locale
AC_CHECK_HEADERS(locale.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(snprintf)
if test "${ac_cv_func_snprintf}" = "no"; then
  echo ""
  echo "  Your system does not have snprintf, using unsafe vsprintf alternative"
  echo ""
fi
AC_CHECK_FUNCS(vsnprintf)
if test "${ac_cv_func_vsnprintf}" = "no"; then
  echo ""
  echo "  Your system does not have vsnprintf, using unsafe vsprintf alternative"
  echo ""
  exit
fi
AC_CHECK_FUNCS(vsprintf)
if test "${ac_cv_func_vsprintf}" = "no"; then
  echo ""
  echo "  Your system does not have vsprintf. Get a real system!"
  echo ""
  exit
fi
AC_CHECK_FUNCS(vfprintf)
if test "${ac_cv_func_vfprintf}" = "no"; then
  echo ""
  echo "  Your system does not have vfprintf. Get a real system!"
  echo ""
  exit
fi
dnl check for MySQL
if test "$mysql" = ""; then
  if test "$mysqlinc" = ""; then
    if test "$mysqllib" = ""; then
      if test "$mysqllib" = ""; then
        echo ""
        echo "  ERROR: You didn't tell me where MySQL is, use"
        echo "           --with-mysql"
        echo "           --with-mysqlinc"
        echo "           --with-mysqllib"
        echo "           --with-mysqlbin"
        echo "         parameter(s) to configure"
        echo ""
        exit
      fi
    fi
  fi
fi
dnl extract $mysqlinc, $mysqllib and $mysqlbin from $mysql if not given
if test "$mysqlinc" = ""; then
  mysqlinc=$mysql/include/mysql
fi
if test "$mysqllib" = ""; then
  mysqllib=$mysql/lib/mysql
fi
if test "$mysqlbin" = ""; then
  mysqlbin=$mysql/bin
fi
if test ! -r "$mysqlinc/mysql.h"; then
  echo ""
  echo "  ERROR: Your system does not have mysql.h in $mysqlinc"
  echo ""
  exit
fi
CPPFLAGS="${CPPFLAGS} -I$mysqlinc"
if test ! -r "$mysqllib/libmysqlclient.so"; then
  echo ""
  echo "  ERROR: Your system does not libmysqlclient.so in $mysqllib"
  echo ""
  exit
fi
LIBS="${LIBS} -L$mysqllib -lmysqlclient"
if test ! -x "$mysqlbin/mysql"; then
  echo ""
  echo "  ERROR: Your system does not have mysql in $mysqlbin"
  echo ""
  exit
fi
if test ! -x "$mysqlbin/mysqladmin"; then
  echo ""
  echo "  ERROR: Your system does not have mysqladmin in $mysqlbin"
  echo ""
  exit
fi
dnl export $mysqlinc and $mysqllib to Makefile
AC_SUBST(MYSQL_PATH_INC)
AC_SUBST(MYSQL_PATH_LIB)
AC_SUBST(MYSQL_PATH_BIN)
MYSQL_PATH_INC=${mysqlinc}
MYSQL_PATH_LIB=${mysqllib}
MYSQL_PATH_BIN=${mysqlbin}

AC_OUTPUT(Makefile)
AC_MSG_RESULT("")
AC_MSG_RESULT("  Type \"make\" to compile oer+MySQL")
AC_MSG_RESULT("")


