dnl Autoconfigure input file for apcupsd (derived from fetchmail-4.7.4).
dnl
dnl Copyright (C) 1999-2000 Riccardo Facchetti <riccardo@master.oasi.gpa.it>
dnl Copyright (C) 2003-2014 Adam Kropelin <adam@kroptech.com>
dnl
dnl Process this file with autoconf to produce a configure script.
dnl Process this file with autoheader to produce a config.h.in
dnl

dnl A distinctive file to look for in srcdir. 
AC_INIT(Developers)

dnl Config header file.
AC_CONFIG_HEADER(include/apcconfig.h:autoconf/config.h.in)

topdir=`pwd`
AC_CONFIG_AUX_DIR(${topdir}/autoconf)

dnl
dnl Absolute srcdir
ABSSRCDIR=`pwd`
AC_SUBST(ABSSRCDIR)
AC_SUBST(topdir)

dnl go up one more level
cd ..
TOP_DIR=`pwd`
cd ${topdir}
AC_SUBST(TOP_DIR)

VERSION=`sed -n -e 's/^.*VERSION.*"\(.*\)"$/\1/p' ${srcdir}/include/version.h`
AC_SUBST(VERSION)

DATE=`sed -n -e 's/^.*[ \t]*ADATE.*"\(.*\)"$/\1/p' ${srcdir}/include/version.h`
AC_SUBST(DATE)

dnl Executable extension, if any (e.g., .exe)
AC_EXEEXT

dnl
dnl These need to be searched before.
dnl
AC_PATH_PROGS(TRUEPRG, true, :)
AC_PATH_PROGS(FALSEPRG, false, :)

dnl 
dnl Create a new path statement starting with what the user has and
dnl adding directories we might need.  
dnl
PATH=$PATH:/bin:/sbin:/etc:/usr/bin:/usr/bin/X11:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

dnl
dnl Sets `host_cpu', `host_vendor', and `host_os' to the current host type.
dnl
AC_CANONICAL_HOST

dnl Interesting things are often in /usr/local
dnl But not if we're cross compiling!
if test "$cross_compiling" = "no" ; then
   for incdir in /usr/local/include ; do
      if test -d "$incdir" ; then
         CPPFLAGS="${CPPFLAGS} -I$incdir"
      fi
   done
   for libdir in /usr/local/lib64 /usr/local/lib ; do
      if test -d "$libdir" ; then
         LDFLAGS="${LDFLAGS} -L$libdir"
       fi
   done
fi

dnl Special considerations when cross-compiling for win32
if test "$host_os" = "mingw32" ; then
   if test -z "$CROSSTOOLS" ; then 
      CROSSTOOLS=${topdir}/../cross-tools
   fi
   if test -z "$DEPKGS"; then 
      DEPKGS=${topdir}/../depkgs-win32 
   fi
   CPPFLAGS="-I${topdir}/src/win32/compat -I$DEPKGS/libroot/include -I$DEPKGS/libroot/include/pthread ${CPPFLAGS}"
   PATH=$CROSSTOOLS/mingw32/bin:$PATH
   LDFLAGS="-L$DEPKGS/libroot/lib ${LDFLAGS}"
   BG=-mwindows
fi
AC_SUBST(BG)
AC_SUBST(CROSSTOOLS)
AC_SUBST(DEPKGS)

if test $HAVE_UNAME=yes -a x`uname -s` = xSunOS
then
dnl ----------------------------------------
dnl  SunOS defaults
dnl ----------------------------------------
   AC_PREFIX_DEFAULT(/opt/apcupsd)
dnl --exec-prefix == eprefix == prefix
dnl --bindir == eprefix/bin
dnl --sbindir == eprefix/sbin
dnl
dnl If the user has not set -sysconfdir, we set our default as
dnl /etc/opt/apcupsd
dnl
   if test x$sysconfdir = x'${prefix}/etc' ; then
      sysconfdir='/etc/opt/apcupsd'
   fi
dnl
dnl If the user has not set -sbindir, we set our default as /sbin
dnl
   if test x$sbindir = x'${exec_prefix}/sbin' ; then
      sbindir='/etc/opt/apcupsd/sbin'
   fi

   dnl Solaris often has interesting things in /usr/sfw
   CPPFLAGS="${CPPFLAGS} -I/usr/sfw/include"
   LDFLAGS="${LDFLAGS} -L/usr/sfw/lib"
