# 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(usb.c)

AH_TOP(
#undef LINUX_API
#undef BSD_API
#undef DARWIN_API

#undef HAVE_OLD_DEV_USB_USB_H

#undef HAVE_LIMITS_H
#undef HAVE_UNISTD_H
#undef HAVE_VALUES_H

#undef LIBUSB_MAJOR_VERSION
#undef LIBUSB_MINOR_VERSION
#undef LIBUSB_MICRO_VERSION
#undef LIBUSB_INTERFACE_AGE
#undef LIBUSB_BINARY_AGE
)

# 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 libusb
# as a whole, so if changes occoured in any of them, they are all
# treated with the same interface and binary age.
#
# Making releases:
#   LIBUSB_MICRO_VERSION += 1;
#   LIBUSB_INTERFACE_AGE += 1;
#   LIBUSB_BINARY_AGE += 1;
# if any functions have been added, set LIBUSB_INTERFACE_AGE to 0.
# if backwards compatibility has been broken,
# set LIBUSB_BINARY_AGE _and_ LIBUSB_INTERFACE_AGE to 0.
#
LIBUSB_MAJOR_VERSION=0
LIBUSB_MINOR_VERSION=1
LIBUSB_MICRO_VERSION=8
LIBUSB_INTERFACE_AGE=0
LIBUSB_BINARY_AGE=4
LIBUSB_VERSION=$LIBUSB_MAJOR_VERSION.$LIBUSB_MINOR_VERSION.$LIBUSB_MICRO_VERSION
dnl
AC_DIVERT_POP()dnl

AC_SUBST(LIBUSB_MAJOR_VERSION)
AC_SUBST(LIBUSB_MINOR_VERSION)
AC_SUBST(LIBUSB_MICRO_VERSION)
AC_SUBST(LIBUSB_VERSION)
AC_SUBST(LIBUSB_INTERFACE_AGE)
AC_SUBST(LIBUSB_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

LIBUSB_MICRO_VERSION_NUM=`echo $LIBUSB_MICRO_VERSION | sed 's/[[a-zA-Z]]//g'`
LT_RELEASE=$LIBUSB_MAJOR_VERSION.$LIBUSB_MINOR_VERSION
LT_CURRENT=`expr $LIBUSB_MICRO_VERSION_NUM - $LIBUSB_INTERFACE_AGE`
LT_REVISION=$LIBUSB_INTERFACE_AGE
LT_AGE=`expr $LIBUSB_BINARY_AGE - $LIBUSB_INTERFACE_AGE`
AC_SUBST(LT_RELEASE)
AC_SUBST(LT_CURRENT)
AC_SUBST(LT_REVISION)
AC_SUBST(LT_AGE)

VERSION=$LIBUSB_VERSION
PACKAGE=libusb

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

# Specify a configuration file
AM_CONFIG_HEADER(config.h)

AC_DEFINE_UNQUOTED(LIBUSB_MAJOR_VERSION, $LIBUSB_MAJOR_VERSION)
AC_DEFINE_UNQUOTED(LIBUSB_MINOR_VERSION, $LIBUSB_MINOR_VERSION)
AC_DEFINE_UNQUOTED(LIBUSB_MICRO_VERSION, $LIBUSB_MICRO_VERSION)
AC_DEFINE_UNQUOTED(LIBUSB_INTERFACE_AGE, $LIBUSB_INTERFACE_AGE)
AC_DEFINE_UNQUOTED(LIBUSB_BINARY_AGE, $LIBUSB_BINARY_AGE)

dnl Initialize libtool
AM_PROG_LIBTOOL

dnl Initialize maintainer mode
AM_MAINTAINER_MODE

dnl This has already been dragged in by AM_PROG_LIBTOOL
dnl AC_CANONICAL_HOST

LINUX_API=0
DARWIN_API=0
BSD_API=0

AC_MSG_CHECKING(for what USB OS support)
case $host in
  *-linux*)
    AC_DEFINE(LINUX_API, 1)
    AC_DEFINE(BSD_API, 0)
    AC_DEFINE(DARWIN_API, 0)
    LINUX_API=1
    os_support=linux
    AC_MSG_RESULT(Linux)
    OSLIBS=""
    ;;
  *-freebsd*|*-openbsd*|*-netbsd*)
    AC_DEFINE(BSD_API, 1)
    AC_DEFINE(LINUX_API, 0)
    AC_DEFINE(DARWIN_API, 0)
    BSD_API=1
    os_support=bsd
    AC_MSG_RESULT(FreeBSD, OpenBSD and/or NetBSD)
    OSLIBS=""
    ;;
  *-darwin*)
    AC_DEFINE(DARWIN_API, 1) 
    AC_DEFINE(LINUX_API, 0)
    AC_DEFINE(BSD_API, 0)
    DARWIN_API=1
    os_support=darwin
    AC_MSG_RESULT(Darwin and/or MacOS 10)
    OSLIBS="-framework IOKit"
    ;;
  *)
    AC_MSG_RESULT(unknown operating system)
    AC_MSG_ERROR(libusb does not support compiling for $host)
    ;;
esac

AC_SUBST(DARWIN_API)
AC_SUBST(LINUX_API)
AC_SUBST(BSD_API)

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

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 $LIBUSB_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, [  --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_C_CONST

dnl for bytesex stuff
AC_C_BIGENDIAN

if test "$ac_cv_c_bigendian" = "yes"; then
  BIGENDIAN="1"
else
  BIGENDIAN="0"
fi
AC_SUBST(BIGENDIAN)

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

# Check for some functions
AC_CHECK_FUNCS(memmove)

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_UNQUOTED(HAVE_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, [  --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
  libusb.spec
  libusb-config
  tests/Makefile
  README
  INSTALL.libusb
  usb.h
],[chmod +x libusb-config])

