#!/bin/sh 
# try guessing CPU type and flags suitable for compiler version
# so far usefull only for linux i386 and m68k

echo "QM_CONFIG_DONE=yes"

[ -f conf.log ] && rm -f conf.log
touch conf.log

if [ -z "$CC" ] ; then
    echo "CC not set, using gcc" 1>&2
    CC=gcc
fi
case $CC in
    *gcc*|*egcs*) ;;
    *) echo "CC=$CC not supported, trying gcc" 1>&2
       CC=gcc
esac

echo "CC = $CC" 1>&2
echo "CC = $CC"

$CC -v 2> .gcc_version

GCC_VER=` sed -n 's/^.*gcc version //p' .gcc_version`
#echo "GCC_VER = $GCC_VER"  1>&2
GCC_VER=`echo $GCC_VER| sed -n 's/^[a-zA-Z -]*\([0-9.]*\).*/\1/p' `
#echo "GCC_VER = $GCC_VER"  1>&2

#GCC_VER=`cat .gcc_version`

echo "gcc version is $GCC_VER" 1>&2

case $GCC_VER in
	*2.6*) echo "gcc version $GCC_VER - are you sure you don't want to update it?" 1>&2
	      OLDFLAGS="y" ;;
	*2.7*) OLDFLAGS="y" 
	       echo "GCC_XFLAGS+= -DNO_REGP"
	    ;;
	*2.8*) ;;
	*2.91.*) ## echo "GCC_XFLAGS+= -DUSE_AREGP" 
	    ;; # even 2.91.66 seems broken :(
	
	*2.95.1) ;;
	*2.95.2) ;;
	*3.0*) ;;
	*2.96*) ;; # echo "GCC_XFLAGS+= -DUSE_AREGP";;
	*2.9*) ;; #echo "you might want to try '-DUSE_AREGP', there is some chance it would work with this compiler version" 1>&2
	*) echo "gcc version $GCC_VER not known - using default settings" 1>&2 ;;
esac

#echo "OLDFLAGS is $OLDFLAGS" 1>&2

ARCH=`uname -m`
if [ -f /proc/cpuinfo ] ; then
    CPU=`cat /proc/cpuinfo| sed -n 's/cpu.*: //p'`
    [ -z "$CPU" ] && CPU=`cat /proc/cpuinfo| sed -n 's/CPU[[:blank:]]*:[^[:alnum:]]*//p'`
fi

#echo "CPU type from /proc/cpuinfo: $CPU" 1>&2

if [ -n "$DEF_CPU" ] ; then
    echo "using DEF_CPU=$DEF_CPU" 1>&2 ;
elif [ "$OLDFLAGS" = "y" ] ; then
    case $ARCH in
	    *486) DEF_CPU=-m486 ;;
	    *586) DEF_CPU=-m486 ;;  # gcc<2.8 doesn't know anything else..
	    *686) DEF_CPU=-m486 ;;
	    m68k) DEF_CPU=-m${CPU} ;;
	    *) echo "cannot determine CPU submodel automagically - this is not a problem, but you can try setting DEF_CPU for better performance" 1>&2 ;;
    esac
else
    case $ARCH in
	    *486) DEF_CPU="-march=i486" ;;
	    *586) DEF_CPU="-march=pentium" ;;
	    *686) DEF_CPU="-march=pentiumpro" ;;
	    m68k) DEF_CPU=-m${CPU} ;;
	    *) echo "cannot determine CPU submodel automagically - this is not a problem, but you can try setting DEF_CPU for better performance" 1>&2 ;;
    esac    
fi

if [ `uname` = Linux ] ; then
    case $ARCH in
	    *486) testvm=y ;;
	    *586) testvm=y ;;
            *686) testvm=y ;;
	  *m68k*) testvm=y ;;
	       *) GCC_XFLAGS=" -DEVM_SCR";;
    esac
fi


gcc -o vmtest vmtest.c 1>&2 2>>conf.log

if [ -n "$testvm" ] ; then
    if ./vmtest 2>>conf.log ; then
	GCC_XFLAGS=" -DUSE_VM"
    else
	echo "vmtest failed, falling back to EVM_SCR" 1>&2
	echo "for m68k kernels get newest patch from rz@linux-m68k.org" 1>&2
	GCC_XFLAGS=" -DEVM_SCR";
    fi
fi

if ./vmtest 2>>conf.log; then
   gcc -o zmtest zmtest.c 1>&2 2>>conf.log
   if ./zmtest 2>>conf.log; then
        GCC_XFLAGS="$GCC_XFLAGS -DZEROMAP"
   else
        echo "zero mapping apparently not supprted" 1>&2
   fi
fi

#rm .gcc_version

if [ -n "$DEF_CPU" ] ; then
    echo "DEF_CPU=$DEF_CPU"
    echo "adding cpu flag $DEF_CPU" 1>&2
fi

if [ -n "$GCC_XFLAGS" ] ; then
    echo "GCC_XFLAGS+=$GCC_XFLAGS"
    echo "adding $GCC_XFLAGS" 1>&2
fi
