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

AC_PREREQ(2.59)

AC_INIT(GPSBabel, 1.4.3, BUG-REPORT-ADDRESS)
DOCVERSION=`echo $PACKAGE_VERSION`

# 1.4.1 is a tiny tweak to 1.4.0.  Use that doc.
DOCVERSION=1.4.3


# Increase GBBUILD for a new release
GBBUILD=24
# YYYYMMDD, please, if beta, i.e. "-beta20060413"
# PACKAGE_RELEASE="-beta20120114"
# DOCVERSION=development
AC_DEFINE_UNQUOTED(PACKAGE_RELEASE, "$PACKAGE_RELEASE", [Define to the release name of this package.])
AC_SUBST(PACKAGE_RELEASE)



AC_SUBST(DOCVERSION)

GBMAJOR=`echo $PACKAGE_VERSION | (IFS="."; read major minor micro; echo $major)`
GBMINOR=`echo $PACKAGE_VERSION | (IFS="."; read major minor micro; echo $minor)`
GBMICRO=`echo $PACKAGE_VERSION | (IFS="."; read major minor micro; echo $micro)`
AC_SUBST(GBMAJOR)
AC_SUBST(GBMINOR)
AC_SUBST(GBMICRO)
AC_SUBST(GBBUILD)

# AC_CONFIG_SRCDIR([nmea.c])
AC_CONFIG_HEADER([config.h])

dnl Detect the canonical host and target build environment
AC_CANONICAL_HOST
AC_CANONICAL_TARGET

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_EXEEXT
AC_SUBST(EXEEXT)
AC_C_BIGENDIAN

# Checks for libraries.
AC_CHECK_LIB([m], [cos])

AC_ARG_WITH(cet,[  --with-cet=(default,all,minimal)],
             cet="$withval", cet="default")

if test $GCC = yes; then
 CFLAGS="$CFLAGS -Wall"
fi

if test "$cet" = "all"; then
   AC_DEFINE(CET_WANTED, 1, [1 for all character sets])
fi
if test "$cet" = "default"; then
   AC_DEFINE(CET_WANTED, 0, [0 for most-used character sets])
fi

AC_MSG_CHECKING(whether to support shapefiles)
AC_ARG_ENABLE(shapefile,
  [  --enable-shapefile=[(yes)|no]],
  [ enable_shapefile="$enableval"],[enable_shapefile="yes"])
    if test "$enable_shapefile" != "no" ; then
        AC_DEFINE(SHAPELIB_ENABLED, 1, [1 to enable shapefile support])
	AC_MSG_RESULT(yes)
    else
	AC_MSG_RESULT(no)
    fi

AC_MSG_CHECKING(whether to support Palm/OS pdb formats)
AC_ARG_ENABLE(pdb,
  [  --enable-pdb=[(yes)|no]],
  [ enable_pdb="$enableval"],[enable_pdb="yes"])
    if test "$enable_pdb" != "no" ; then
        AC_DEFINE(PDBFMTS_ENABLED, 1, [1 to enable Palm PDB support])
	AC_MSG_RESULT(yes)
    else
	PALM_DB_CMT=#
	AC_MSG_RESULT(no)
    fi
AC_SUBST(PALM_DB_CMT)

AC_MSG_CHECKING(whether to support csv formats)
AC_ARG_ENABLE(csv,
  [  --enable-csv=[(yes)|no]],
  [ enable_csv="$enableval"],[enable_csv="yes"])
    if test "$enable_csv" != "no" ; then
        AC_DEFINE(CSVFMTS_ENABLED, 1, [1 to enable the CSV formats support])
	AC_MSG_RESULT(yes)
    else
	AC_MSG_RESULT(no)
    fi

AC_MSG_CHECKING(whether to support maximum number of formats)
AC_ARG_ENABLE(most,
  [  --enable-most=[(yes)|no]],
  [ enable_most="$enableval"],[enable_most="yes"])
    if test "$enable_most" != "no" ; then
	AC_MSG_RESULT(yes)
        AC_DEFINE(MAXIMAL_ENABLED, 1, [1 to enable as many formats as possible])
	FMTS='$(ALL_FMTS)'
    else
	AC_MSG_RESULT(no)
	FMTS='$(MINIMAL_FMTS)'
    fi
  AC_SUBST(FMTS)

