:

mydir=$1
resolv_ok=''

old="$resolv_dir"
resolv_dir=''
resolv_include_dir=''
resolv_lib_dir=''
resolv_link=''
resolv_sendok=''
resolv_seqpacket=''

while $test "$resolv_ok" = "" ; do

    for a in $old trysystem none; do		
	case "$resolv_dir" in 	  
	    '')
    		case "$a" in
		    none)
			resolv_dir=none
			;;
		    system)
			resolv_dir=system
			;;
		    
		    *)
			if $test -r /usr/include/resolv.h -a \
                            "`$ls /usr/$libdirname/libresolv.* 2>/dev/null`" != "" 
			then
			    resolv_dir=system
			    echo "Found resolver library from system directory"
			elif $test -r /usr/include/resolv.h -a  \
			    "`$ls /usr/lib/libresolv.* 2>/dev/null`" != "" 
			then
			    resolv_dir=system
			    echo "Found resolver library from system directory"
			elif $test -r /usr/include/resolv.h -a \
			    "$found_libcso" != ""
			then 
                            d="`$dirname \"$found_libcso\"`"
			    if $test "`$ls $d/libresolv.* 2>/dev/null`" != "" 
                            then
				resolv_dir=system
				echo "Found resolver from system directory"
                            fi                             
			fi
			
			;;
		    
		    *)

			if $test -r $a/include/resolv.h -a \
                            "`$ls $a/$libdirname/libresolv.* 2>/dev/null`" != "" 
			then
			    resolv_dir="$a"
			    echo "Found resolver library from $resolv_dir"
			elif $test -r $a/include/resolv.h -a  \
			    "`$ls $a/lib/libresolv.* 2>/dev/null`" != "" 
			then
			    resolv_dir="$a"
			    echo "Found resolver library from $resolv_dir"
			fi
			;;

		esac
		;;
	esac
    done

   cat <<EOM

Elm ME+ can use resolver library for some DNS lookups. That is
implemented as shared library libelmme-resolv.so. Give directory
under which there are library and include files of resolver library.
Give "none" to disable generation of shared library. Give "system" to 
indicate that resolver include and library files are search path of 
compiler.

EOM
    dflt="$resolv_dir"

    rp="Resolver directory prefix? [$dflt]"

    echo $n "$rp $c"
    . myread

    staticlib=''

    case "$ans" in
	none)
	    resolv_dir=none
	    resolv_ok="$undef"
	    echo "libelmme-resolv.so disabled"
	;;
	system)
	    resolv_include_dir=''
	    resolv_lib_dir=''
	    resolv_dir=system
	    resolv_ok="$define"
	    echo "Resolver library is assumed to found from system locations"

	    if $test -r /usr/$libdirname/libresolv.a 
	    then
		staticlib=/usr/$libdirname/libresolv.a
		echo "  (static library $staticlib)"
	    elif $test -r /usr/lib/libresolv.a
	    then
		staticlib=/usr/lib/libresolv.a
		echo "  (static library $staticlib)"
	    elif "$found_libcso" != ""
	    then 
                d="`$dirname \"$found_libcso\"`"
		if $test -r $d/libresolv.a
		then
		    staticlib=$d/libresolv.a
		    echo "  (static library $staticlib)"
		fi
	   fi

	;;
	*)
	    resolv_dir="$ans"
	    resolv_include_dir="$resolv_dir/include"
	    X="`$ls $resolv_dir/$libdirname/libresolv.* 2>/dev/null`"
	    if $test "$X" != ""
	    then
		resolv_lib_dir="$resolv_dir/$libdirname"
	    else
	    	resolv_lib_dir="$resolv_dir/lib"
	    fi
	    resolv_ok="$define"
	    echo "Resolver library is assumed to found from $resolv_dir directory"

	    if $test -r $resolv_lib_dir/resolv.a
	    then
		staticlib=$resolv_lib_dir/resolv.a
		echo "  (static library $staticlib)"
	    fi

	    ;;
    esac

    if $test "$resolv_ok" = "$define"; then	
	cat >testconfig.h <<EOF
