#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/getaddrinfo          */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Mon Jul 10 14:01:20 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check for the getaddrinfo in replacement of gethostbyname.       */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=$CFLAGS
solaris_lib="-lsocket -lnsl"
mingw_lib="-lws2_32"

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    --lib=*|-lib=*)
      lib="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$TMP/actest$USER
aout=$TMP/Xactest$USER

#*---------------------------------------------------------------------*/
#*    mingw is super clear                                             */
#*---------------------------------------------------------------------*/
if [ "$HOSTOS " = "mingw " ]; then
  echo 1 #"-lws2_32" 
  exit 0
fi

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $cflags $file.c -o $aout $lib >/dev/null"
compile_solaris="$CC $cflags $file.c -o $aout $solaris_lib >/dev/null"
compile_mingw="$CC -D_MINGW_VER $cflags $file.c -o $aout $mingw_lib >/dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#ifdef __FreeBSD__
#  include <netinet/in.h>
#endif
#ifdef BGL_ANDROID
#  include <linux/in.h>
#endif
#include <arpa/inet.h>

int main( int argc, char *argv[] ) {
   int status;
   struct addrinfo hints;
   struct addrinfo *res, *rp;       
   char host[ 100 ];

   hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_protocol = 0;
   hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;

   if( !(status = getaddrinfo( "localhost", 0L, &hints, &res )) ) {
      for( rp = res; rp != NULL; rp = rp->ai_next ) {
	 if( rp->ai_canonname ) printf( " canonname=%s\n", rp->ai_canonname );
         printf( " ip=%s\n", inet_ntoa( ((struct sockaddr_in *)(rp->ai_addr))->sin_addr ) );
      }
   } else {
      printf( "fail: %s\n", gai_strerror( status ) );
   }

   if( !(status = getnameinfo( ((struct sockaddr *)(res->ai_addr)),
			       res->ai_addrlen,
			       host, sizeof( host ), 0, 0, 0 )) ) {
      printf( "host=%s\n", host );
   }

   freeaddrinfo( res ); 
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile" 2> /dev/null; then
   \rm -f $file.*
   rm -f $aout
   rm -rf $aout*
   echo "1"
else
   \rm -f $file.*
   rm -f $aout
   rm -rf $aout*
   echo "0"
fi
exit 0