else
dnl -------------------------------------------
dnl  non Sun defaults (i.e. all other platforms  
dnl -------------------------------------------
   AC_PREFIX_DEFAULT(/usr)
dnl
dnl If the user has not set -sysconfdir, we set our default as /etc/apcupsd
dnl
   if test x$sysconfdir = x'${prefix}/etc' ; then
      sysconfdir='/etc/apcupsd'
   fi

dnl
dnl If the user has not set -sbindir, we set our default as /sbin
dnl
   if test x$sbindir = x'${exec_prefix}/sbin' ; then
      sbindir='/sbin'
   fi
fi

dnl
dnl Check for programs.
dnl

AC_PROG_CXX                dnl We require a C++ compiler now
AC_PROG_CC                 dnl Determine a C compiler to use.
AC_PROG_CPP                dnl Determine a C pre-processor to use.
AC_PROG_INSTALL            dnl Determine a BSD install program.
AC_PROG_AWK
AC_ISC_POSIX

dnl Default linker is gcc
if test x$LD = x ; then
   LD="$CC"
fi

AC_SUBST(DEBUG)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(CXXFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBS)
AC_SUBST(DRVLIBS)

APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/libdrivers.a"
AC_SUBST(APCDRVLIBS)

dnl Initialize pthreads inclusion: warn PTHREAD_LFLAGS must contain only
dnl the pthreads libraries, if needed. For linker flags, add them to
dnl LDFLAGS.
PTHREAD_CFLAGS=""
PTHREAD_LFLAGS="-lpthread"
with_pthreads=yes

AC_DEFINE(HAVE_PTHREADS, [1], [Define if you have pthreads])
AC_SUBST(PTHREAD_CFLAGS)
AC_SUBST(PTHREAD_LFLAGS)

APCACCESS=apcaccess
SMTP=smtp
APCTEST=apctest
AC_SUBST(APCACCESS)
AC_SUBST(SMTP)
AC_SUBST(APCTEST)

dnl Now check for programs
AC_REQ_PATH_PROGS(SHUTDOWN, shutdown shutdown.bsd)

AC_PATH_PROGS(APCUPSD_MAIL, mail, no)
if test "$APCUPSD_MAIL" = "no"
then
   AC_MSG_WARN([mail program not found !])
   APCUPSD_MAIL=mail
fi

AC_REQ_PATH_PROGS(WALL, wall)
AC_REQ_PATH_PROGS(SCRIPTSHELL, sh bash)

AC_PROG_GMAKE
AC_REQ_TOOL(RANLIB, ranlib)
AC_REQ_PATH_PROG(RM, rm)
AC_REQ_PATH_PROG(CP, cp)
AC_REQ_PATH_PROG(ECHO, echo)
AC_REQ_PATH_PROG(LN, ln)
AC_REQ_PATH_PROG(SED, sed)
AC_REQ_TOOL(AR, ar)
AC_REQ_PATH_PROG(MV, mv)
AC_CHECK_TOOL(WINDRES, windres)
AC_CHECK_TOOLS(STRP, strip true)

AC_PATH_PROG(RST2HTML, rst2html,)
AC_PATH_PROG(RST2HTML, rst2html.py,)
AC_PATH_PROG(RST2PDF, rst2pdf,)

# -------------------------------------------------------------------------
#  If the user has not set --mandir, we default to /usr/share/man
# -------------------------------------------------------------------------
if test x$mandir = x'${prefix}/man' ; then
   mandir=/usr/share/man
fi

dnl
dnl Check for library functions.
dnl

dnl Mandatory on all builds
AC_CHECK_FUNCS(
   abort calloc getpid \
   rewind signal strerror strncmp \
   strncpy vfprintf ,,
   [AC_MSG_ERROR([cannot find required function.])]
)

dnl Optional on all builds
dnl We will provide our own implementations if needed
AC_CHECK_FUNCS(snprintf vsnprintf strlcpy strlcat accept4)

dnl Mandatory except for WIN32 builds where we provide our own
if test "$host_os" != "mingw32" ; then
   AC_CHECK_FUNCS(
      fork ioctl kill select setsid tcgetattr
      ,,
      [AC_MSG_ERROR([cannot find required function.])]
   )

   AC_MSG_CHECKING([for syslog])

   AC_TRY_LINK(
      [#include <syslog.h>],
      [syslog(0, "%s", "test");],
      AC_MSG_RESULT([yes]),
      AC_CHECK_LIB(socket, std_syslog,,
         [AC_MSG_ERROR([cannot find required function.])])
      AC_TRY_LINK(
         [#include <syslog.h>],
         [syslog(0, "%s", "test");], AC_MSG_RESULT([yes]),
         [AC_MSG_ERROR([cannot find required function.])])
   )
fi

dnl Solaris hides nanosleep in one of two libraries depending on version
dnl If we can't find it at all we'll just use our own substitute.
AC_SEARCH_LIBS(nanosleep, [rt posix4], AC_DEFINE(HAVE_NANOSLEEP, 1,
   [Define if you have the nanosleep function.]),
   [LIBEXTRAOBJ="$LIBEXTRAOBJ nanosleep.c"])

AC_FUNC_STRFTIME     dnl check for strftime.

# Under sysV68, socket and friends are provided by the C library.
# -linet does not provide socket, but causes multiple definition
# errors at link-time.  It is thus better to only use the C library.
# So don't add -linet to the link list unless it's necessary
#
# With Solaris socket and friends seem not to be in C library.
# I hope that if socket is found, all the BSD friends are in the same library
#
# - RF
#
AC_SEARCH_LIBS(socket, [xnet socket inet])
AC_SEARCH_LIBS(gethostname, [xnet socket inet])
AC_SEARCH_LIBS(gethostbyname, [nsl resolv])
AC_SEARCH_LIBS(gethostbyname_r, [nsl resolv])

# The condition in this test copes with the presence of inet_addr in libc6.
AC_SEARCH_LIBS(inet_addr, nsl)

dnl Needed by Interactive UNIX System V/386 Release 3.2
AC_CHECK_FUNC(strchr,
   AC_MSG_RESULT([using libc's strchr]),
   AC_CHECK_LIB(cposix,strchr,
   [EXTRADEFS="$EXTRADEFS -D_SYSV3"
    LIBS="$LIBS -lcposix"]))

AC_CHECK_FUNC(strstr, AC_DEFINE(HAVE_STRSTR,, [Define if you have strstr]), 
   [LIBEXTRAOBJ="$LIBEXTRAOBJ strstr.c"])

AC_CHECK_FUNC(strcasecmp, AC_DEFINE(HAVE_STRCASECMP,, [Define if you have strcasecmp]), 
   [LIBEXTRAOBJ="$LIBEXTRAOBJ strcasecmp.c"])
        
AC_CHECK_FUNC(memmove, AC_DEFINE(HAVE_MEMMOVE,, [Define if you have memmove]), 
   [LIBEXTRAOBJ="$LIBEXTRAOBJ memmove.c"])
        
AC_CHECK_FUNC(getopt_long, AC_DEFINE(HAVE_GETOPTLONG,, [Define if you have getoptlong]),
   [LIBEXTRAOBJ="$LIBEXTRAOBJ getopt.c getopt1.c"])

AC_CHECK_FUNC(inet_pton, AC_DEFINE(HAVE_INETPTON,, [Define if you have inetpton]), 
    [LIBEXTRAOBJ="$LIBEXTRAOBJ inet_pton.c"])

dnl
dnl Check for presence of localtime_r
dnl
AC_CHECK_FUNC(localtime_r, AC_DEFINE(HAVE_LOCALTIME_R,, [Define if you have localtime_r]),
   [LIBEXTRAOBJ="$LIBEXTRAOBJ localtime_r.c"])

dnl
dnl   Check for header files.
dnl
AC_CHECK_HEADERS(
   arpa/inet.h ctype.h errno.h fcntl.h \
   limits.h netdb.h netinet/in.h signal.h \
   stdarg.h stdio.h stdlib.h string.h strings.h sys/ioctl.h \
   sys/socket.h sys/types.h syslog.h termios.h \
   unistd.h
   ,,[AC_MSG_WARN([at least 1 header file is missing. This may not compile.])])

AC_CHECK_HEADERS(arpa/nameser.h,AC_DEFINE(HAVE_NAMESER_H,, [Define if you have nameser.h]))

AC_HEADER_STAT       dnl Check for S_ISDIR and S_ISREG macros.
AC_HEADER_TIME       dnl Check for time.h and sys/time.h.
AC_HEADER_SYS_WAIT      dnl Check for <sys/wait.h>

dnl
dnl   Check for structures.
dnl
AC_STRUCT_TM         dnl Check if time.h don't define struct tm.
AC_STRUCT_TIMEZONE      dnl Check how to have current timezone.

dnl
dnl Check for types.
dnl
AC_TYPE_GETGROUPS
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIGNAL
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_SOCKETLEN_T

dnl
dnl Check for system services.
dnl
AC_PATH_XTRA       dnl Is X Window System interesting ?.  **GAPCMON**
AC_SYS_INTERPRETER dnl Check for '#!' in shell scripts.

dnl
dnl UNIX variants
dnl
case $host in
   *-*-sunos*)   AC_DEFINE(HAVE_SUN_OS,,     [Define if building for SunOS])   ;;
   *-*-solaris*) AC_DEFINE(HAVE_SUN_OS,,     [Define if building for SunOS])   ;;
   *-*-osf*)     AC_DEFINE(HAVE_OSF1_OS,,    [Define if building for OSF1])    ;;
   *-*-aix*)     AC_DEFINE(HAVE_AIX_OS,,     [Define if building for AIX])     ;;
   *-*-hpux*)    AC_DEFINE(HAVE_HPUX_OS,,    [Define if building for HPUX])    ;;
   *-*-linux*)   AC_DEFINE(HAVE_LINUX_OS,,   [Define if building for Linux])   ;;
   *-*-freebsd*) AC_DEFINE(HAVE_FREEBSD_OS,, [Define if building for FreeBSD]) ;;
   *-*-netbsd*)  AC_DEFINE(HAVE_NETBSD_OS,,  [Define if building for NetBSD])  ;;
   *-*-openbsd*) AC_DEFINE(HAVE_OPENBSD_OS,, [Define if building for OpenBSD]) ;;
   *-*-bsdi*)    AC_DEFINE(HAVE_BSDI_OS,,    [Define if building for BSDi])    ;;
   *-*-irix*)    AC_DEFINE(HAVE_SGI_OS,,     [Define if building for IRIX])    ;;
   *-*-darwin*)  AC_DEFINE(HAVE_DARWIN_OS,,  [Define if building for Darwin])  ;;
   *-*-nto*)     AC_DEFINE(HAVE_QNX_OS,,     [Define if building for QNX])     ;;
   *-*-mingw*)   AC_DEFINE(HAVE_MINGW,,      [Define if building for MINGW])   ;;