AC_MSG_CHECKING(whether to support filters)
AC_ARG_ENABLE(filters,
  [  --enable-filters=[(yes)|no]],
  [ enable_filters="$enableval"],[enable_filters="yes"])
    if test "$enable_filters" != "no" ; then
	AC_DEFINE(FILTERS_ENABLED, 1, [1 to enable all the filters.])
	AC_MSG_RESULT(yes)
    else
	AC_MSG_RESULT(no)
    fi

AC_MSG_CHECKING(whether to support zlib)
AC_ARG_WITH(zlib, [  --with-zlib=[(included)|system|no]])
  case $with_zlib in 
    "system") 
	AC_CHECK_LIB([z], [gzopen])
	;;
    "no") 
        AC_DEFINE(ZLIB_INHIBITED, 1, [1 to inhibit our use of zlib.])
	AC_MSG_RESULT(no) 
	;;
    *) 	ZLIB="\$(ZLIB)" 
	AC_MSG_RESULT(using included version);;
  esac

case "$target" in
    *-*-mingw32*)
	FILEINFO=fileinfo.o
	if test "$CC" = gcc ; then
		RC=windres
	else
		RC=`echo "$CC" | sed -e 's/gcc$/windres/'`
	fi
	;;
    *)
    	RC=false
	;;
esac
AC_SUBST(FILEINFO)
AC_SUBST(RC)

case "$target" in
    *-*-cygwin* | *-*-mingw32*)

	GBSER=gbser_win.o

	if test "$with_libusb" = no ; then
		AC_MSG_RESULT(USB skipped)
		OSJEEPS=jeeps/gpsusbstub.o
	else
		OSJEEPS=jeeps/gpsusbwin.o
		USB_LIBS="-lsetupapi"
		AC_MSG_CHECKING(for Windows DDK)
		old_LIBS="$LIBS"
		LIBS="$LIBS -lhid"
		AC_TRY_LINK([#include <windows.h>]
			[#include <hidsdi.h>],
			[HANDLE              HidDeviceObject;
			HIDD_ATTRIBUTES    Attributes;
			HidD_GetAttributes(HidDeviceObject, &Attributes);],
			[AC_MSG_RESULT(yes)]
			[AC_DEFINE(HAVE_WDK, 1, [Defined if you have Windows DDK])]
			[USB_LIBS=${USB_LIBS}" -lhid"],
			[AC_MSG_RESULT(no)]
                ) 
                LIBS="$old_LIBS"
	fi
	;;
    *-*-darwin*)
      GBSER=gbser_posix.o
      OSJEEPS="jeeps/gpslibusb.o \
      mac/libusb/darwin.o \
      mac/libusb/descriptors.o \
      mac/libusb/error.o \
      mac/libusb/usb.o "
      USB_LIBS="-framework  IOKit -framework CoreFoundation"
      AC_DEFINE(HAVE_LIBUSB, 1)
      CFLAGS="$CFLAGS -Imac/libusb/"
      ;;
    *)
	GBSER=gbser_posix.o
	AC_MSG_CHECKING(for libusb)
	if test "$with_libusb" = no ; then
		AC_MSG_RESULT(check not done)
		OSJEEPS=jeeps/gpsusbstub.o
	else
		AC_CHECK_PROG(LIBUSBCONFIG, libusb-config, true, false)
	    if test "$LIBUSBCONFIG" = true; then
		OLDFLAGS=$LDFLAGS
		OCFLAGS=$CFLAGS
		LDFLAGS="$LDFLAGS `libusb-config --libs`"
		CFLAGS="$OCFLAGS `libusb-config --cflags`"

		AC_CHECK_LIB([usb], [usb_interrupt_read],
			AC_DEFINE(HAVE_LIBUSB, 1, [Defined if you have libusb])
			[USB_CFLAGS="`libusb-config --cflags`"]
			[USB_LIBS="`libusb-config --libs`"]
#			,[AC_MSG_ERROR([libusb >= 0.1.8 is needed])]
			)
		OSJEEPS=jeeps/gpslibusb.o
		CFLAGS="$OCFLAGS"
	#	LIBS="$LIBS `libusb-config --libs`"
	    else
		OSJEEPS=jeeps/gpsusbstub.o
	    fi
	fi
	;;
esac

