#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/setsockopt           */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Mon Jul 10 13:54:01 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check for setsockopt function and options.                       */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=
solaris_lib="-lsocket -lnsl"
mingw_lib="-lws2_32"
tcp_opt=TCP_NODELAY

#*---------------------------------------------------------------------*/
#*    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]*=//'`";;

    --opt=*)
      tcp_opt="`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 "-lws2_32" # -lwsock32 -lmswsock"
  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>
#ifdef _MINGW_VER
#include <winsock2.h>
#endif
#include <netinet/in.h>
#include <netinet/tcp.h>

int main( int argc, char *argv[] ) {
   int s = socket( AF_INET, SOCK_STREAM, 0 );
   int sock_opt = 1;

   setsockopt( s, IPPROTO_TCP, 
EOF

echo $tcp_opt >> $file.c

cat >> $file.c <<EOF
, &sock_opt, sizeof( sock_opt ) );
   return 0;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile" 2> /dev/null; then
   echo "1"
else
   echo "0"
fi

rm -f $aout
rm -rf $aout*
rm -f $file.*