esac

dnl
dnl Check for enable particular features.
dnl

AC_ARG_ENABLE(all, [
AC_HELP_STRING([--enable-all], [Enable all optional modules])],
   [ if test "$enableval" = "yes" ; then
      enable_usb=yes
      enable_net=yes
      enable_snmp=yes
      enable_test=yes
      enable_pcnet=yes
      enable_modbus=yes
      enable_modbus_usb=yes
      enable_cgi=yes
      if test "$host_os" != "mingw32" ; then
         enable_gapcmon=yes
      fi
   fi], [])

dnl
dnl halpolicydir configuration
dnl

dnl Establish default
case $host in
   *-*-linux*)
      HALPOLICYDIR="/usr/share/hal/fdi/policy/20thirdparty"
      ;;
   *)
      HALPOLICYDIR=""
      ;;
esac

dnl Allow user override
AC_ARG_WITH(halpolicydir,
   [AC_HELP_STRING([--with-halpolicydir=DIR], [HAL policy directory (default is platform dependent)])],
   HALPOLICYDIR=$withval)

dnl Report result
if test "x$HALPOLICYDIR" != "x" ; then
   AC_MSG_RESULT([Using halpolicydir=$HALPOLICYDIR])
else
   AC_MSG_RESULT([Will not install HAL policy file])
fi
AC_SUBST(HALPOLICYDIR)

#--------------------------------------------------------------------
# Check for TCP wrapper support, code taken from Bacula KES-7Nov03
#--------------------------------------------------------------------

AC_ARG_WITH(libwrap,
   [AC_HELP_STRING([--with-libwrap=DIR], [Compile in libwrap (tcp_wrappers) support])],
   [
      if test "x$withval" != "xno" ; then
         saved_LIBS="$LIBS"
         LIBS="$saved_LIBS -lwrap"
         AC_MSG_CHECKING([for libwrap])
         AC_TRY_LINK(
            [ #include <tcpd.h>
              int deny_severity = 0;
              int allow_severity = 0;
              struct request_info *req; ],
            [ hosts_access(req); ],
            [ AC_MSG_RESULT([yes])
              AC_DEFINE(HAVE_LIBWRAP,, [Define if you have libwrap])
              TCPW_MSG="yes" ],
            [ LIBS="$saved_LIBS -lwrap -lnsl"
              AC_TRY_LINK(
                 [ #include <tcpd.h>
                   int deny_severity = 0;
                   int allow_severity = 0;
                   struct request_info *req; ],
                 [ hosts_access(req); ],
                 [ AC_MSG_RESULT([yes])
                   AC_DEFINE(HAVE_LIBWRAP)
                   TCPW_MSG="yes" ],
                 [AC_MSG_ERROR([*** libwrap missing]) ] ]
              )
         )
      fi
   ]
)


dnl   
dnl Check for enable of cgi program builds
dnl
ENABLE_CGI="no"
CGI=
GD_LIBS=
SYS_GD_FOUND=no
AC_ARG_ENABLE(cgi,
   [AC_HELP_STRING([--enable-cgi], [Compile CGI programs])],
   [ if test "$enableval" = "yes"
     then
        ENABLE_CGI="yes"
        CGI=cgi

        dnl
        dnl First search for system library that defines gdImagePng
        AC_CHECK_LIB(gd, gdImagePng,
           [ AC_MSG_RESULT([using PNG file format.])
             AC_DEFINE(SYS_IMGFMT_PNG,, [Define if you have gdImagePng in libgd])
             GD_LIBS="-lgd"
             SYS_GD_FOUND=yes ],
           [$as_unset ac_cv_lib_gd_gdImagePng],
           [])

        dnl
        dnl How about with libpng and libz dependencies?
        if test "${SYS_GD_FOUND}" = "no"
        then
           AC_CHECK_LIB(gd, gdImagePng,
              [ AC_MSG_RESULT([using PNG file format.])
                AC_DEFINE(SYS_IMGFMT_PNG,, [Define if you have gdImagePng in libgd])
                GD_LIBS="-lgd -lpng12 -lz"
                SYS_GD_FOUND=yes ],
              [],
              [-lpng12 -lz])
        fi

        dnl
        dnl Second search for system library that defines gdImageGif
        if test "${SYS_GD_FOUND}" = "no"
        then
           AC_CHECK_LIB(gd, gdImageGif,
              [ AC_MSG_RESULT([using GIF file format.])
                AC_DEFINE(SYS_IMGFMT_GIF,, [Define if you have gdImageGif in libgd])
                GD_LIBS="-lgd"
                SYS_GD_FOUND=yes ],
              [],
              [])
        fi

        if test "${SYS_GD_FOUND}" = "no"
        then
           AC_MSG_ERROR([Your system lacks the GD library which is needed for
                         compiling the apcupsd CGI programs. Please install 
                         libgd and re-run the ./configure script.
                         Alternatively you can disable the CGI support.])
        fi

        AC_CHECK_HEADER(gd.h, [ GDHEAD= ],
           [ AC_CHECK_HEADER(gd/gd.h, [ GDHEAD="gd/" ],
              [ AC_CHECK_HEADER(gd2/gd.h, [ GDHEAD="gd2/" ],
                 [ AC_MSG_ERROR([Found system GD library but no header file.
                                 Please find the file gd.h in your system
                                 include directories and report its location
                                 to apcupsd-users@lists.sourceforge.net]) ])
              ])
           ])

         AC_SUBST(GDHEAD)
    fi
])