#$d_strings     STRINGS
#$i_netinet_in  I_NETINET_IN
#$d_have_in6    HAVE_IN6
#$i_arpa_inet   I_ARPA_INET
#$i_netdb       I_NETDB 
#$d_use_xtox_pn USE_INET_XTOX_PN

EOF

	rpathlist=''
	linklist=''
	if $test "$resolv_lib_dir" != "" ; then
	    linklist="-L$resolv_lib_dir"
	    rpathlist="$rpath_opt$resolv_lib_dir"
	fi

	inclist=''
	if $test "$resolv_include_dir" != "" ; then
	    inclist="-I$resolv_include_dir"
	fi

	cp "$source_dir/shared_libs/$doconfig/resolvertest.c" tryresolv.c 2>/dev/null \
	    || $echo "Oh no!  I cannot find the \"shared_libs/$doconfig/resolvertest.c\" file."

	if $cc -c $ccflags  $inclist tryresolv.c > try.log 2>&1
	then
	    echo "Test compilation resolv routines succeed ..."

	    resolv_link=BAD

	    for link in '-lresolv' $staticlib
	    do
	      case "$resolv_link" in
		  BAD)
		  
		  $echo $n "  Trying linking with $link ... $c"
		  
		  if $cc -o resolvertest  $linklist $rpathlist \
		      tryresolv.o $link > try.log 2>&1
		      then
		      resolv_link="$link"
		      echo "OK"
		  else
		      echo "(failed)"
		  fi
		  ;;
	      esac
	    done

	    if $test "$resolv_link" != BAD
	    then

		./resolvertest query ns .

		case $? in
		    0)
		    echo "Calling of resolver succeed..."
		    ;;
		    1)
		    echo "resolvertest usage error?"
		    resolv_ok=''
		    ;;
		    2)
		    echo "resolvertest does not found root (.) ?"
		    resolv_ok=''
		    ;;
		    3)
		    echo "resolvertest does not found root (.) ?"
		    echo "Network not available? Assuming OK."
		    ;;
		    *)
		    echo "Unexpected failure from resolvertest"
		    resolv_ok=''
		    ;;
		esac

	   else
		resolv_ok=''
		echo "Failed to link with resolver library."
		# cat try.log
	    fi

	else
	    resolv_ok=''
	    echo "Failed to compile resolv routines"
	    # cat try.log
	fi

        rm -f try.log tryresolv.c resolvertest tryresolv.o 

	case "$resolv_ok" in
	'')
	    cat <<EOF

Resolver location $resolv_dir seems not work. Select on of following
    d) Disable libelmme-resolv.so
    c) Change resolver library directory prefix

EOF

	    dflt=d
	    rp="Your selection? [$dflt]"
	    echo $n "$rp $c"
	    . myread
	    
	    if $test "$ans" = d; then
		resolv_dir=none
		resolv_ok="$undef"
		echo "libelmme-resolv.so disabled"	
	    fi
	;;
	esac
    fi
	    	    
done

if $test "$resolv_ok" = "$define"; then		

    echo "Testing res_mkquery(), res_send() and related routines"

    : hostalias test is excluded from resolvertest2	
    : because RESOLV_HOSTALIAS is not defined during test

    cp "$source_dir/shared_libs/$doconfig/resolvertest2.c" tryresolv2.c 2>/dev/null \
	|| $echo "Oh no!  I cannot find the \"shared_libs/$doconfig/resolvertest2.c\" file."

    if $cc -c $ccflags  $inclist tryresolv2.c > try2.log 2>&1
    then
	echo "Test compilation for res_mkquery(), res_send() and related routines succeed"

	$echo $n "  Trying linking with $resolv_link ... $c"

	if $cc -o resolvertest2  $linklist $rpathlist tryresolv2.o $resolv_link > try2.log 2>&1
	then
	    echo "OK"
	    resolv_sendok="$define"
	    
	    ./resolvertest2 query ns .
	    
	    case $? in
		0)
		    echo "Calling of resolver succeed..."
		    ;;
		1)
		    echo "resolvertest2 usage error?"
		    resolv_sendok="$undef"
		    ;;
		2)
		    echo "resolvertest2 does not found root (.) ?"
		    resolv_sendok="$undef"
		    ;;
		3)
		    echo "resolvertest2 does not found root (.) ?"
		    echo "Network not available? Assuming OK."
		    ;;
		 *)
		     echo "Unexpected failure from resolvertest2"
		     resolv_sendok="$undef"
		     ;;
	    esac
	else
	    echo "Failed to link with resolver library."
	    resolv_sendok="$undef"
	    cat try2.log
	fi
	
    else
	resolv_sendok="$undef"
	echo "Failed to compile test for res_mkquery(), res_send() and related routines"
	cat try2.log
    fi

    rm -f try2.log tryresolv2.c resolvertest2 tryresolv2.o

    if $test "$d_have_socket" = "$define" ; then
	$echo $n "Testing sockectpair() for SOCK_SEQPACKET ... $c"

	$cat > seqpacket.c <<EOF
