dnl -*- mode: m4; -*-
dnl Process this file with autoconf to produce a configure script.

dnl Autoconf boilerplate/
AC_INIT(src/winscard.h)
AC_CONFIG_AUX_DIR(build)

dnl Automake boilerplate.
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE(pcsc-lite, 0.8.7)
##AM_CONFIG_HEADER(config.h)

dnl Options.
AC_DISABLE_FAST_INSTALL
##AC_DISABLE_SHARED
AM_MAINTAINER_MODE

dnl Select OS specific versions of source files.
case "${build_os}" in
 linux-gnu)	arch=linux ;;
 *bsd)		arch=bsd ;;
 darwin)	arch=darwin ;;
 solaris*)	arch=solaris ;;
 *) AC_MSG_ERROR([Operating system ${build_os} not supported]) ;;
esac
AM_CONDITIONAL(PCSC_ARCH_LINUX, test x$arch = xlinux)
AM_CONDITIONAL(PCSC_ARCH_BSD, test x$arch = xbsd)
AM_CONDITIONAL(PCSC_ARCH_OSX, test x$arch = xdarwin)
AM_CONDITIONAL(PCSC_ARCH_SOLARIS, test x$arch = xsolaris)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_LN_S
AM_PROG_LEX

dnl Special check for pthread support.
ACX_PTHREAD(
[
 AC_DEFINE(HAVE_PTHREAD,1,
  [Define if you have POSIX threads libraries and header files.])
], [
 AC_MSG_ERROR([POSIX thread support required])
])
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
CC="$PTHREAD_CC"

dnl Add libtool support.
AM_PROG_LIBTOOL
dnl Automatically update the libtool script if it becomes out-of-date.
AC_SUBST(LIBTOOL_DEPS)

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

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

dnl Checks for library functions.
AC_FUNC_MMAP
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(gettimeofday strdup strtoul)

dnl Setup dist stuff
AC_SUBST(ac_aux_dir)

dnl Enable/disable debugging messages.
AC_ARG_ENABLE(usb,
[  --enable-usb            enable USB hotplug support (Linux only)],
[
 case "${enableval}" in
  yes)
   case "${build_os}" in
    linux-gnu)	usb=true ;;
    *) AC_MSG_WARN([USB hotplug support only available on Linux])
		usb=false
     ;;
   esac
   ;;
  no)	usb=false ;;
  *) AC_MSG_ERROR([bad value ${withval} for --enable-usb]) ;;
 esac
], [
 usb=false
])
AM_CONDITIONAL(PCSC_LINUX_USB, test x$usb = xtrue)

dnl Enable/disable debugging messages.
AC_DEFUN(PCSC_ENABLE_DEBUG_GENERIC,
 [AC_DEFINE(PCSC_DEBUG, 1, [Enable generic pcsc-lite debugging messages.])])
AC_DEFUN(PCSC_ENABLE_DEBUG_ATR,
 [AC_DEFINE(ATR_DEBUG, 1, [Enable pcsc-lite ATR debugging messages.])])
AC_ARG_ENABLE(debug,
[  --enable-debug          enable debug messages from pcscd.],
[case "${enableval}" in
  yes)	debug_gen=true ;;
  no)	debug_gen=false ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac], [
 debug_gen=false
])
AC_ARG_ENABLE(debug,
[  --enable-debug-atr      enable ATR debug messages from pcscd.],
[case "${enableval}" in
  yes)	debug_atr=true ;;
  no)	debug_atr=false ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug-atr]) ;;
esac], [
 debug_atr=false
])
AM_CONDITIONAL(PCSC_DEBUG, test x${debug_gen} = xtrue)
AM_CONDITIONAL(ATR_DEBUG, test x${debug_atr} = xtrue)
if test x${debug_gen} = xtrue ; then
  PCSC_ENABLE_DEBUG_GENERIC
fi
if test x${debug_atr} = xtrue ; then
  PCSC_ENABLE_DEBUG_ATR
fi

dnl Write Makefiles.
AC_OUTPUT(Makefile src/Makefile src/utils/Makefile etc/Makefile)