dnl   
dnl Check for enable of drivers
dnl
AC_ARG_ENABLE(apcsmart, [
AC_HELP_STRING([--enable-apcsmart], [Compile APC SmartUPS serial driver (default)])
AC_HELP_STRING([--disable-apcsmart], [No APC SmartUPS serial driver])],
   [ if test "$enableval" = "yes" ; then
       dnl This is for C code inclusion
       AC_DEFINE(HAVE_APCSMART_DRIVER,, [Define if building apcsmart driver])

       dnl This is for Makefile.in inside src/drivers/
       APCSMART_DRIVER="apcsmart"
       AC_SUBST(APCSMART_DRIVER)
       APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/apcsmart/libapcsmartdrv.a"
     fi ],
   [ AC_DEFINE(HAVE_APCSMART_DRIVER)
     APCSMART_DRIVER="apcsmart"
     AC_SUBST(APCSMART_DRIVER)
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/apcsmart/libapcsmartdrv.a" ])

AC_ARG_ENABLE(dumb, [
AC_HELP_STRING([--enable-dumb], [Compile dumb UPS driver (default)])
AC_HELP_STRING([--disable-dumb], [No dumb UPS driver])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_DUMB_DRIVER,, [Define if building simple signaling driver])
        DUMB_DRIVER="dumb"
        AC_SUBST(DUMB_DRIVER)
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/dumb/libdumbdrv.a"
     fi],
   [ AC_DEFINE(HAVE_DUMB_DRIVER)
     DUMB_DRIVER="dumb"
     AC_SUBST(DUMB_DRIVER)
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/dumb/libdumbdrv.a" ])

dnl Allow user to force USB type to generic
GENERICUSB="no"
AC_ARG_WITH(generic-usb,
[AC_HELP_STRING([--with-generic-usb], [Force use of generic USB driver]) ],
   [GENERICUSB="yes" ])

AC_ARG_ENABLE(usb, [
AC_HELP_STRING([--enable-usb], [Compile USB UPS code])
AC_HELP_STRING([--disable-usb], [No USB UPS code (default)])],
   [ if test "$enableval" = "yes" ; then
      dnl USB driver is usually determined by host system.
      dnl But if user forces generic_usb then fake it out.
      if test $GENERICUSB = "yes" ; then
         usbhost=forcegeneric
      else
         usbhost=$host
      fi

      AC_DEFINE(HAVE_USB_DRIVER,, [Define if building USB driver])
      USB_DRIVER="usb"
      AC_SUBST(USB_DRIVER)
      APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/usb/libusbdrv.a"

      case $usbhost in
         *-*-linux*)
            USB_TYPE="linux"
            AC_MSG_RESULT([Using Linux USB driver.])
            ;;
         *)
            needlibusb=true
            USB_TYPE="generic"
            ;;
      esac

      AC_SUBST(USB_TYPE)
      DISPLAY_USB_DRIVER=$USB_TYPE-$USB_DRIVER
     fi ],
    [])

AC_ARG_ENABLE(net, [
AC_HELP_STRING([--enable-net], [Compile networking driver for slaves (default)])
AC_HELP_STRING([--disable-net], [No network driver for slaves])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_NET_DRIVER,, [Define if building net driver])
        NET_DRIVER="net"
        AC_SUBST(NET_DRIVER)
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/net/libnetdrv.a"
     fi ],
   [ AC_DEFINE(HAVE_NET_DRIVER)
     NET_DRIVER="net"
     AC_SUBST(NET_DRIVER)
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/net/libnetdrv.a" ])

AC_ARG_ENABLE(snmp, [
AC_HELP_STRING([--enable-snmp], [Compile SNMP driver (default)])
AC_HELP_STRING([--disable-snmp], [No SNMP driver])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_SNMPLITE_DRIVER,, [Define if building snmplite driver])
        SNMPLITE_DRIVER="snmplite"
        DISPLAY_SNMP_DRIVER="snmp"
        AC_SUBST(SNMPLITE_DRIVER)        
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/snmplite/libsnmplitedrv.a"
     fi ],
   [ AC_DEFINE(HAVE_SNMPLITE_DRIVER)
     SNMPLITE_DRIVER="snmplite"
     DISPLAY_SNMP_DRIVER="snmp"
     AC_SUBST(SNMPLITE_DRIVER) 
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/snmplite/libsnmplitedrv.a" ])

dnl Enable test driver
AC_ARG_ENABLE(test, [
AC_HELP_STRING([--enable-test], [Compile TEST driver code])
AC_HELP_STRING([--disable-test], [No TEST driver (default)])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_TEST_DRIVER,, [Define if building test driver])
        TEST_DRIVER="test"
        AC_SUBST(TEST_DRIVER)
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/test/libtestdrv.a"
     fi],
   [])

dnl Enable pcnet driver
AC_ARG_ENABLE(pcnet, [
AC_HELP_STRING([--enable-pcnet], [Compile PCNET driver code (default)])
AC_HELP_STRING([--disable-pcnet], [No PCNET driver])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_PCNET_DRIVER,, [Define if building PCNET driver])
        PCNET_DRIVER="pcnet"
        AC_SUBST(PCNET_DRIVER)
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/pcnet/libpcnetdrv.a"
     fi],
   [ AC_DEFINE(HAVE_PCNET_DRIVER)
     PCNET_DRIVER="pcnet"
     AC_SUBST(PCNET_DRIVER)
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/pcnet/libpcnetdrv.a" ])

dnl Enable modbus USB support
AC_ARG_ENABLE(modbus-usb, [
AC_HELP_STRING([--enable-modbus-usb], [Compile MODBUS/USB driver code])
AC_HELP_STRING([--disable-modbus-usb], [No MODBUS/USB driver (default)])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_MODBUS_USB_DRIVER,, [Define if building MODBUS/USB driver])
        MODBUS_USB_DRIVER="modbus-usb"
        AC_SUBST(MODBUS_USB_DRIVER)
        enable_modbus=yes
        needlibusb=true
     fi],
   [])

dnl Enable modbus driver
AC_ARG_ENABLE(modbus, [
AC_HELP_STRING([--enable-modbus], [Compile MODBUS driver code (default)])
AC_HELP_STRING([--disable-modbus], [No MODBUS driver])],
   [ if test "$enableval" = "yes" ; then
        AC_DEFINE(HAVE_MODBUS_DRIVER,, [Define if building MODBUS driver])
        MODBUS_DRIVER="modbus"
        AC_SUBST(MODBUS_DRIVER)
        APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/modbus/libmodbusdrv.a"
     fi],
   [ AC_DEFINE(HAVE_MODBUS_DRIVER)
     MODBUS_DRIVER="modbus"
     AC_SUBST(MODBUS_DRIVER) 
     APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/drivers/modbus/libmodbusdrv.a" ])

dnl If USB or MODBUS driver need libusb, handle it now
if test "$needlibusb" = "true" ; then
   case $host in
      *-*-mingw*)
         LIBUSBH=libusb-winusb-bridge.h
         DRVLIBS="$DRVLIBS -lsetupapi"
         AC_MSG_RESULT([Using generic winusb USB driver.])
         ;;
      *)
         dnl Use libusb-config if found, otherwise fall back on AC_CHECK_LIB
         AC_PATH_PROG(usbcfg, libusb-config)
         if test x$usbcfg != x ; then
            LIBUSB=`$usbcfg --libs`
            LIBUSBH=`$usbcfg --prefix`/include/usb.h
         else
            AC_CHECK_LIB(usb, usb_init, 
                         [LIBUSB=-lusb
                          LIBUSBH=usb.h],
                         [AC_MSG_ERROR(Unable to find libusb)], 
                         [$DRVLIBS])
         fi

         DRVLIBS="$DRVLIBS $LIBUSB"
         AC_MSG_RESULT([Using generic libusb USB driver.])
         ;;
   esac

   AC_SUBST(LIBUSBH)
   LIBUSBHID=libusbhid
   AC_SUBST(LIBUSBHID)
   APCDRVLIBS="$APCDRVLIBS \$(topdir)/src/libusbhid/libusbhid.a"
   CPPFLAGS="$CPPFLAGS -I\$(topdir)/src/libusbhid"
fi

dnl Force NIS to enabled since libnis is required by apcaccess.
dnl Users may still disable NIS at runtime in apcupsd.conf.
AC_DEFINE(HAVE_NISSERVER,, [Define if apcupsd NIS server is being built])
AC_DEFINE(HAVE_NISLIB,, [Define if apcupsd NIS library is being built])
NISSRV_ENABLED=yes

dnl Allow setting of NISIP value
NISIP="0.0.0.0"
AC_ARG_WITH(nisip,
   [AC_HELP_STRING([--with-nisip=IP-ADDRESS], [Specify the IP address to bind to (default=0.0.0.0)])],
   [NISIP="$withval";])
AC_SUBST(NISIP)


CGIBIN="/etc/apcupsd"
AC_ARG_WITH(cgi-bin,
   [AC_HELP_STRING([--with-cgi-bin=DIR], [Specify cgi-bin directory (default=/etc/apcupsd)])],
   [CGIBIN="$withval";])

dnl
dnl Check for enable of gapcmon
dnl
GAPCMON=
GAPCMON_ENABLED=no
AC_ARG_ENABLE(gapcmon, 
   [AC_HELP_STRING([--enable-gapcmon], [Build GTK/GUI front-end to apcupsd (default=no)])],
   [ if test "$enableval" = "yes" ; then
      GAPCMON=gapcmon
      GAPCMON_ENABLED=yes

      dnl
      dnl Check for Gnome packages
      dnl
      PKG_CHECK_MODULES(GAPCMON, gtk+-2.0 >= 2.4.0 glib-2.0 gthread-2.0 gconf-2.0)
      GAPCMON_CFLAGS="$GAPCMON_CFLAGS -DVERSION=\\\"$VERSION\\\""
   fi],
  [])
AC_SUBST(GAPCMON)

dnl
dnl Check for enable of apcagent
dnl
APCAGENT=
APCAGENT_ENABLED=no
AC_ARG_ENABLE(apcagent, 
   [AC_HELP_STRING([--enable-apcagent], [Build menubar app for Mac OS X (default=no)])],
   [ if test "$enableval" = "yes" ; then
      case $host in
      *-*-darwin*)
         APCAGENT=apcagent
         APCAGENT_ENABLED=yes
         ;;
      *)
         AC_MSG_ERROR([apcagent can only be built on Mac OS X])
         ;;
      esac
   fi],
  [case $host in
   *-*-darwin*)
      APCAGENT=apcagent
      APCAGENT_ENABLED=yes
      ;;
   esac])