#include <stdio.h>
#if defined(__STDC__)
#include <stdlib.h>
#endif

#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <sys/wait.h>

struct message {
      int number;
      char text[256];
} NULL_message;

#if defined(__STDC__)
int main (int argc, char * argv[]);
#endif 
int main (argc,argv)
   int argc;
   char * argv[];
{
     int pair[2];
     int pid,r;
     struct message A = NULL_message;
     struct message B = NULL_message;
     int rw,status = 0;

     A.number = getpid();
     strcpy(A.text,"Foobar");
    
     r = socketpair(AF_UNIX,SOCK_SEQPACKET,0,pair);
     if (-1 == r ) {
            perror("socketpair");
            return 1;
     }

     pid = fork();
     if (-1 == pid) {
          perror("fork");
          return 1;
     } else if (0 == pid) {
          int r1;
          close(pair[1]);

          r1 = send(pair[0],&A,sizeof A,MSG_EOR);
          if (-1 == r1) {
              perror("send");
              return 2;
          } else if (r1 != sizeof A) {
               fprintf(stderr,"send returned %d (should be %lu)\n",
                      r1,(long unsigned)(sizeof A));
               return 3;
          }
          return 0;
     }

     close(pair[0]);     

     /* Assumes that whole message is received at once */
     r = recv(pair[1],&B,sizeof B, 0);
     if (-1 == r) {
         perror("recv"); 
         return 4;
     } else if (r != sizeof B) {
         fprintf(stderr,"recv returned %d (should be %lu)\n",
                 r,sizeof B);
         return 5;
     } else if (0 != memcmp(&A,&B,sizeof B)) {
         fprintf(stderr,"data mismatch\n");
         return 5;
      }

      rw = waitpid(pid,&status,0);
      if (-1 == rw) {
          perror("waitpid"); 
          return 1;
      } 

      if (WIFSIGNALED(status)) {
           fprintf(stderr,"Child got signal %d\n",WTERMSIG(status));
           return 1;
      }

      if (WIFEXITED(status)) {
           int stat = WEXITSTATUS(status);
           if (stat) {
                 fprintf(stderr,"Child exited with status %d\n",stat);
            }
            return stat;
      }
      return 1;
}
EOF
	if $cc $ccflags -o  seqpacket  seqpacket.c > try3.log 2>&1 && ./seqpacket ; then
	    echo "Yes, it does."
	    resolv_seqpacket="$define"
	else
	    echo "NO -- seems not work"
	    resolv_seqpacket="$undef"
	    
	    cat try3.log
	fi
	
	rm -f  seqpacket  seqpacket.c  seqpacket.o  try3.log
	    
    else
	echo "Configure test for socket() have failed"
	resolv_seqpacket="$undef"
    fi

    echo
    $echo  $n "Testing hostalias() ... $c"
    
    $cat > hostalias.c <<EOF
#if defined(__STDC__) || defined(_AIX)
# define ANSI_C 1
#endif

#include <stdio.h>
#ifdef ANSI_C
#include <stdlib.h>
#endif

#include "testconfig.h"

#include <errno.h>
#ifndef ANSI_C
extern int errno;
#endif

#ifdef I_NETINET_IN
#include <netinet/in.h>
#endif

#ifdef I_ARPA_INET
#include <arpa/inet.h>
#endif

#include <arpa/nameser.h>
#include <resolv.h>

#if defined(__STDC__)
int main (int argc, char * argv[]);
#endif

