# This configure.in is based off of the one that glib uses

# require atleast autoconf 2.13
AC_PREREQ(2.13)

# init autoconf (and check for presence of usb.c)
AC_INIT(src/usb.c)

# Save this value here, since automake will set cflags later
cflags_set=${CFLAGS+set}

dnl we need to AC_DIVERT_PUSH/AC_DIVERT_POP these variable definitions so they
dnl are available for $ac_help expansion (don't we all *love* autoconf?)
ifdef([AC_DIVERSION_NOTICE], [AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)],
      [AC_DIVERT_PUSH(NOTICE)])

#
# The following version number definitions apply to openusb
# as a whole, so if changes occoured in any of them, they are all
# treated with the same interface and binary age.
#
# Making releases:
# if only bugfixes get integrated, which also means only internal changes
#   LIBOPENUSB_MICRO_VERSION += 1;
#   LIBOPENUSB_INTERFACE_AGE += 1;
#   LIBOPENUSB_BINARY_AGE += 1;
#
# else if any functions have been added,
# 	LIBOPENUSB_MICRO_VERSION += 1;
#	set LIBOPENUSB_INTERFACE_AGE to 0.
#
# else if backwards compatibility has been broken,
#	 set LIBOPENUSB_BINARY_AGE _and_ LIBOPENUSB_INTERFACE_AGE to 0.
#
# see libtool manual section 6.3 for version rules.
#
LIBOPENUSB_MAJOR_VERSION=1
LIBOPENUSB_MINOR_VERSION=1
LIBOPENUSB_MICRO_VERSION=11
LIBOPENUSB_INTERFACE_AGE=11
LIBOPENUSB_BINARY_AGE=11
LIBOPENUSB_VERSION=$LIBOPENUSB_MAJOR_VERSION.$LIBOPENUSB_MINOR_VERSION.$LIBOPENUSB_MICRO_VERSION
dnl
AC_DIVERT_POP()dnl

AC_SUBST(LIBOPENUSB_MAJOR_VERSION)
AC_SUBST(LIBOPENUSB_MINOR_VERSION)
AC_SUBST(LIBOPENUSB_MICRO_VERSION)
AC_SUBST(LIBOPENUSB_VERSION)
AC_SUBST(LIBOPENUSB_INTERFACE_AGE)
AC_SUBST(LIBOPENUSB_BINARY_AGE)

# libtool versioning
# JR: Add support for MICRO_VERSION to be alpha-numeric. This will make
# lt_current use the numeric part of MICRO_VERSION
# see libtool manual section 6.3 for version rules.

LIBOPENUSB_MICRO_VERSION_NUM=`echo $LIBOPENUSB_MICRO_VERSION | sed 's/[[a-zA-Z]]//g'`
LT_RELEASE=$LIBOPENUSB_MAJOR_VERSION.$LIBOPENUSB_MINOR_VERSION
LT_CURRENT=`expr $LIBOPENUSB_MICRO_VERSION_NUM - $LIBOPENUSB_INTERFACE_AGE`
LT_REVISION=$LIBOPENUSB_INTERFACE_AGE
LT_AGE=`expr $LIBOPENUSB_BINARY_AGE - $LIBOPENUSB_INTERFACE_AGE`
AC_SUBST(LT_RELEASE)
AC_SUBST(LT_CURRENT)
AC_SUBST(LT_REVISION)
AC_SUBST(LT_AGE)

VERSION=$LIBOPENUSB_VERSION
PACKAGE=libopenusb

AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)

# Specify a configuration file
AM_CONFIG_HEADER(config.h)

AC_DEFINE_UNQUOTED(LIBOPENUSB_MAJOR_VERSION, $LIBOPENUSB_MAJOR_VERSION,
				   [Major version])
AC_DEFINE_UNQUOTED(LIBOPENUSB_MINOR_VERSION, $LIBOPENUSB_MINOR_VERSION,
				   [Minor version])
