#!/bin/sh
#
#	Copyright (C) 2004-2006 VVD (vvd0@users.sourceforge.net).
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#	$Id: configure 754 2008-02-06 20:09:12Z renzo $
#

# C Compiler
CC=gcc
echo CC="${CC}" > Makefile

FILE=conftest


# Byte order determination
echo "Checking byte order..."
cat > "${FILE}.c"  << _EOF
#include <stdio.h>
/*
__LITTLE_ENDIAN__	1234
__BIG_ENDIAN__		4321
__PDP_ENDIAN__		3412
*/
int main()
{
	unsigned a = 0x11223344;
    if (((unsigned char*)&a)[0] == 0x44 &&
		((unsigned char*)&a)[1] == 0x33 &&
		((unsigned char*)&a)[2] == 0x22 &&
		((unsigned char*)&a)[3] == 0x11)
		printf("__LITTLE_ENDIAN__\n");
	else if (
		((unsigned char*)&a)[0] == 0x11 &&
		((unsigned char*)&a)[1] == 0x22 &&
		((unsigned char*)&a)[2] == 0x33 &&
		((unsigned char*)&a)[3] == 0x44)
		printf("__BIG_ENDIAN__\n");
	else if (
		((unsigned char*)&a)[0] == 0x22 &&
		((unsigned char*)&a)[1] == 0x11 &&
		((unsigned char*)&a)[2] == 0x44 &&
		((unsigned char*)&a)[3] == 0x33)
		printf("__PDP_ENDIAN__\n");
	else
		printf("UNKNOWN\n");

	return 0;
}
_EOF

if "${CC}" -o "${FILE}" "${FILE}.c"; then
BYTE_ORDER=`"./${FILE}"`
rm -f "${FILE}" "${FILE}.c"
if test "x${BYTE_ORDER}" != xUNKNOWN; then
	echo "Byte order is: ${BYTE_ORDER}"
	echo "BYTE_ORDER=${BYTE_ORDER}" >> Makefile
else
	rm Makefile
	echo "can't determine"
	echo "Error: can't continue"
	return 1
fi
else
	rm Makefile
	echo "can't determine"
	echo "Error: can't continue"
	return 1
fi


# OS determination
if [ x$1 != x ]; then
UNAME=$1
else
UNAME=`uname`
BRANCH=`uname -m`
fi
echo UNAME="${UNAME}" >> Makefile
echo BRANCH="${BRANCH}" >> Makefile

case ${UNAME} in
FreeBSD | OpenBSD | NetBSD | DragonFly | BSD)
	echo "${UNAME} system - use BSD Makefile."
	cat Makefile.BSD >> Makefile
	echo "Configuration completed."
	;;
Darwin | MacOSX | Linux | SunOS | GNU)
	if [ ${BRANCH} = x86_64 ]; then
	echo "${UNAME} ${BRANCH} system - use GNU32 Makefile."
	cat Makefile.GNU32 >> Makefile
	echo "Configuration completed."
	else
	echo "${UNAME} system - use GNU Makefile."
	cat Makefile.GNU >> Makefile
	echo "Configuration completed."
	fi
	;;
*)
	echo ""
	echo "Unknown system: ${UNAME}."
	echo "You must specify the system which you want to compile for:"
	echo ""
	echo "./configure FreeBSD               FreeBSD"
	echo "./configure OpenBSD               OpenBSD"
	echo "./configure NetBSD                NetBSD"
	echo "./configure DragonFly             DragonFly BSD"
	echo "./configure Darwin                Darwin/MacOS X"
	echo "./configure MacOSX                Darwin/MacOS X"
	echo "./configure Linux                 Linux"
	echo "./configure SunOS                 SunOS/Solaris"
	echo "or"
	echo "./configure BSD                   for build with BSD Makefile"
	echo "./configure GNU                   for build with GNU Makefile"
	echo "./configure                       for auto configure"
	echo ""
	echo "Other systems not supported yet."
	echo ""
	;;
esac
