#!/bin/sh

OS=`uname`
REL=`uname -r`

if [ "$OS" = "Linux" ]; then
	ARCH=`uname -m`
else	
	ARCH=`uname -p`
fi

echo "#ifndef MACHDEP_H" > machdep.h
echo "#define MACHDEP_H" >> machdep.h
echo >> machdep.h
echo "#define PLATFORM \"$OS $REL\"" >> machdep.h
echo "#define BUILDDATE \"`date`\"" >> machdep.h

case "$OS" in
Darwin)
	echo "configuring for MacOS X ($OS $REL) on $ARCH"

	echo "#define MACOSX" >> machdep.h
        if [ -f /usr/include/sys/event.h ]; then
                echo "Using kqueue() event notification interface"
                echo "#define HAVE_KQUEUE" >> machdep.h
		echo "#define HAVE_POLL" >> machdep.h
	else
		echo "#define HAVE_SELECT" >> machdep.h
	fi
	echo "#define HAVE_UIO" >> machdep.h
	echo "#define HAVE_PTHREADS" >> machdep.h
	echo "#define HAVE_STRERROR_R" >> machdep.h
	echo "#define HAVE_SOCKLEN_T" >> machdep.h
	echo "#define HAVE_GETADDRINFO" >> machdep.h
	echo "#define HAVE_PREAD" >> machdep.h
	;;
SunOS)
	echo "configuring for Solaris ($OS $REL) on $ARCH"
	if [ -f /usr/include/sys/devpoll.h ]; then
		echo "Using /dev/poll event notification interface"
		echo "#define HAVE_DEVPOLL" >> machdep.h
	else
		echo "#define HAVE_POLL" >> machdep.h
	fi
	echo "#define HAVE_UIO" >> machdep.h
	echo "#define HAVE_PTHREADS" >> machdep.h
	echo "#define HAVE_SOCKLEN_T" >> machdep.h
	echo "#define HAVE_GETADDRINFO" >> machdep.h
	echo "#define HAVE_PREAD" >> machdep.h
	;;
Irix)
	echo "configuring for $OS $REL on $ARCH"
        if [ -f /usr/include/sys/devpoll.h ]; then
                echo "Using /dev/poll event notification interface"
                echo "#define HAVE_DEVPOLL" >> machdep.h
        else
                echo "#define HAVE_POLL" >> machdep.h
        fi
        echo "#define HAVE_UIO" >> machdep.h
	echo "#define HAVE_PTHREADS" >> machdep.h
	echo "#define HAVE_SOCKLEN_T" >> machdep.h
	echo "#define HAVE_STRERROR_R" >> machdep.h
	echo "#define HAVE_GETADDRINFO" >> machdep.h
        ;;
Linux)
	echo "configuring for $OS $REL on $ARCH"
	echo "*** Edit Dispatcher.c to enable epoll support"
	echo "#define HAVE_POLL" >> machdep.h
	echo "#define HAVE_UIO" >> machdep.h
	echo "#define HAVE_FNOTIFY" >> machdep.h
	echo "#define HAVE_PTHREADS" >> machdep.h
	echo "#define HAVE_STRERROR_R" >> machdep.h
	echo "#define HAVE_SOCKLEN_T" >> machdep.h
	echo "#define HAVE_GETADDRINFO" >> machdep.h
	echo "#define HAVE_PREAD" >> machdep.h
	;;
FreeBSD|OpenBSD|NetBSD)
	echo "configuring for $OS $REL on $ARCH"
	if [ -f /usr/include/sys/event.h ]; then
		echo "Using kqueue() event notification interface" 
		echo "#define HAVE_KQUEUE" >> machdep.h
	else
		echo "#define HAVE_POLL" >> machdep.h
	fi
	echo "#define HAVE_UIO" >> machdep.h
	echo "#define HAVE_PTHREADS" >> machdep.h
	echo "#define HAVE_STRERROR_R" >> machdep.h
	echo "#define HAVE_SOCKLEN_T" >> machdep.h
	echo "#define HAVE_GETADDRINFO" >> machdep.h
	echo "#define HAVE_PREAD" >> machdep.h
	;;
*)
	echo "unsupported OS: $OS $REL"
	echo "*** You may need to hand edit machdep.h"
	echo "#define HAVE_SELECT" >> machdep.h
	;;
esac

case "$ARCH" in
powerpc|sparc|SUN*|mips|alpha)
	echo "Configuring for a big endian processor architecture: $ARCH"
        echo "#define HOST_BIGENDIAN 1" >> machdep.h
        ;;
*)
	echo "Configuring for a little endian processor architecture: $ARCH"
        echo "/* #undef HOST_BIGENDIAN */" >> machdep.h
        ;;
esac
				

echo >> machdep.h

# POSIX specific includes
echo "#include <sys/types.h>" >> machdep.h
echo "#include <sys/time.h>" >> machdep.h
echo "#include <sys/socket.h>" >> machdep.h
echo "#include <netinet/in.h>" >> machdep.h
echo "#include <arpa/inet.h>" >> machdep.h
echo "#include <inttypes.h>" >> machdep.h
echo "#include <netdb.h>" >> machdep.h
echo "#include <unistd.h>" >> machdep.h
echo "#include <fcntl.h>" >> machdep.h
echo >> machdep.h
echo "#ifndef HAVE_SOCKLEN_T" >> machdep.h
echo "typedef int socklen_t;" >> machdep.h
echo "#endif /* HAVE_SOCKLEN_T */" >> machdep.h
echo >> machdep.h
echo "#endif" >> machdep.h

exit 0