AC_DEFINE_UNQUOTED(LIBOPENUSB_MICRO_VERSION, $LIBOPENUSB_MICRO_VERSION,
				   [Micro version])
AC_DEFINE_UNQUOTED(LIBOPENUSB_INTERFACE_AGE, $LIBOPENUSB_INTERFACE_AGE,
				   [Binary interface age])
AC_DEFINE_UNQUOTED(LIBOPENUSB_BINARY_AGE, $LIBOPENUSB_BINARY_AGE,
				   [Binary age])

dnl Initialize libtool
AM_PROG_LIBTOOL


dnl Initialize maintainer mode
AM_MAINTAINER_MODE

#AC_CANONICAL_HOST

AC_MSG_CHECKING(for what USB OS support)
case $host in
  *-linux*)
    AC_DEFINE(LINUX_API, 1, [Linux])
    AC_SUBST(LINUX_API)
    os_support=linux
    AC_MSG_RESULT(Linux)
    OSLIBS=""
    ;;
  *-freebsd*|*-openbsd*|*-netbsd*)
    AC_DEFINE(BSD_API, 1, [BSD system])
    AC_SUBST(BSD_API)
    os_support=bsd
    AC_MSG_RESULT(FreeBSD, OpenBSD and/or NetBSD)
    OSLIBS=""
    ;;
  *-darwin*)
    AC_DEFINE(DARWIN_API, 1, [Darwin])
    AC_SUBST(DARWIN_API)
    os_support=darwin
    AC_MSG_RESULT(Darwin and/or MacOS 10)
    OSLIBS="-lIOKit"
    ;;
  *-solaris*)
   AC_DEFINE(SUNOS_API, 1, [Solaris])
   AC_SUBST(SUNOS_API)
   os_support=sunos
   AC_MSG_RESULT(Solaris)
   OSLIBS=""
   ;;
esac

AM_CONDITIONAL(LINUX_API, test "$os_support" = "linux")
AM_CONDITIONAL(BSD_API, test "$os_support" = "bsd")
AM_CONDITIONAL(DARWIN_API, test "$os_support" = "darwin")
AM_CONDITIONAL(SUNOS_API, test "$os_support" = "sunos")

AC_SUBST(OSLIBS)

dnl figure debugging default, prior to $ac_help setup
dnl
ifdef([AC_DIVERSION_NOTICE], [AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)],
      [AC_DIVERT_PUSH(NOTICE)])
if test `expr $LIBOPENUSB_MINOR_VERSION \% 2` = 1 ; then
  debug_default=yes
else
  debug_default=minimum
fi
AC_DIVERT_POP()dnl

dnl declare --enable-* args and collect ac_help strings
AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug=[no/minimum/yes]],
	[turn on debugging [default=$debug_default]]),,enable_debug=$debug_default)

if test "x$enable_debug" = "xyes"; then
  test "$cflags_set" = set || CFLAGS="$CFLAGS -g"
fi

# Checks for programs.
AC_PROG_CC
AM_PROG_CC_STDC
AC_PROG_INSTALL

changequote(,)dnl
if test "x$GCC" = "xyes"; then
  case " $CFLAGS " in
  *[\ \	]-Wall[\ \	]*) ;;
  *) CFLAGS="$CFLAGS -Wall" ;;
  esac
fi
changequote([,])dnl

# Checks for header files.
AC_HEADER_STDC

# Checks for library functions.
AC_FUNC_VPRINTF

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(long long)

AC_C_CONST

dnl header file checks
AC_CHECK_HEADERS(limits.h, AC_DEFINE(HAVE_LIMITS_H))
AC_CHECK_HEADERS(unistd.h, AC_DEFINE(HAVE_UNISTD_H))
AC_CHECK_HEADERS(values.h, AC_DEFINE(HAVE_VALUES_H))

# Check for some functions
AC_CHECK_FUNCS(memmove)