AC_SUBST(APCAGENT)

dnl
dnl Figure out which gethostbyname_r() variant we have
dnl
AX_FUNC_WHICH_GETHOSTBYNAME_R
case "$ac_cv_func_which_gethostbyname_r" in
   three)
   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_3,, [Define for 3 argument version of gethostbyname_r])
   GAPCMON_CFLAGS="$GAPCMON_CFLAGS -DHAVE_FUNC_GETHOSTBYNAME_R_3"
   ;;

   five)
   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_5,, [Define for 5 argument version of gethostbyname_r])
   GAPCMON_CFLAGS="$GAPCMON_CFLAGS -DHAVE_FUNC_GETHOSTBYNAME_R_5"
   ;;

   six)
   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_6,, [Define for 6 argument version of gethostbyname_r])
   GAPCMON_CFLAGS="$GAPCMON_CFLAGS -DHAVE_FUNC_GETHOSTBYNAME_R_6"
   ;;

   no)
   AC_DEFINE(HAVE_FUNC_GETHOSTBYNAME_R_0,, [Define if system does not have gethostbyname_r])
   GAPCMON_CFLAGS="$GAPCMON_CFLAGS -DHAVE_FUNC_GETHOSTBYNAME_R_0"
   ;;

   *)
   AC_MSG_ERROR([gethostbyname_r is required])
   ;;
esac

if test -n "$GCC"; then
   # Starting with GCC 3.0, you must link C++ programs against either
   # libstdc++ (shared by default), or libsupc++ (always static).  If
   # you care about binary portability between Linux distributions,
   # you need to either 1) build your own GCC with static C++ libraries
   # or 2) link using gcc and libsupc++.  We choose the latter since
   # CUPS doesn't (currently) use any of the stdc++ library.
   #
   # Previous versions of GCC do not have the reliance on the stdc++
   # or g++ libraries, so the extra supc++ library is not needed.
   AC_MSG_CHECKING(if libsupc++ is required)

   SUPC="`$CXX -print-file-name=libsupc++.a 2>/dev/null`"
   case "$SUPC" in
   libsupc++.a*)
      # Library not found, so this is an older GCC...
      LD="$CXX"
      AC_MSG_RESULT(no)
      ;;
   *)
      # This is gcc 3.x, and it knows of libsupc++, so we need it
      LIBS="$LIBS -lsupc++"
      LD="$CC"
      AC_MSG_RESULT(yes)

      # See if this system has a broken libsupc++ that requires
      # a workaround (FreeBSD 5.x, 6.x)
      case $host in
         *-*-freebsd*)
            AC_MSG_CHECKING(if libsupc++ is missing __terminate_handler)
            nm -C --defined-only "$SUPC" 2>/dev/null | grep __terminate_handler > /dev/null
            if test $? -eq 0 ; then
               AC_MSG_RESULT(no)
            else
               AC_MSG_RESULT(yes -- will attempt workaround)
               LIBEXTRAOBJ="$LIBEXTRAOBJ libsupc++fix.cpp"
            fi
            ;;
      esac
      ;;
   esac
fi

dnl Disable exceptions and RTTI if compiler supports it
AX_ADD_COMPILE_FLAG(-fno-exceptions)
AX_ADD_COMPILE_FLAG(-fno-rtti)

