#!/bin/sh

set -e
PATH=/bin:/usr/bin:$PATH; export PATH
trap "rm -f tmp$$a.c tmp$$b.c tmp$$c.c tmp$$a.o tmp$$b.o tmp$$c.o" 0
target=port_ipv6

cat > tmp$$a.c <<EOF
#include <sys/types.h>
#include <netinet/in.h>
struct sockaddr_in6 xx;
EOF

cat > tmp$$b.c <<EOF
#include <sys/types.h>
#include <netinet/in.h>
struct in6_addr xx;
EOF

cat > tmp$$c.c <<EOF
#include <sys/types.h>
#include <netinet/in.h>

foo() {
	void *a = &in6addr_any;
}
EOF

cat > ${target}.h <<EOF

/* This file is automatically generated. Do Not Edit. */

#ifndef ${target}_h
#define ${target}_h

EOF

if gcc -c tmp$$a.c > /dev/null 2>&1
then
        echo "#define HAS_INET6_STRUCTS" >> ${target}.h
        if gcc -c tmp$$b.c > /dev/null 2>&1
        then
		def=
	else
                echo "#define in6_addr in_addr6" >> ${target}.h
		def="-Din6_addr=in_addr6"
        fi
        if gcc $def -c tmp$$c.c > /dev/null 2>&1
        then
		:
	else
		echo "#define MISSING_IN6ADDR_ANY" >> ${target}.h
        fi
else
        echo "#undef HAS_INET6_STRUCTS" >> ${target}.h
fi
echo  >> ${target}.h
echo "#endif" >> ${target}.h
exit 0
