#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/resolv               */
#*    -------------------------------------------------------------    */
#*    Author      :  Jose Romildo                                      */
#*    Creation    :  Sat Nov 11 13:27:23 1995                          */
#*    Last change :  Mon Jul 10 13:54:46 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check for the host resolv (glibc included) library.              */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=
resolvlib="resolv"
type=

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

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

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

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

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

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $cflags $file.c -o $aout -l$resolvlib >/dev/null"

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

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>
#include <netdb.h>

#define N 4096

int main( int argc, char *argv[] ) {
    u_char nsbuf[ N ];
    char dispbuf[ N];
    ns_msg msg;
    ns_rr rr;
    int i, l;

    l = res_query( argv[ 1 ], ns_c_any, ns_t_a, nsbuf, sizeof( nsbuf ) );
    if( l >= 0 ) {
       ns_initparse( nsbuf, l, &msg );
       l = ns_msg_count( msg, ns_s_an );
       for( i = 0; i < l; i++ ) {
	  ns_parserr( &msg, ns_s_an, 0, &rr );
	  ns_sprintrr( &msg, &rr, NULL, NULL, dispbuf, sizeof( dispbuf ) );
       }
       return 0;
    }
    return 0;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   eval "$HOSTSH $aout hop.inria.fr"
   ret_code=$?
   \rm -f $file.*
   rm -f $aout
   rm -rf $aout*
   if [ $ret_code = 0 ]; then
     echo "$resolvlib"
   else
     echo "no"
   fi
   exit 0
else
   \rm -f $file.*
   echo "no"
   exit 0
fi

