#! /bin/sh
# $Id: configure,v 1.4 2004/05/29 14:46:09 mjt Exp $
# autoconf-style configuration script
# Author: Michael Tokarev <mjt@corpit.ru>
# License: GPL

name=proxycheck

case "$1" in
  --help | --hel | --he | --h | -help | -hel | -he | -h )
    cat <<EOF
configure: configure $name package.
Usage: ./configure [options]
where options are:
 --help - print this help and exit
EOF
    exit 0
    ;;
  "") ;;
  *) echo "configure: unknown option \`$1'" >&2; exit 1 ;;
esac

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

set -e
rm -f conftest* confdef* config.log
exec 5>config.log
cat <<EOF >&5
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.

EOF
cat >confdef.h <<EOF
/* $name autoconfiguration header file.
 * Generated automatically by configure. */

EOF
touch confdef.sed

case `echo "a\c"` in
  *c*) en=-n ec= ;;
  *)   en= ec='\c' ;;
esac

subst=
ac_define() {
  echo "#define $1	${2:-1}" >>confdef.h
}
ac_subst() {
  subst="$subst $*"
}
ac_error() {
  if [ -n "$*" ]; then
    echo "configure: $*" >&2
  fi
  echo "configure: see config.log and conftest.* for possible explanation"
  exit 1
}
ac_checking() {
  echo $en "checking $1... $ec" >&2
  echo >&5
  echo "configure: *** checking for $1 ***" >&5
}

read VERSION_DATE VERSION < CHANGES
ac_subst VERSION VERSION_DATE

echo "Configuring $name $VERSION ($VERSION_DATE)"
echo

### check for C compiler.  Set $CC
###
ac_checking "for C compiler"
rm -f conftest*; cat >conftest.c <<EOF
int main(int argc, char **argv) { return 0; }
EOF
if [ -n "$CC" ]; then
  if $CC -o conftest conftest.c 2>&5 && ./conftest 2>&5 ; then
    echo "\$CC ($CC)" >&2
  else
    echo no >&2
    ac_error "\$CC ($CC) is not a working compiler"
  fi
else
  for cc in gcc cc ; do
    if $cc -o conftest conftest.c 2>&5 && ./conftest 2>&5 ; then
      echo $cc
      CC=$cc
      break
    fi
  done
  if [ -z "$CC" ]; then
    echo no
    ac_error "no working C compiler found in \$PATH"
  fi
fi
ac_subst CC

if [ -z "$CFLAGS" ]; then
  ac_checking "whenever C compiler ($CC) is GNU CC"
  rm -f conftest*; cat >conftest.c <<EOF
#ifdef __GNUC__
  yes_it_is_gcc;
#endif
EOF
  if $CC -E conftest.c 2>&5 | grep yes_it_is_gcc >/dev/null ; then
    echo yes
    CFLAGS="-Wall -W -O2"
  else
    echo no
    CFLAGS=-O
  fi
fi
cc="$CC $CFLAGS"
if [ -z "$LDFLAGS" ]; then LDFLAGS='$(CFLAGS)'; ldflags=
else ldflags="$LDFLAGS"
fi
ccld="$cc $ldflags -o conftest conftest.c"
cpp="$cc -E conftest.c"
cc="$cc -c conftest.c"
ac_subst CFLAGS LDFLAGS

ac_run() {
  ac_checking "$1"
  rm -f conftest*; cat >conftest.c
  if $ccld $2 2>&5 && ./conftest 2>&5 ; then
    echo ${3:-yes} >&2
    return 0
  else
    echo ${4:-no} >&2
    return 1
  fi
}
ac_link() {
  ac_checking "$1"
  rm -f conftest*; cat >conftest.c
  if $ccld $2 2>&5 ; then
    echo ${3:-yes} >&2
    return 0
  else
    echo ${4:-no} >&2
    return 1
  fi
}
ac_cpp() {
  ac_checking "$1"
  rm -f conftest*; cat >conftest.c
  if $cpp $2 2>&5 ; then
    echo ${3:-yes} >&2
    return 0
  else
    echo ${4:-no} >&2
    return 1
  fi
}

