dnl Make sure a recent autoconf is being used
AC_PREREQ(2.52)

dnl Process this file with autoconf to produce a configure script.
AC_INIT(linuxerrors.h)
AC_CONFIG_SRCDIR(../vipsi_vers.cp)
AC_CONFIG_HEADER(config.h)
AC_PREFIX_DEFAULT(/usr)

BINNAME=vipsi

dnl  shtool bootstrap
SHTOOL="\$(TOP)/shtool"
AC_SUBST(SHTOOL)
ac_shtool="./shtool"
T_MD=`$ac_shtool echo -n -e %B`
T_ME=`$ac_shtool echo -n -e %b`
PLATFORM=`${CONFIG_SHELL-/bin/sh} ./config.guess`
PLATFORM=`${CONFIG_SHELL-/bin/sh} ./config.sub $PLATFORM`
VIPSI_VERSION_STR="`$ac_shtool version -l c -d long ../vipsi_vers.cp`"
VIPSI_VERSION_STR_SHORT="`$ac_shtool version -l c -d short ../vipsi_vers.cp`"
AC_SUBST(VIPSI_VERSION_STR)
AC_SUBST(VIPSI_VERSION_STR_SHORT)

dnl 
echo "Configuring ${T_MD}VIPSI${T_ME} (Shell), Version ${T_MD}${VIPSI_VERSION_STR}${T_ME}"
echo "Copyright (c) 1990-2003 Kio, All Rights Reserved."
echo "Platform: ${T_MD}${PLATFORM}${T_ME}"


dnl Checks for programs.
AC_CONFIGURE_PART(Build Tools)
AC_PROG_CC
AC_PROG_CPP
AC_SET_MAKE
AC_PATH_PROG(RM, rm)
AC_PATH_PROG(SH, sh)
AC_PATH_PROG(TAR, tar)
AC_PATH_PROG(STRIP, strip, true)
AC_PROG_INSTALL


dnl enable gnu extensions for NAN

if test "$GCC" = "yes"; then
        AC_DEFINE_UNQUOTED(_GNU_SOURCE, 1, enable GNU extensions)
fi


dnl Various OS checks that apparently set required flags
AC_CONFIGURE_PART(OS Dependent checks)
AC_CANONICAL_SYSTEM
AC_ISC_POSIX
AC_MINIX
AC_SYS_LARGEFILE
AC_SYS_LONG_FILE_NAMES
AC_C_BIGENDIAN(AC_DEFINE(_BIG_ENDIAN, 1, Big Endian System))


dnl Checks for libraries.
AC_CONFIGURE_PART(Libraries)
AC_CHECK_LIB(m, modf)


dnl find libkios
AC_CONFIGURE_PART(Libkios)
AC_ARG_ENABLE(libkios, AC_HELP_STRING([--enable-libkios[=PATH]],
                            [Use the installed libkios library (default=no).]),
  [if test "$withval" = "yes"; then
      AC_CHECK_LIB(kios)
      VIPSI_ADDTO(LDFLAGS,-lkios)
    else
      VIPSI_ADDTO(LDFLAGS,-lkios)
    fi

    if test -n "$enableval" ; then
	LIBKIO_ACLOCAL=$enableval/aclocal-libkios.m4 ;
    else
	LIBKIO_ACLOCAL=/usr/lib/libkios/aclocal-libkios.m4  ;
    fi
    LOCAL_LIBKIO=no
])dnl


dnl configure libkios from here
dnl

AC_CONFIG_SUBDIRS(../../libraries/)

dnl if test ! "$LOCAL_LIBKIO" = "yes" ; then
dnl 	
dnl 	VIPSI_ADDTO(LDFLAGS, -l../../libraries/.libs/libkios.a)
dnl fi


dnl Checks for header files.
AC_CONFIGURE_PART(Header Files)

AC_STDC_HEADERS
AC_HAVE_HEADERS(stdio.h stdlib.h string.h ctype.h dnl 
                errno.h limits.h unistd.h fcntl.h dnl
                sys/stat.h sys/types.h sys/utsname.h dnl
		time.h fnmatch.h stdarg.h string.h dnl
		assert.h)


dnl Checks for typedefs, structures, and compiler characteristics.
VIPSI_ADDTO(CFLAGS,-fsigned-char -Wno-multichar -W -Wall -Wundef -Wcast-align)


dnl Checks for library functions.


dnl Checks for Apache support
AC_CONFIGURE_PART(Apache DSO support)
AC_MSG_CHECKING(for Apache module support via DSO through APXS)
AC_ARG_WITH(apxs,
[  --with-apxs[=FILE]      Build shared Apache module.  FILE is the optional
                          pathname to the Apache apxs tool; defaults to "apxs".],
[
    if test "$withval" = "yes"; then
      APXS=apxs
    else
      APXS="$withval"
    fi
])