AC_SUBST(USB_LIBS)
AC_SUBST(USB_CFLAGS)
AC_SUBST(OSJEEPS)
AC_SUBST(GBSER)
AC_SUBST(ZLIB)

dnl Linux HID support
case "$target" in
	*-linux-*)
	AC_MSG_CHECKING(for Linux HID interface)
	AC_COMPILE_IFELSE([
		#include <sys/ioctl.h>
		#include <linux/types.h>
		#include <linux/hiddev.h>
		#include <linux/hidraw.h>
		void f(void) {
			struct hidraw_devinfo info;
			struct hiddev_field_info finfo;
			struct hiddev_usage_ref_multi urefm;
			struct hiddev_report_info rinfo;
			ioctl(0, HIDIOCGRAWINFO, &info);
			ioctl(0, HIDIOCGFIELDINFO, &finfo);
			ioctl(0, HIDIOCSUSAGES, &urefm);
			ioctl(0, HIDIOCSREPORT, &rinfo);
		}],
		AC_MSG_RESULT(yes)
		AC_DEFINE(HAVE_LINUX_HID, 1, [Defined if you have Linux HID]),
		AC_MSG_RESULT(no))
	;;
esac

AC_MSG_CHECKING(for random stuff to make you feel better)
AC_MSG_RESULT(failed)

AC_ARG_WITH(expathdr,
  [  --with-expathdr[=DIR]   Use this to specify the location of expat.h],
  [ xpathdr="$withval" ], [
	case "$target" in
	*-*-darwin6*|*-*-darwin7*|*-*-darwin8*)
		# Restrict test to OS/X 10.4 and earlier.  Leopard (10.5) 
		# provides expat and in a sensible location.
		if test -f /sw/include/expat.h ; then
			xpathdr=/sw/include/
		fi
		;;
	*-gentoo-freebsd*)
		;;
	*-*-freebsd*)
 		if test -f /usr/local/include/expat.h ; then
			xpathdr=/usr/local/include
		fi
		;;
	*) 	;;
	esac
]
)

if test "x-$xpathdr" != "x-" ; then
	CFLAGS="$CFLAGS -I$xpathdr"
fi

AC_MSG_CHECKING(for libexpat)
AC_ARG_WITH(libexpat,
  [  --with-libexpat[=DIR]   Use this to specify expat library .],
  [ 
    # If the developer specified a reference
    # to a FILE and not a directory, assume they are a highly
    # trained professional that has specified a .a to to be used.
    if test -f $withval ; then
      EXPAT_LIB=$withval
    else
      CFLAGS="$CFLAGS -L$withval"
      EXPAT_LIB="-L$withval -lexpat"
    fi
  ], [
	case "$target" in
	*-*-darwin6*|*-*-darwin7*|*-*-darwin8*)
		# Restrict test to OS/X 10.4 and earlier.  Leopard (10.5) 
		# provides expat and in a sensible location.
		if test -f /sw/lib/libexpat.a ; then
			# libexpat installed via fink
			EXPAT_LIB=/sw/lib/libexpat.a
			LIBS="$LIBS -L/sw/lib"
		fi
		if test -f /opt/local/lib/libexpat.a ; then
			# libexpat installed via macports
			EXPAT_LIB=/opt/local/lib/libexpat.a
			LIBS="$LIBS -L/opt/local/lib"
		fi
		if test -f /usr/local/lib/libexpat.a ; then
			# libexpat installed from source
			EXPAT_LIB=/usr/local/lib/libexpat.a
			LIBS="$LIBS -L/usr/local/lib"
		fi				
		;;
	*-gentoo-freebsd*)
		EXPAT_LIB=-lexpa
		;;
	*-*-freebsd*)
		if test -f /usr/local/lib/libexpat.a ; then
			EXPAT_LIB=/usr/local/lib/libexpat.a
		fi
		;;
	esac
  ]
)

if ! test "x${EXPAT_LIB}" = x; then
  AC_MSG_RESULT($EXPAT_LIB)
  AC_DEFINE(HAVE_LIBEXPAT, 1, [Defined if you have libexpat])
else
  AC_MSG_RESULT(try to autodetect)
  AC_CHECK_LIB([expat], [XML_ParserCreate], 
    AC_DEFINE(HAVE_LIBEXPAT, 1, [Defined if you have libexpat])
    EXPAT_LIB=-lexpat
  )