dnl
dnl Check for glib, gthread, dbus, dbus-glib, hal, libusb
dnl
PKG_PROG_PKG_CONFIG
if test "$os_support" = "darwin"; then

   PKG_CHECK_MODULES(LIBUSB, libusb, HAVE_LIBUSB="yes")
   AC_SUBST(LIBUSB_CFLAGS)
   AC_SUBST(LIBUSB_LIBS)

elif test "$os_support" = "linux"; then

   PKG_CHECK_MODULES(LIBUSB, libusb, HAVE_LIBUSB="yes")
   AC_SUBST(LIBUSB_CFLAGS)
   AC_SUBST(LIBUSB_LIBS)
   
   PKG_CHECK_MODULES(UDEV, libudev, HAVE_UDEV="yes")
   AC_SUBST(UDEV_CFLAGS)
   AC_SUBST(UDEV_LIBS)

else

   PKG_CHECK_MODULES(GLIB, glib-2.0, HAVE_GLIB="yes")
   AC_SUBST(GLIB_CFLAGS)
   AC_SUBST(GLIB_LIBS)

   PKG_CHECK_MODULES(GTHREADS, gthread-2.0, HAVE_GTHREADS="yes")
   AC_SUBST(GTHREADS_CFLAGS)
   AC_SUBST(GTHREADS_LIBS)

   PKG_CHECK_MODULES(DBUS, dbus-1, HAVE_DBUS="yes")
   AC_SUBST(DBUS_CFLAGS)
   AC_SUBST(DBUS_LIBS)

   PKG_CHECK_MODULES(DBUSGLIB, dbus-glib-1, HAVE_DBUSGLIB="yes")
   AC_SUBST(DBUSGLIB_CFLAGS)
   AC_SUBST(DBUSGLIB_LIBS)

   PKG_CHECK_MODULES(HAL, hal, HAVE_HAL="yes")
   AC_SUBST(HAL_CFLAGS)
   AC_SUBST(HAL_LIBS)

   PKG_CHECK_MODULES(LIBUSB, libusb, HAVE_LIBUSB="yes")
   AC_SUBST(LIBUSB_CFLAGS)
   AC_SUBST(LIBUSB_LIBS)
fi


if test "$os_support" = "bsd"; then
  AC_MSG_CHECKING(if dev/usb/usb.h uses new naming convention)
  AC_TRY_COMPILE(
    [#include <dev/usb/usb.h>],
    [int main(void)
     {
       int a = ((struct usb_ctl_request *)0L)->ucr_addr;
       return 0;
     }],
    [AC_MSG_RESULT(yes)],
    [AC_MSG_RESULT(no)
    AC_DEFINE(HAVE_OLD_DEV_USB_USB_H, 1, [Old dev/usb/usb.h])])
fi

# Check if the user wants to enable documentation
AC_MSG_CHECKING(if we should build the documentation)
AC_ARG_ENABLE(build-docs, AC_HELP_STRING([--disable-build-docs],
	[turn off building documentation]),
	[ test "$enableval" != no && build_docs=yes || build_docs=no ],
	build_docs=auto)
AC_MSG_RESULT($build_docs)

if test "$build_docs" != "no"; then
  AC_CHECK_PROG(JADE, jade, jade)
  if test  -z "$JADE"; then
    AC_CHECK_PROG(JADE, openjade, openjade)
  fi
fi

dnl Not quite ready for primetime --jerdfelt
dnl AC_CHECK_PROG(DVIPS, dvips, dvips, false)
dnl if test "$DVIPS" = "false"; then
dnl   unset BUILD_DOCS
dnl fi

if test "$build_docs" != "no" -a -z "$JADE"; then
  AC_MSG_WARN(Documentation build components not installed. sgml docs will not be built)
  build_docs="no"
fi

AM_CONDITIONAL(BUILD_DOCS, test "$build_docs" != "no")

AC_OUTPUT([
  Makefile
  doc/Makefile
  src/Makefile
  tests/Makefile
  src/openusb.h
  openusb.spec
  openusb-config
  README
  INSTALL.openusb
],[chmod +x openusb-config])

