#!/bin/sh

set -e
PATH=/bin:/usr/bin:$PATH; export PATH
trap "rm -f tmp$$[abcde].c tmp$$[abcde].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 > tmp$$d.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
struct sockaddr_storage xx;
EOF

cat > tmp$$e.c <<EOF
#include <sys/types.h>
#include <sys/socket.h>
struct sockaddr_storage xx;
main() {
	printf("%u\n", sizeof(xx.ss_family));
}
EOF

cat > ${target}.h <<EOF

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

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

EOF

if ${CC} -c tmp$$a.c > /dev/null 2>&1
then
        echo "#define HAS_INET6_STRUCTS" >> ${target}.h
        if ${CC} -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 ${CC} $def -c tmp$$c.c > /dev/null 2>&1
        then
		:
	else
		echo "#define NEED_IN6ADDR_ANY" >> ${target}.h
        fi
	if ${CC} -c tmp$$d.c > /dev/null 2>&1
	then
		if ${CC} -c tmp$$e.c > /dev/null 2>&1
		then
			:
		else
			echo "#define ss_family __ss_family" >> ${target}.h
		fi
	else
		echo "#define NEED_SOCKADDR_STORAGE" >> ${target}.h
	fi
else
        echo "#undef HAS_INET6_STRUCTS" >> ${target}.h
fi
echo  >> ${target}.h
echo "#endif" >> ${target}.h
exit 0