if test -n "$APXS"; then
    BINNAME=vipsi.so
    INSTALL="$APXS -i -a -n vipsi $BINNAME"

    APACHE_MODULE_OBJ=mod_vipsi.o
    CC=$APXS
    CFLAGS=-Wc,\"$CFLAGS\"

    AC_DEFINE_UNQUOTED(_APACHE_MODULE, 1, compiling as an Apache module)
    AC_SUBST(INSTALL)
    AC_MSG_RESULT(found at $APXS)
else
    AC_MSG_RESULT(no)
fi

AC_SUBST(BINNAME)
AC_SUBST(APXS)
AC_SUBST(APACHE_MODULE_OBJ)
AC_SUBST(AP_INC)

dnl Enable debugging and warnings

VIPSI_REMOVEFROM(CFLAGS,-g)

AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],
                            [enable debugging and compile time warnings (default is NO). May be given options from 0 to 3.]),
  [VIPSI_ADDTO(CFLAGS,-g)
   if test "$GCC" = "yes"; then
     VIPSI_ADDTO(CFLAGS,-Wall)
   elif test "$AIX_XLC" = "yes"; then
     VIPSI_ADDTO(CFLAGS,-qfullpath)
   fi
   STRIP="true"
   if test -n $enable_debug ; then
	AC_DEFINE_UNQUOTED(GLOB_LOG,  $enable_debug, Log Level)
	AC_DEFINE_UNQUOTED(GLOB_SAFE, $enable_debug, Warn Level)
    fi
])dnl



AC_CONFIGURE_PART(Finishing)


case $PLATFORM in
        *-*-linux* )
        AC_DEFINE(_UNIX, 1, Unix)
        AC_DEFINE(_LINUX, 1, Linux)
        case $PLATFORM in 
                *-suse-* )
                AC_DEFINE(_SUSE, 1, SuSE Linux)
                ;;
                *-redhat-* )
                AC_DEFINE(_REDHAT, 1, RedHat Linux)
                ;;
                *-debian-* )
                AC_DEFINE(_DEBIAN, 1, Debian Linux)
                ;;
                *-yellowdog-* )
                AC_DEFINE(_YELLOWDOG, 1, Yellowdog Linux)
                ;;
                * )
                ;;
        esac
        ;;
        *-apple-darwin* )
        AC_DEFINE(_UNIX, 1, Unix)        
        AC_DEFINE(_BSD, 1, MacOS X)      
        AC_DEFINE(_MACOSX, 1, MacOS X)   
        ;;
        *-beos )
        AC_DEFINE(_BEOS, 1, BeOS)
        ;;
        *-solaris* )
        AC_DEFINE(_SOLARIS, 1, Sun Solaris)
        AC_DEFINE(_UNIX, 1, Unix)        
        ;;
        *openbsd* | *freebsd* | *netbsd* )
        AC_DEFINE(_UNIX, 1, Unix)
        AC_DEFINE(_BSD, 1, BSD)
        case $PLATFORM in 
                *openbsd* )
                AC_DEFINE(_OPENBSD, 1, OpenBSD)
                ;;
                *freebsd* )
                AC_DEFINE(_FREEBSD, 1, FreeBSD)
                ;;
                *netbsd* )
                AC_DEFINE(_NETBSD, 1, NetBSD)
                ;;
        esac
        ;;
        alpha-pc-interix | i386-pc-interix | alpha-dec-winnt3.5 )
        AC_DEFINE(_WINDOWS, 1, MS Windows)
        ;;
        * )
        ;;
esac


case $PLATFORM in
        i*86-* | ia64-* )
        AC_DEFINE(_I386,1, Intel)
        ;;
        alpha* )
        AC_DEFINE(_ALPHA,1, Alpha processor)
        ;;
        sparc* )
        AC_DEFINE(_SPARC,1, Sparc processor)
        ;;
        powerpc-* )
        AC_DEFINE(_POWERPC,1, Motorola PPC)
        ;;
        m68k-* )
        AC_DEFINE(_M68K,1, Motorola 68k)
        ;;
        x86_64-* )
        AC_DEFINE(_I386x64,1, AMD64 Opteron (preliminary))
        ;;
        * )
        ;;
esac


AC_DEFINE(CUSTOMERRORS, "verrors.h", custom error messages)
AC_DEFINE(MATCH_INVALID_UTF8_PATHS, 1, special glue to match invalid utf-8 file names)
AC_OUTPUT(Makefile)
