#!/bin/sh
# Gush! I hate writing configure scripts...

CFLAG="-O2"

src=a$$.c
prog=a$$
sysdeps_h=src/sysdeps.h
ipv6ok="";
debugok="#undef DEBUG";
ipv6resolve="";
socketinlib="";

rm -f Rules.make
rm -f src/aesop.h
rm -f $sysdeps_h

umask 077

for arg
do
  option=`echo $arg | sed s/\-\-enable\-//`
  case "$arg" in
    --enable*)
      case "$option" in
        ipv6)
          ipv6ok="#define IPV6";;
        debug)
          debugok="#define DEBUG";;
      esac
      ;;
    --disable*)
      case "$option" in
        ipv6)
          ipv6ok="#undef IPV6";;
        debug)
          debugok="#undef DEBUG";;
      esac
      ;;
    --help)
      printf "\nconfigure --enable[-option] enables an option.\n"
      printf "configure --disable[-option] disables an option.\n\n"
      printf "valid options:\n"
      printf "ipv6: Adds IPv6 support\n"
      printf "debug: Adds debugging support\n"
      exit 1;
  esac
done

printf "Configuring for Aesop\n"

printf "Checking for a working c compiler..."
if [ -n "$CC" ]; then
   printf " $CC\n";
else
  CC=`./which gcc`
  if [ -z "$CC" ]; then
    CC=`./which cc`
  else
    CFLAG="$CFLAG -Wall"
  fi
  if [ -z "$CC" ]; then
    printf " none.\nAborting.\n"
    exit 1
  else
    printf " $CC\n"
  fi
fi

printf "Checking for strip..."
STRIP=`./which strip`
if [ -z "$STRIP" ]; then
  printf " none.\n"
else
  printf " $STRIP\n"
fi

printf "Checking if datatype socklen_t exists..."
cat >$src <<_STOP
#include <sys/types.h>
#include <sys/socket.h>
int main() {
   socklen_t blah = 5;
}
_STOP
$CC -o $prog $src >/dev/null 2>&1
if [ -x $prog ]; then
  printf " yes\n"
  printf "#define have_socklen_t\n" >> $sysdeps_h
else
  printf " no\n"
fi
rm -f $src $prog

printf "Checking if gethostbyname needs -lnsl and eventually -lsocket..."
cat >$src <<_STOP
int main() {
   char *f = gethostbyname();
}
_STOP
$CC -o $prog $src >/dev/null 2>&1
if [ -x $prog ]; then
  printf " no\n"
else
  $CC -o $prog $src -lnsl >/dev/null 2>&1
  if [ -x $prog ]; then
    LIBS="$LIBS -lnsl"
    printf " yes, it needs -lnsl\n"
  else
    $CC -o $prog $src -lnsl -lsocket >/dev/null 2>&1
    if [ -x $prog ]; then
      LIBS="$LIBS -lnsl -lsocket"
      socketinlib="1"
      printf " yes, it needs -lnsl and -lsocket\n"
    else
      printf " no\n"
      printf "Error: No resolver routes found, these are necessary for Aesop\n"
      rm -f $src $prog
      exit 1
    fi
  fi
fi
rm -f $src $prog

if [ -z "$socketinlib" ]; then
  printf "Checking if socket needs -lsocket..."
  cat >$src <<_STOP
int main() {
   char *f = socket();
}
_STOP
  $CC -o $prog $src >/dev/null 2>&1
  if [ -x $prog ]; then
    printf " no\n"
  else
    $CC -o $prog $src -lsocket >/dev/null 2>&1
    if [ -x $prog ]; then
      if [ -z "$socketinlib" ]; then
        LIBS="$LIBS -lsocket"
      fi
      printf " yes\n"
    fi
  fi
  rm -f $src $prog
fi

if [ -n "$ipv6ok" ]; then
  printf "Checking for IPv6 resolver routines...\n"
  for func in gethostbyname2 getipnodebyname
  do
    printf "Checking for $func..."
    cat >$src <<_STOP
int main() {
   char *f = $func();
}
_STOP
    $CC $LIBS -o $prog $src >/dev/null 2>&1
    if [ -x $prog ]; then
      printf " yes\n"
      ipv6resolve="1"
      printf "#define have_$func\n" >> $sysdeps_h
    else
      printf " no\n"
    fi
    rm -f $src $prog
  done
  if [ -z "$ipv6resolve" ]; then
    echo "\nError: IPv6 Support enabled, and no IPv6 resolver routines found"
    echo "Either gethostbyname2() or getipnodebyname() is needed"
    exit 1
  fi
fi

for func in inet_pton isblank
do
printf "Checking for $func..."
  cat >$src <<_STOP
int main() {
char *f = $func();
}
_STOP
$CC $LIBS -o $prog $src >/dev/null 2>&1
if [ -x $prog ]; then
  printf " yes\n"
  printf "#define have_$func\n" >> $sysdeps_h
else
  printf " no\n"
fi
rm -f $src $prog
done

printf "Checking for header sys/sockio.h..."
if [ -r "/usr/include/sys/sockio.h" ]; then
  printf " yes\n"
  printf "#include <sys/sockio.h>\n" >> $sysdeps_h
else
  printf " no\n"
fi

printf "Creating src/aesop.h..."
cat src/aesop.h.in | sed -e s/@IPV6@/"$ipv6ok "/ -e s/@DEBUG@/"$debugok"/ > src/aesop.h
printf " done\n"

printf "Creating src/Rules.make..."
cat src/Rules.make.in | sed -e s/@CFLAG@/"$CFLAG"/ -e s%@LIBS@%"$LIBS"% -e s%@CC@%"$CC"% \
 -e s%@STRIP@%"$STRIP"%> src/Rules.make
printf " done\n"