dnl Configure compile warnings
AX_ADD_COMPILE_FLAG(-Wall)
AX_ADD_COMPILE_FLAG(-Wno-unused-result)

AC_SUBST(MAKE)
AC_SUBST(CP)
AC_SUBST(MV)
AC_SUBST(ECHO)
AC_SUBST(LN)
AC_SUBST(SED)
AC_SUBST(AR)
AC_SUBST(RM)
AC_SUBST(CC)
AC_SUBST(CXX)
AC_SUBST(LD)
AC_SUBST(CGI)
AC_SUBST(GD_LIBS)
AC_SUBST(CGIBIN)
AC_DEFINE_UNQUOTED(SYSCONFDIR, "$sysconfdir", [Default directory in which apcupsd config files are stored])
AC_SUBST(SYSCONFDIR)
AC_SUBST(RST2HTML)
AC_SUBST(RST2PDF)

dnl If the AC_CONFIG_AUX_DIR macro for autoconf is used we will probably
dnl find the mkinstalldirs script in another subdir than ($topdir).
dnl Try to locate it.
MKINSTALLDIRS=
if test -n "$ac_aux_dir"; then
   MKINSTALLDIRS="$ac_aux_dir/mkinstalldirs"
fi
if test x$MKINSTALLDIRS = x ; then
   MKINSTALLDIRS="\$(topdir)/autoconf/mkinstalldirs"
fi
AC_SUBST(MKINSTALLDIRS)


dnl AC_CHECK_FUNC calls that might hack the Makefile must precede
dnl the following AC_SUBSTs

AC_SUBST(EXTRADEFS)
AC_SUBST(LIBEXTRAOBJ)
AC_SUBST(EXTRAOBJ)

dnl Check out the wait reality.  We have to assume sys/wait.h is present.
AC_CHECK_FUNCS(wait waitpid wait3)

if test "$ENABLE_CGI" = "yes"
then
   AC_CHECK_FUNCS( snprintf,,
      [AC_MSG_ERROR([cannot find required function.])])
   echo 'Enabling cgi program support...'
else
   echo 'Disabling cgi support...'
fi

dnl
dnl Find out the distribution.   NOTE!!! Only determine the
dnl   distribution here.  Do all additional work below.
dnl

# now allow user to specify DISTNAME
AC_ARG_WITH(distname,
   [AC_HELP_STRING([--with-distname=NAME], [Specify DISTNAME])],
   [DISTNAME=$withval])

if test "x$DISTNAME" != "x"
then
   echo "distname set to $DISTNAME"
elif test "$host_os" = "mingw32" ; then
   DISTNAME=mingw
elif test $HAVE_UNAME=yes -a x`uname -s` = xOSF1
then
   DISTNAME=alpha
elif test $HAVE_UNAME=yes -a x`uname -s` = xHP-UX
then
   DISTNAME=hpux
elif test $HAVE_UNAME=yes -a x`uname -s` = xSunOS
then
   DISTNAME=sun
elif test $HAVE_UNAME=yes -a x`uname -s` = xFreeBSD
then
   DISTNAME=freebsd
elif test $HAVE_UNAME=yes -a x`uname -s` = xNetBSD
then
   DISTNAME=netbsd
elif test $HAVE_UNAME=yes -a x`uname -s` = xOpenBSD
then
   DISTNAME=openbsd
elif test $HAVE_UNAME=yes -a x`uname -s` = xBSD/OS
then
   DISTNAME=bsdi
elif test $HAVE_UNAME=yes -a x`uname -s` = xDarwin
then
    DISTNAME=darwin
elif test $HAVE_UNAME=yes -a x`uname -s` = xQNX
then
   DISTNAME=qnx
elif test -f /etc/SuSE-release
then
   DISTNAME=suse
elif test -d /etc/SuSEconfig
then
   DISTNAME=suse5
elif test -f /etc/mandrake-release
then
    DISTNAME=mandrake
elif test -f /etc/whitebox-release
then
   DISTNAME=redhat
elif test -f /etc/redhat-release
then
   DISTNAME=redhat
elif test -f /etc/yellowdog-release
then
   DISTNAME=yellowdog
elif test -f /etc/debian_version
then
   DISTNAME=debian
elif test -f /etc/slackware-version
then
   DISTNAME=slackware
elif test -f /etc/gentoo-release
then
   DISTNAME=gentoo
elif test -f /etc/engarde-version
then
   DISTNAME=engarde
else
   DISTNAME=unknown
fi

dnl   
dnl Check for enable install of distdir
dnl
DISTDIR=$DISTNAME
with_distdir="yes"
AC_ARG_ENABLE(install-distdir,
   [AC_HELP_STRING([--enable-install-distdir], [Install distribution])],
   [ if test "$enableval" = "no" ; then
        DISTDIR=""
        with_distdir="no"
     fi ])
AC_SUBST(DISTDIR)

dnl
dnl At this point, we define the apcupsd defaults for a 
dnl  number of different values (directories, ports, etc)
dnl  When the distribution is determined a bit lower, the 
dnl  default can be reset to correspond to the standard for
dnl  that system.  
dnl  Finally, after that any value that the user has set via
dnl  command line options will apply.
dnl  

# Define the default UPS
UPSTYPE=apcsmart

# Define the default Cable
UPSCABLE=smart

# set the default nologin directory
nologdir=/etc

# Find the default directory to put the root-mode PID file in
for PIDDIR in "/var/run" "/etc/apcupsd"
do
   if test -d $PIDDIR 
   then
      break;
   fi
done

# Find the default directory to put the apcupsd.events and apcupsd.status files
for LOGDIR in "/var/log" "/etc/apcupsd"
do
   if test -d $LOGDIR 
   then
      break;
   fi
done

# set the default serial port lock director
for LOCKDIR in "/var/lock" "/var/spool/locks" "/etc/apcupsd"
do
   if test -d $LOCKDIR
   then
      break;
   fi
done

# set the default PWRFAILDIR
PWRFAILDIR=${sysconfdir}

# define the default serial port device
SERIALDEV=/dev/ttyS0

# define the default NIS (Network Information Server -- CGI) port
NISPORT=3551

#
# Now we set appropriate distribution specific
#  variables and defaults
#
case "$DISTNAME" in
alpha)
   PTHREAD_LFLAGS="-lpthread -lexc"
   DISTVER=`uname -r`
   SERIALDEV=/dev/tty01
   LOCKDIR=/var/spool/locks
   LOGDIR=/etc/apcupsd
   DFILES="\
      platforms/alpha/apcupsd \
      platforms/alpha/awkhaltprog"
   ;;
bsdi)
   DISTVER=`uname -a |awk '{print $3}'`
   ;;
debian)
   DISTVER=`cat /etc/debian_version`
   DFILES="\
      platforms/debian/apcupsd \
      platforms/debian/ups-monitor"
   ;;