ac_run "whenever C compiler ($CC) works" <<EOF >/dev/null || exit 1
#include <stdio.h>
int main(int argc, char **argv) {
  puts("hello, world!");
  return 0;
}
EOF

if ac_link "for socket routines" <<EOF
int main() { connect(); accept(); return 0; }
EOF
then
  LIBSOCKET=
else
  LIBSOCKET="-lsocket -lnsl"
  ac_link "for socket routines in $LIBSOCKET" "$LIBSOCKET" <<EOF || ac_error
int main() { connect(); accept(); return 0; }
EOF
fi
ac_subst LIBSOCKET

if ac_link "for gethostbyname()" $LIBSOCKET <<EOF
int main() { gethostbyname(); return 0; }
EOF
then
  LIBRESOLV=
else
  LIBRESOLV="-lresolv"
  ac_link "for gethostbyname() in $LIBRESOLV" "$LIBRESOLV $LIBSOCKET" <<EOF || ac_error
int main() { gethostbyname(); return 0; }
EOF
fi
ac_subst LIBRESOLV

ev_methods=select
add_method() {
 ev_methods="$* $ev_methods"
}

ac_cpp "for <sys/select.h>" <<EOF >/dev/null && ac_define HAVE_SYS_SELECT_H
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/select.h>
int foo() { return 0; }
EOF

ac_link "for poll()" <<EOF && ac_define HAVE_POLL && add_method poll
#include <sys/types.h>
#include <sys/poll.h>
int main() {
  struct pollfd pfd[2];
  return poll(pfd, 2, 10);
}
EOF

ac_link "for epoll" <<EOF && ac_define HAVE_EPOLL && add_method epoll
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/epoll.h>
int main() {
  struct epoll_event ev;
  ev.events = EPOLLIN;
  ev.data.fd = 0;
  epoll_create(10);
  epoll_ctl(10, EPOLL_CTL_ADD, 0, &ev);
  return 0;
}
EOF

ac_link "for kqueue" <<EOF && ac_define HAVE_KQUEUE && add_method kqueue
#include <sys/types.h>
#include <sys/time.h>
#include <sys/event.h>
int main() {
  struct kevent ke;
  EV_SET(&ke, 1, EVFILT_READ, EV_ADD, 0, 0, 0);
  kqueue();
  kevent(10, &ke, 1, 0, 0, 0);
  return 0;
}
EOF

ac_link "for devpoll" <<EOF && ac_define HAVE_DEVPOLL && add_method devpoll
#include <sys/types.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <sys/devpoll.h>
#include <sys/ioctl.h>
int main() {
  struct pollfd pfd;
  dvpoll_t dp;
  pfd.fd = 10;
  pfd.events = POLLIN|POLLREMOVE;
  dp.dp_timeout = 10;
  dp.dp_nfds = 1;
  dp.dp_fds = &pfd;
  ioctl(10, DP_POLL, &dp);
  return 0;
}
EOF

echo "Using the following I/O multiplexing methods: $ev_methods" >&2

ac_link "for memmem()" <<EOF && ac_define HAVE_MEMMEM
#include <string.h>
int main(int argc, char **argv) {
  memmem(argv[0], 'a', 10);
  return 0;
}
EOF

echo $en "creating Makefile... $ec"
for var in $subst LIBS DEFS ; do
  eval echo "\"s|@$var@|\$$var|\""
done >>confdef.sed
rm -f Makefile.tmp
echo "# Automatically generated from Makefile.in by configure" >Makefile.tmp
echo "#" >>Makefile.tmp
sed -f confdef.sed Makefile.in >>Makefile.tmp
chmod +x Makefile.tmp
mv -f Makefile.tmp Makefile
echo ok

echo $en "creating config.h... $ec"
mv -f confdef.h config.h
echo ok

echo "all done."
rm -f conftest* confdef*
exit 0
