#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(sispmctl, 3.1, mondrian.nuessle@googlemail.com)
AC_CONFIG_AUX_DIR(admin)
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(sispmctl,3.1)

# Checks for programs.
AC_PROG_CC

# check configure options
AC_ARG_ENABLE(webless,
[  --enable-webless  compile version of sispmctl without internal webserver],
[WEBLESS=true])
AC_SUBST(WEBLESS)
AM_CONDITIONAL(WEBLESSCOND, test x$WEBLESS = xtrue)

AC_ARG_WITH(webdir,[  --with-webdir=DIR use DIR to look for html files for the web-interface], [WEBDIR="$withval"])
AC_SUBST(WEBDIR)

AC_ARG_WITH(bindaddr,[  --with-bindaddr=IP bind listening socket to IP], [BINDADDR="$withval"])
AC_SUBST(BINDADDR)


# Checks for libraries.
dnl check for libusb-config
AC_PATH_PROG(HAVELIBUSB, libusb-config, $PATH)

if test -e "$HAVELIBUSB"; then
   dnl   LIBUSB_CFLAGS=`$HAVELIBUSB --cflags`
   LIBUSB_LIBS=`$HAVELIBUSB --libs`
   CFLAGS="$CFLAGS $LIBUSB_CFLAGS"
   LIBS="$LIBS $LIBUSB_LIBS"
else
   AC_MSG_ERROR([*** libusb-config not found. You need a working libusb installation. Version >=0.1.7])
fi

dnl check for version of libusb
AC_MSG_CHECKING([if libusb version is >= 0.1.9])
libusb_version_needed="1009"
libusb_version=`$HAVELIBUSB --version | sed -e "s/libusb //" | awk 'BEGIN { FS = "."; } { printf "%d", ($''1 * 1000 + $''2) * 1000 + $''3;}'`

if test $libusb_version -lt $libusb_version_needed; then
   AC_MSG_RESULT(no)
   AC_MSG_ERROR([*** libusb is too old ($libusb_version). You need a libusb installation newer or equal to 0.1.9.])
else
   AC_MSG_RESULT(yes)
fi

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h net/ethernet.h sys/ethernet.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_TYPE_UID_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_CHECK_FUNCS([memset strchr strdup strerror strncasecmp strrchr])

AC_CHECK_DECL(MSG_NOSIGNAL)
AC_CHECK_FUNC(nanosleep, [true], [AC_CHECK_LIB(rt, nanosleep)])
AC_CHECK_FUNC(inet_pton, [true], [AC_CHECK_LIB(nsl, inet_pton)])
AC_CHECK_FUNC(socket, [true], [AC_CHECK_LIB(socket, socket)])

AC_CONFIG_FILES([Makefile
                 src/Makefile])
AM_CONFIG_HEADER(config.h)
AC_OUTPUT
echo ""
echo "Good - your configure finished. Start make now"
echo ""