freebsd)
   DISTVER=`uname -a |awk '{print $3}'`
   SERIALDEV=/dev/cuaa0
   nologdir=/var/run
   PWRFAILDIR=/var/run
   LOCKDIR=/var/spool/lock
   PTHREAD_CFLAGS="-pthread"
   PTHREAD_LFLAGS=""
   LDFLAGS="$LDFLAGS -pthread"
   DFILES="\
      platforms/freebsd/apcupsd \
      platforms/freebsd/apccontrol"
   ;;
gentoo)
   DISTVER=`awk '/version / {print $5}' < /etc/gentoo-release`
   DFILES="\
      platforms/gentoo/apcupsd \
      platforms/gentoo/halt"
   ;;
hpux)
   DISTVER=`uname -r`
   SERIALDEV=/dev/tty0p0
   PTHREAD_CFLAGS="-D_XOPEN_SOURCE_EXTENDED -D_REENTRANT"
   DFILES="\
      platforms/hpux/apcupsd \
      platforms/hpux/halt"
   ;;
netbsd)
   DISTVER=`uname -a |awk '{print $3}'`
   SERIALDEV=/dev/cua01
   PTHREAD_CFLAGS="-pthread"
   PTHREAD_LFLAGS=""
   LDFLAGS="$LDFLAGS -pthread"
   DFILES="\
      platforms/netbsd/apcupsd"
   ;;
openbsd)
   DISTVER=`uname -a |awk '{print $3}'`
   SERIALDEV=/dev/cua01
   LOCKDIR=/var/spool/lock
   PTHREAD_CFLAGS="-pthread"
   PTHREAD_LFLAGS=""
   LDFLAGS="$LDFLAGS -pthread"
   DFILES="\
      platforms/openbsd/apcupsd \
      platforms/openbsd/apccontrol"
  ;;
mandrake)
   DISTVER=`cat /etc/mandrake-release | grep release | cut -f 5 -d ' '`
   DFILES="\
      platforms/mandrake/apcupsd \
      platforms/mandrake/apcupsd.spec \
      platforms/mandrake/awkhaltprog"
   ;;
redhat)
   if test -f /etc/whitebox-release ; then
      f=/etc/whitebox-release
   else
      f=/etc/redhat-release
   fi
   if test `cat $f | grep release | cut -f 3 -d ' '`x = "Enterprise"x ; then
      DISTVER="Enterprise "`cat $f | grep release | cut -f 6 -d ' '`
   else
      DISTVER=`cat /etc/redhat-release | grep release | cut -f 5 -d ' '`
   fi
   DFILES="\
      platforms/redhat/apcupsd \
      platforms/redhat/apcupsd.spec \
      platforms/redhat/awkhaltprog"
   ;;
yellowdog)
    if test `cat /etc/yellowdog-release | grep release | cut -f 3 -d ' '`x = "Enterprise"x ; then
       DISTVER="Enterprise "`cat /etc/yellowdog-release | grep release | cut -f 6 -d ' '`
    else
       DISTVER=`cat /etc/yellowdog-release | grep release | cut -f 5 -d ' '`
   fi
   DFILES="\
      platforms/yellowdog/apcupsd \
      platforms/yellowdog/apcupsd.spec \
      platforms/yellowdog/awkhaltprog"
   ;;
engarde)
   DISTVER=`cat /etc/engarde-release | grep ersion | cut -f 5 -d ' '`
   DFILES="\
      platforms/engarde/apcupsd \
      platforms/engarde/apcupsd.spec \
      platforms/engarde/awkhaltprog"
   ;;
slackware)
   DISTVER=`cat /etc/slackware-version`
   DFILES="\
      platforms/slackware/apcupsd \
      platforms/slackware/rc6.patch" 
   ;;
sun)
   DISTVER=Solaris
   SERIALDEV=/dev/ttya
   PWRFAILDIR=/etc
   PIDDIR=/var/run
   LOCKDIR=/var/spool/locks
   LOGDIR=${sysconfdir}
   APCUPSD_MAIL=/usr/bin/mailx
   PTHREAD_CFLAGS="-D_POSIX_PTHREAD_SEMANTICS"
   DFILES="\
      platforms/sun/apcupsd \
      platforms/sun/rc0.solaris \
      platforms/sun/apccontrol"
   ;;
suse)
   DISTVER=`cat /etc/SuSE-release |grep VERSION| cut -f 3 -d ' '`
   DFILES="\
      platforms/suse/apcupsd \
      platforms/suse/awkhaltprog"
   ;;
suse5)
   DISTNAME=suse
   DISTVER=5.x
   DFILES="\
      platforms/suse/apcupsd \
      platforms/suse/halt-setup.sh \
      platforms/suse/halt"
   ;;
darwin)
   DISTVER=`uname -r`
   SERIALDEV=
   UPSTYPE=usb
   UPSCABLE=usb
   PIDDIR=/var/run
   LOCKDIR=/var/tmp
   DFILES="\
      platforms/darwin/apccontrol \
      platforms/darwin/apcupsd-start \
      platforms/darwin/apcupsd-uninstall \
      platforms/darwin/org.apcupsd.apcupsd.plist"
   ;;
qnx)
   PTHREAD_LFLAGS=""
   DISTVER=`uname -r`
   SERIALDEV=/dev/ser1
   DFILES="\
      platforms/qnx/apcupsd \
      platforms/qnx/apccontrol"
   ;;
mingw)
   PTHREAD_LFLAGS="-lpthreadGCE"
   LIBS="${LIBS} -luser32 -lgdi32 -lwsock32 -lnetapi32"
   CPPFLAGS="${CPPFLAGS} -D_WIN32_IE=0x0500"
   WIN32=win32
   AC_SUBST(WIN32)
   ;;
unknown)
   DISTVER=unknown
   DFILES="\
      platforms/unknown/apcupsd \
      platforms/unknown/halt"
  ;;
*)
  AC_MSG_WARN([Something went wrong. Unknown DISTNAME $DISTNAME])
  ;;
esac  

dnl
dnl Allow user to set nologdir
dnl
AC_ARG_WITH(nologin,
   [AC_HELP_STRING([--with-nologin=DIR], [Specify nologin file directory (default /etc)])],
   [ nologdir="$withval"; ])

# now allow the user to specify the PID directory
AC_ARG_WITH(pid-dir,
    [AC_HELP_STRING([--with-pid-dir=DIR], [Specify PIDDIR directory (default is OS dependent)])],
    [PIDDIR="$withval";])

# now allow user to specify LOGDIR
AC_ARG_WITH(log-dir,
    [AC_HELP_STRING([--with-log-dir=DIR], [Specify EVENTS and STATUS directory (default is OS dependent)])],
    [LOGDIR="$withval";])

# now allow user to specify LOCKDIR
AC_ARG_WITH(lock-dir,
    [AC_HELP_STRING([--with-lock-dir=DIR], [Specify serial port lock directory (default is OS dependent)])],
    [LOCKDIR="$withval";])

