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

CFLAG="-O2"

src=a$$.c
prog=a$$
sysdeps_h=src/sysdeps.h
ipv6ok="";
libaesopok="";
debugok="";
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";;
	libaesop)
	  libaesopok="1";;
      esac
      ;;
    --disable*)
      case "$option" in
	ipv6)
	  ipv6ok="";;
	debug)
	  debugok="";;
	libaesop)
	  libaesopok="";;
      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 "libaesop: Built libaesop\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 a working c preprocessor..."
if [ -n "$CCP" ]; then
   printf " $CCP\n";
else
  CPP=`./which cpp`
  if [ -z "$CPP" ]; then
    printf " none.\nAborting.\n"
    exit 1
  else
    printf " $CPP\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/or -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 -lsocket >/dev/null 2>&1
    if [ -x $prog ]; then
      LIBS="$LIBS -lsocket"
      socketinlib="1"
      printf " yes, it needs -lsocket\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
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

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


if [ -n "$libaesopok" ]; then
  printf "Checking for header dlfcn.h..."
  if [ -r "/usr/include/dlfcn.h" ]; then
    printf " yes\n"
  else
    printf " no\n"
    printf "Error: header dlfcn.h is not found. Needed for libaesop\n"
    exit -1
  fi

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

  printf "Checking for RTLD_NEXT in dlfcn.h..."
  cat > $src <<EOF
#include <dlfcn.h>
#ifdef RTLD_NEXT
	yes
#endif
EOF
  if $CPP $src 2>/dev/null | egrep "yes" >/dev/null 2>&1; then
    printf " yes\n"
    printf "#define have_RTLD_NEXT\n" >> $sysdeps_h
  else
    printf " no\n"
    printf "Checking for RTLD_NEXT in dlfcn.h with _GNU_SOURCE..."
    cat > $src <<EOF
#define _GNU_SOURCE
#include <dlfcn.h>
#ifdef RTLD_NEXT
	yes
#endif
EOF
    if $CPP $src 2>/dev/null | egrep "yes" >/dev/null 2>&1; then
      printf " yes\n"
      printf "#define _GNU_SOURCE\n" >> $sysdeps_h
      printf "#define __USE_GNU\n" >> $sysdeps_h
      printf "#define have_RTLD_NEXT\n" >> $sysdeps_h
    else
      printf " no\n"
      printf "Checking for the location of libc..."
      if [ -n "$LIBC" ]; then
	printf "$LIBC\n";
      else
	LIBC=`ldd /bin/ls 2>/dev/null | grep "libc" | awk ' { print $3 } '`
	if [ -z "$LIBC" ]; then
	  printf " not found\n"
	  printf "Warning: defaulting to "libc.so". Set LIBC with the location of your libc\n"
	  printf "         If you want another location to be used.\n"
	  LIBC="libc.so"
	else
	  printf " $LIBC\n"
	fi 
	printf "#define LIBC $LIBC\n" >> $sysdeps_h
      fi
    fi
  fi
  rm -f $src
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"% -e s%@DLLIB@%"$DLLIB"%> src/Rules.make
printf " done\n"

printf "\nMake sure you edit src/sysdeps.h and src/Rules.make!\n"
printf "You can now run 'make'\n"