fi
AC_SUBST(EXPAT_LIB)

AC_MSG_CHECKING(for efence)
AC_ARG_ENABLE(efence,
  [  --enable-efence=[yes|(no)]],
  [ if test "$enable_efence" != "no" ; then
  	EFENCE_LIB=-lefence
	GPSBABEL_DEBUG=gpsbabel-debug
	INSTALL_DEBUG=install-debug
    fi])
AC_SUBST(EFENCE_LIB)
AC_SUBST(GPSBABEL_DEBUG)
AC_SUBST(INSTALL_DEBUG)
AC_MSG_RESULT($EFENCE_LIB)

AC_MSG_CHECKING(for docdir)
AC_ARG_WITH(doc,[  --with-doc=DIR        Path where the documentation will be stored.],
             DOCDIR="$withval", DOCDIR="../babelweb/")
AC_SUBST(DOCDIR)

# Checks for header files.
# AC_HEADER_STDC

# AC_CHECK_HEADERS([fcntl.h inttypes.h libintl.h limits.h malloc.h stddef.h stdint.h stdlib.h string.h strings.h sys/ioctl.h sys/time.h termios.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
# AC_C_CONST
# AC_C_INLINE
# AC_TYPE_OFF_T
# AC_TYPE_SIZE_T
# AC_HEADER_TIME
# AC_STRUCT_TM
# AC_STRUCT_TIMEZONE

# Checks for library functions.
# AC_FUNC_MALLOC
# AC_FUNC_MEMCMP
# AC_FUNC_MKTIME
# AC_FUNC_REALLOC
# AC_FUNC_SELECT_ARGTYPES
# AC_FUNC_STRFTIME
# AC_FUNC_STRTOD
# AC_FUNC_VPRINTF
# AC_CHECK_FUNCS([atexit floor localtime_r memmove memset pow select sqrt strchr strcspn strdup strerror strncasecmp strrchr strspn strstr strtol strtoul])
AC_CHECK_FUNCS([nanosleep sleep uname glob])

#
# Checks for how the system handles va_list
# paul.bromiley@man.ac.uk
#
dnl **** Check for va_copy ****
AC_CACHE_CHECK([for va_copy], ac_cv_c_va_copy,
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdarg.h>]], [[va_list ap1, ap2;
    va_copy(ap1,ap2);
   ]])],[ac_cv_c_va_copy="yes"],[ac_cv_c_va_copy="no"])
 )
if test "$ac_cv_c_va_copy" = "yes"
then
    AC_DEFINE(HAVE_VA_COPY, 1, [Define if we have va_copy])
fi
AC_CACHE_CHECK([for __va_copy], ac_cv_c___va_copy,
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdarg.h>]], [[va_list ap1, ap2;
    __va_copy(ap1,ap2);
   ]])],[ac_cv_c___va_copy="yes"],[ac_cv_c___va_copy="no"])
 )
if test "$ac_cv_c___va_copy" = "yes"
then
    AC_DEFINE(HAVE___VA_COPY, 1, [Define if we have __va_copy])
fi

#
# Does this platform require array notation to assign to a va_list?
#
AC_MSG_CHECKING(va_list assignments need array notation)
AC_CACHE_VAL(ac_cv_valistisarray,
        [AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdlib.h>
                    #include <stdarg.h>
                    void foo(int i, ...) {
                       va_list ap1, ap2;
                       va_start(ap1, i);
                       ap2 = ap1;
                       if (va_arg(ap2, int) != 123 || va_arg(ap1, int) != 123) { exit(1); }
                       va_end(ap1); va_end(ap2);
                    }
                    int main()
                    { foo(0, 123); return(0); }]])],[ac_cv_valistisarray=false],[ac_cv_valistisarray=true],[ac_cv_valistisarray=false])])
                                            
if test "$ac_cv_valistisarray" = true ; then
       AC_DEFINE(HAVE_VA_LIST_AS_ARRAY, 1, [Define as 1 if your va_list type is an array])
       AC_MSG_RESULT(yes)
else
       AC_MSG_RESULT(no)
fi
AC_CONFIG_FILES([Makefile gbversion.h gui/setup.iss gui/makelinuxdist.sh xmldoc/makedoc tools/mkcapabilities win32/gpsbabel.rc jeeps/Makefile shapelib/Makefile zlib/empty])
AC_OUTPUT

