#!/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 880 2009-03-14 22:13:46Z deurk $
#

# 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`
fi

ARCH=`uname -m | sed -e 's/i.86/x86/g' -e 's/Power Macintosh/ppc/g' -e 's/amd64/x86_64/g'`
echo UNAME="${UNAME}" >> Makefile
echo ARCH="${ARCH}" >> Makefile

if [ x$2 = x64 ]; then
BITS=64
else
BITS=32
echo FORCE32BITFLAGS=-m32 >> Makefile
fi

SVNREV=`svnversion -n . 2>/dev/null | sed -e "s/M//"`
echo SVNREV="${SVNREV}" >> Makefile

case ${UNAME} in
FreeBSD | OpenBSD | NetBSD | DragonFly | BSD)
	echo "${UNAME} ${ARCH} ${BITS}bits system - using BSD Makefile."
	cat Makefile.BSD >> Makefile
	echo "Configuration completed."
	;;
Darwin | MacOSX | Linux | SunOS | GNU)
	echo "${UNAME} ${ARCH} ${BITS}bits system - using GNU Makefile."
	cat Makefile.GNU >> Makefile
	echo "Configuration completed."
	;;
Usage)
	echo "Usage:"
	echo "./configure SYSTEM BITS"
	echo "SYSTEM: used `uname`"
	echo "Possible values are: FreeBSD, OpenBSD, NetBSD, DragonFly, Darwin, MacOSX, Linux, SunOS, BSD, GNU"
	echo "Other systems not supported yet."
	echo "BITS: 64"
	echo "Defaults to BITS=32 because QVM is not yet supported by MVDSV compiled at 64 bits."
	echo "If you want to turn off force compile 32 bits on 64 bits system use BITS parameter."
	echo ""
        ;;
*)
	echo ""
	echo "Unknown system: ${UNAME}."
	echo ""
	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 "./configure Usage			for see usage information"
	echo ""
	echo "Other systems not supported yet."
	echo ""
	;;
esac
