#! /bin/sh
# $Id: configure,v 1.4 2007/01/07 23:19:40 mjt Exp $
# autoconf-style configuration script
#

set -e

name=udns

if [ -f udns.h -a -f udns_resolver.c ] ; then :
else
  echo "configure: error: sources not found at `pwd`" >&2
  exit 1
fi

options="ipv6"

for opt in $options; do
  eval enable_$opt=
done

if [ -f config.status ]; then
  . ./config.status
fi

enable() {
  opt=`echo "$1" | sed 's/^--[^-]*-//'`
  case "$opt" in
    ipv6|stats|master_dump|zlib|dso) ;;
    master-dump) opt=master_dump ;;
    *) echo "configure: unrecognized option \`$1'" >&2; exit 1;;
  esac
  eval enable_$opt=$2
}

while [ $# -gt 0 ]; do
  case "$1" in
    --disable-*|--without-*|--no-*) enable "$1" n;;
    --enable-*|--with-*) enable "$1" y;;
    --help | --hel | --he | --h | -help | -hel | -he | -h )
      cat <<EOF
configure: configure $name package.
Usage: ./configure [options]
where options are:
 --enable-option, --with-option --
   enable the named option/feature
 --disable-option, --without-option, --no-option --
   disable the named option/feature
 --help - print this help and exit
Optional features (all enabled by default if system supports a feature):
  ipv6 - enable/disable IP version 6 (IPv6) support
EOF
      exit 0
      ;;
    *) echo "configure: unknown option \`$1'" >&2; exit 1 ;;
  esac
  shift
done

. ./configure.lib

ac_msg "configure"
ac_result "$name package"

ac_prog_c_compiler_v
ac_prog_ranlib_v

ac_ign ac_yesno "for getopt()" ac_have GETOPT ac_link <<EOF
#include <stdio.h>
extern int optind;
extern char *optarg;
extern int getopt(int, char **, char *);
int main(int argc, char **argv) {
  getopt(argc, argv, "abc");
  return optarg ? optind : 0;
}
EOF

ac_ign \
 ac_yesno "for inet_pton() && inet_ntop()" \
 ac_have INET_PTON_NTOP \
 ac_link <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main() {
  char buf[64];
  long x = 0;
  inet_pton(AF_INET, &x, buf);
  return inet_ntop(AF_INET, &x, buf, sizeof(buf));
} 
EOF

if ac_yesno "for socklen_t" ac_compile <<EOF
#include <sys/types.h>
#include <sys/socket.h>
int foo() { socklen_t len; len = 0; return len; }
EOF
then :
else
  ac_define socklen_t int
fi

if ac_library_find_v 'socket and connect' "" "-lsocket -lnsl" <<EOF
int main() { socket(); connect(); return 0; }
EOF
then :
else
  ac_fatal "cannot find libraries needed for sockets"
fi

if [ n != "$enable_ipv6" ]; then
if ac_yesno "for IPv6" ac_have IPv6 ac_compile <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
  struct sockaddr_in6 sa;
  sa.sin6_family = AF_INET6;
  return 0;
}
EOF
then :
elif [ "$enable_ipv6" ]; then
  ac_fatal "IPv6 is requested but not available"
fi
fi # !disable_ipv6?

if ac_yesno "for poll()" ac_have POLL ac_link <<EOF
#include <sys/types.h>
#include <sys/poll.h>
int main() {
  struct pollfd pfd[2];
  return poll(pfd, 2, 10);
}
EOF
then :
else
  ac_ign ac_yesno "for sys/select.h" ac_have SYS_SELECT_H ac_cpp <<EOF
#include <sys/types.h>
#include <sys/select.h>
EOF
fi

# HACK FIXME - use 32bit on Mac
os=`uname -s`;
if [ $os = "Darwin"  ]; then
  CFLAGS="$CFLAGS -m32"
fi

ac_config_h
ac_output Makefile
ac_msg "creating config.status"
rm -f config.status
{
echo "# automatically generated by configure to hold command-line options"
echo
found=
for opt in $options; do
  eval val=\$enable_$opt
  if [ -n "$val" ]; then
    echo enable_$opt=$val
    found=y
  fi
done
if [ ! "$found" ]; then
  echo "# (no options encountered)"
fi
} > config.status
ac_result ok

ac_result "all done."
exit 0