# now allow user to specify PWRFAILDIR
AC_ARG_WITH(pwrfail-dir,
    [AC_HELP_STRING([--with-pwrfail-dir=DIR], [Specify power failure file directory (default is OS dependent)])],
    [PWRFAILDIR="$withval";])

# now allow user to specify SERIALDEV
AC_ARG_WITH(serial-dev,
    [AC_HELP_STRING([--with-serial-dev=DEV], [Specify serial port device])],
    [SERIALDEV="$withval";])

AC_ARG_WITH(dev,
    [AC_HELP_STRING([--with-dev=DEV], [Specify port or device])],
    [SERIALDEV="$withval";])

# now allow user to specify NISPORT
AC_ARG_WITH(nis-port,
    [AC_HELP_STRING([--with-nis-port=PORT], [Specify NIS (CGI) port (default 3551 except Debian)])],
    [NISPORT="$withval";])

# now allow user to specify UPSTYPE
AC_ARG_WITH(upstype,
    [AC_HELP_STRING([--with-upstype=TYPE], [Specify the UPS type])],
    [UPSTYPE="$withval";])

# now allow user to specify UPSCABLE
AC_ARG_WITH(upscable,
    [AC_HELP_STRING([--with-upscable=CABLE], [Specify the UPS cable])],
    [UPSCABLE="$withval";])

AC_SUBST(DISTNAME)
AC_SUBST(DISTVER)
AC_SUBST(SERIALDEV)
AC_DEFINE_UNQUOTED(PIDDIR, "$PIDDIR", [Default directory in which pid file is written])
AC_SUBST(PIDDIR)
AC_DEFINE_UNQUOTED(LOGDIR, "$LOGDIR", [Default directory in which log is written])
AC_SUBST(LOGDIR)
AC_DEFINE_UNQUOTED(NOLOGDIR, "$nologdir", [Default directory in which nologin file is written])
AC_SUBST(nologdir)
AC_DEFINE_UNQUOTED(PWRFAILDIR, "$PWRFAILDIR", [Default directory in which powerfail flag file is written])
AC_SUBST(PWRFAILDIR)
AC_SUBST(LOCKDIR)
AC_SUBST(NISPORT)
AC_DEFINE_UNQUOTED(NISPORT, $NISPORT, [Default port number for NIS server])
AC_SUBST(UPSTYPE)
AC_SUBST(UPSCABLE)

HOST=$DISTNAME
AC_DEFINE_UNQUOTED(HOST, "$HOST", [Name of system on which apcupsd will run])
AC_SUBST(HOST)

dnl
dnl Find out gcc's installation directory and include files. Used in
dnl configure.
dnl
dnl Make sure a gccinclude variable exist.
dnl '.' is innocuous.
gccinclude="."

if test "$CC" = "gcc"
then
   rm -f $srcdir/gcc.v
   $CC -v 2> $srcdir/gcc.v
   gccinclude=`cat $srcdir/gcc.v|grep specs|awk '{print $4}'| \
      sed -e 's|specs|include|g'`
   echo "gcc private include directory found."
   rm -f $srcdir/gcc.v
fi

dnl
dnl First entry must always be "variables.mak"
dnl
GENERATE_FILES="\
   autoconf/variables.mak \
   platforms/apccontrol \
   platforms/etc/changeme \
   platforms/etc/commfailure \
   platforms/etc/commok \
   platforms/etc/offbattery \
   platforms/etc/onbattery \
   platforms/etc/apcupsd.conf \
   examples/safe.apccontrol \
   $DFILES"

if test "$needlibusb"x = "true"x
then
   GENERATE_FILES="$GENERATE_FILES \
   include/libusb.h"
fi

if test "$ENABLE_CGI" = "yes"
then
   GENERATE_FILES="$GENERATE_FILES src/cgi/cgiconfig.h"
fi

AC_OUTPUT([ ${GENERATE_FILES} ], [])

chmod 755 examples/safe.apccontrol
$MAKE clean

#
# A whole lot of hand springs to get the compiler version.
#  This is because gcc changed the output in version 3.0
#
CXXVERSION=`${CXX} --version | tr '\n' ' ' | cut -f 3 -d ' '`
if test "x${CXXVERSION}" = "x" ; then
   CXXVERSION=`${CXX} --version | tr '\n' ' ' | cut -f 1 -d ' '`
fi

eval srcdir=${srcdir}
eval sbindir=${sbindir}
eval sysconfdir=${sysconfdir}
eval mandir=${mandir}

echo "

Configuration on `date`:

  Host:                       $host -- ${DISTNAME} ${DISTVER} 
  Apcupsd version:            ${VERSION} (${DATE})
  Source code location:       ${srcdir}
  Install binaries:           ${sbindir}
  Install config files:       ${sysconfdir}
  Install man files:          ${mandir}
  Nologin file in:            ${nologdir}
  PID directory:              ${PIDDIR}
  LOG dir (events, status)    ${LOGDIR}
  LOCK dir (for serial port)  ${LOCKDIR}
  Power Fail dir              ${PWRFAILDIR}
  Compiler:                   ${CXX} ${CXXVERSION}
  Preprocessor flags:         ${CPPFLAGS}
  Compiler flags:             ${CXXFLAGS} ${PTHREAD_FLAGS}
  Linker:                     ${LD}
  Linker flags:               ${LDFLAGS}
  Host and version:           ${DISTNAME} ${DISTVER}
  Shutdown Program:           ${SHUTDOWN}
  Port/Device:                ${SERIALDEV}
  Network Info Port (CGI):    ${NISPORT}
  UPSTYPE                     ${UPSTYPE}
  UPSCABLE                    ${UPSCABLE}

  drivers (no-* are disabled): ${APCSMART_DRIVER:-no-apcsmart} ${DUMB_DRIVER:-no-dumb} ${NET_DRIVER:-no-net} ${DISPLAY_USB_DRIVER:-no-usb} ${DISPLAY_SNMP_DRIVER:-no-snmp} ${PCNET_DRIVER:-no-pcnet} ${MODBUS_DRIVER:-no-modbus} ${MODBUS_USB_DRIVER:-no-modbus-usb} ${TEST_DRIVER:-no-test}

  enable-nis:                 ${NISSRV_ENABLED}
  with-nisip:                 ${NISIP}
  enable-cgi:                 ${ENABLE_CGI}
  with-cgi-bin:               ${CGIBIN}
  with-libwrap:               ${with_libwrap}
  enable-pthreads:            ${with_pthreads}
  enable-dist-install:        ${with_distdir}
  enable-gapcmon:             ${GAPCMON_ENABLED}
  enable-apcagent:            ${APCAGENT_ENABLED}
  " > config.out

cat config.out

echo Configuration complete: Run \'$MAKE\' to build apcuspd.
echo

echo $PATH | grep /usr/ucb >/dev/null
if test $? = 0 ; then
   AC_MSG_WARN([ 
      WARNING! You have /usr/ucb on your path. We STRONGLY recommend
         that you remove it and rerun configure. Otherwise
         apcupsd may be unable to shutdown your system.
   ]);
fi