int main (argc,argv)
   int argc;
   char * argv[];
{
  char * alias;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s <name>\n",argv[0]);
    return 1;
  }

  alias = hostalias(argv[1]);

  if (alias) {
      printf ("%s\n",alias);
  } else {
     fprintf(stderr, "%s: No alias for %s\n",
     		   argv[0],argv[1]);
     return 1;
  }

  return 0;
}
EOF

    if $cc $ccflags -o  hostalias  $linklist $rpathlist hostalias.c $resolv_link > try4.log 2>&1
    then
	echo "Compiles"
	
	HOSTALIASES="`pwd`/hostaliases.test"
	
	if echo "test www.example.com" > $HOSTALIASES
	then
	    export HOSTALIASES
	    
	    if ./hostalias test > hostalias.result
	    then
		
		result="`cat hostalias.result`"
		
		if $test "$result" = www.example.com
		then
		    
		    hostalias_ok=y
		    def=y
		    
		    if $test "$d_addrinfo" = "$define" ; then
			A=", gethostbyname() or getaddrinfo()"
		    else
			A=" or gethostbyname()"
		    fi
		    
		    if $test "$d_setgid"  = "$define"
		    then
			B="Elm ME+ is installed as setgid, and \$HOSTALIASES will not work."
			hostalias_ok=""
		    else
			B=""
		    fi
		    
		    if $grep "is deprecated" try4.log
		    then
		       echo
		       hostalias_ok=""
		       def=n
		    fi
		       
		    $cat <<MSG
hostalias() returned correct result $result

hostalias()$A look host aliases from file given
on \$HOSTALIASES environment variable. This
is not done if program is setuid or setgid.
$B

Shared library libelmme-resolv.so may use hostalias()
for some lookups. Disabling does not affect other
use of \$HOSTALIASES environment variable.

MSG
			    
		    if $test "$hostalias_ok" = ""
		    then
			
			case "$resolv_hostalias" in
			    "$define") dflt=y;;
			    "$undef")  dflt=n;;
			    *)  dflt=$def;;
			esac

			res=""
			rp="Do you want use hostalias() directly on shared library libelmme-resolv.so?  [$dflt]"
			
			while $test "$res" = "" ; do
		    	    echo $n "$rp $c"
    			    . myread	
			    
			    case "$ans" in
				n*|d*)
				    echo "Disabling direct use of hostalias() on resolver module"
				    hostalias_ok=n
				    res=allow
				    ;;
				y*|a*)
				    hostalias_ok=y
				    res=disable
				    ;;
				*)
				    echo "Use y or a to allow direct use of hostalias(), and n or d to disable dirext use of hostalias()"
				    ;;
			    esac
			done				
		    fi
		    
		    if  $test "$hostalias_ok" = y
		    then
			resolv_hostalias="$define"
		    else
			resolv_hostalias="$undef"						
			echo "hostalias() is not used directly on libelme-resolv module"
		    fi
		    
		else			
		    
		    echo "hostalias() returned result $result when www.example.com was expected"
		    resolv_hostalias="$undef"
		    
		fi
		
	    else
		echo "hostalias() seems not work"
		resolv_hostalias="$undef"
		
		echo "\$HOSTALIASES was $HOSTALIASES:"
		cat $HOSTALIASES
		
	    fi

	else
	    echo "... Enabling hostalias() anyway"

	    resolv_hostalias="$define"	
	fi
	
	rm -f $HOSTALIASES hostalias.result
	
    else
	    echo "Does not compile"
	    
	    cat try4.log
	    
            resolv_hostalias="$undef"
    fi

    rm -f  hostalias hostalias.c hostalias.o  try4.log

else
    resolv_sendok="$undef"
    resolv_seqpacket="$undef"
    resolv_hostalias="$undef"
fi
rm -f testconfig.h


$spitshell <<EOT > "$build_dir/$mydir/config.res"
#  Use resolver library
resolv_ok='$resolv_ok'
resolv_dir='$resolv_dir'
resolv_include_dir='$resolv_include_dir'
resolv_lib_dir='$resolv_lib_dir'
resolv_link='$resolv_link'
resolv_sendok='$resolv_sendok'
resolv_seqpacket='$resolv_seqpacket'
resolv_hostalias='$resolv_hostalias'
EOT
