#!/bin/sh
#
# net6emu r1: net6emuconf
# Configuration script for the API emulation code.
#
# Copyright (c) 2002 Christoph Pfisterer
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. 
#

echo "net6emu configuration script probing your build environment"
TESTFILE="_n6e_test"
LOGFILE="net6emuconf.log"

CC=${CC:-gcc}
CPPFLAGS=${CPPFLAGS:-}
CFLAGS=${CFLAGS:-}
LDFLAGS=${LDFLAGS:-}
LIBS=${LIBS:-}

echo "Using CC=$CC"
echo "      CPPFLAGS=$CPPFLAGS"
echo "      CFLAGS=$CFLAGS"
echo "      LDFLAGS=$LDFLAGS"
echo "      LIBS=$LIBS"

compile="$CC $CPPFLAGS $CFLAGS $LDFLAGS $TESTFILE.c $LIBS -o $TESTFILE"



rm -f net6emuconf.h $LOGFILE
echo "/* net6emuconf.h -- this is a generated file */" >net6emuconf.h



echo "Checking struct sockaddr_storage..."
cat >$TESTFILE.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
int main(void)
{
  struct sockaddr_storage ss;
  ((struct sockaddr *)&ss)->sa_family = 0;
  return 0;
}
EOF

echo $compile
if eval $compile >>$LOGFILE 2>&1 ; then
  echo "struct sockaddr_storage present."
  echo "#define N6E_HAVE_SOCKADDR_STORAGE 1" >>net6emuconf.h
else
  echo "struct sockaddr_storage not present. It will be redeclared."
fi
rm -f $TESTFILE.c $TESTFILE



echo "Checking struct addrinfo..."
cat >$TESTFILE.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main(void)
{
  struct addrinfo ai;
  ai.ai_flags = 0;
  return 0;
}
EOF

test_getaddrinfo=0
echo $compile
if eval $compile >>$LOGFILE 2>&1 ; then
  echo "struct addrinfo present."
  echo "#define N6E_HAVE_STRUCT_ADDRINFO 1" >>net6emuconf.h
  test_getaddrinfo=1
else
  echo "struct addrinfo not present. It will be redeclared."
  echo "Not checking for getaddrinfo() in consequence. It will be emulated."
fi
rm -f $TESTFILE.c $TESTFILE



if [ `uname` = "Darwin" ]; then
  case `uname -r` in [012345]*)   # up to Mac OS X 10.1.x / Darwin 5.x
    test_getaddrinfo=0            #  getaddrinfo() is somewhat broken
    echo "Skipping check for getaddrinfo() because it is known to be buggy on this"
    echo "system (Mac OS X up to 10.1.x alias Darwin up to 5.x). It will be emulated."
    ;;
  esac
fi

if [ $test_getaddrinfo -eq 1 ]; then
  echo "Checking getaddrinfo()..."
  cat >$TESTFILE.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main(void)
{
  struct addrinfo hints, *ai;
  int err;
  hints.ai_flags = 0;
  err = getaddrinfo((void*)0, (void*)0, &hints, &ai);
  freeaddrinfo(ai);
  gai_strerror(err);
  return 0;
}
EOF

  echo $compile
  if eval $compile >>$LOGFILE 2>&1 ; then
    echo "getaddrinfo() present."
    echo "#define N6E_HAVE_GETADDRINFO 1" >>net6emuconf.h
  else
    echo "getaddrinfo() not present, not complete, or misdeclared. Will be emulated."
  fi
  rm -f $TESTFILE.c $TESTFILE
fi



echo "Checking getnameinfo()..."
cat >$TESTFILE.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main(void)
{
  struct sockaddr sa;
  char hn[256];
  getnameinfo(&sa, sizeof(sa), hn, sizeof(hn), (void*)0, 0, 0);
  return 0;
}
EOF

echo $compile
if eval $compile >>$LOGFILE 2>&1 ; then
  echo "getnameinfo() present."
  echo "#define N6E_HAVE_GETNAMEINFO 1" >>net6emuconf.h
else
  echo "getnameinfo() not present or misdeclared. Will be emulated."
fi
rm -f $TESTFILE.c $TESTFILE



echo "net6emu configuration complete."
if [ -s $LOGFILE ]; then
  echo "Compiler messages were saved to $LOGFILE for inspection."
else
  rm -f $LOGFILE
fi
exit 0
