#!/bin/sh
#set -e

. $KUMATUXBIN/kumatux.org-vars

KUMATUX_CPPFLAGS_ESCAPED=`$KUMATUXBIN/kumatux.org-sed-escape "$KUMATUX_CPPFLAGS"`
KUMATUX_CFLAGS_ESCAPED=`$KUMATUXBIN/kumatux.org-sed-escape "$KUMATUX_CFLAGS"`
KUMATUX_CXXFLAGS_ESCAPED=`$KUMATUXBIN/kumatux.org-sed-escape "$KUMATUX_CXXFLAGS"`

if [ "$KUMATUX_CPPFLAGS_ESCAPED" ]
then
    SYSTEM_CPPFLAGS=`echo $CPPFLAGS | sed "s/$KUMATUX_CPPFLAGS_ESCAPED //"`
    SYSTEM_CPPFLAGS=`echo $CPPFLAGS | sed "s/$KUMATUX_CPPFLAGS_ESCAPED//"`
else
    SYSTEM_CPPFLAGS=$CPPFLAGS
fi

if [ "$KUMATUX_CFLAGS_ESCAPED" ]
then
    SYSTEM_CFLAGS=`echo "$CFLAGS" | sed "s/$KUMATUX_CFLAGS_ESCAPED //"`
    SYSTEM_CFLAGS=`echo "$CFLAGS" | sed "s/$KUMATUX_CFLAGS_ESCAPED//"`
else
    SYSTEM_CFLAGS=$CFLAGS
fi

if [ "$KUMATUX_CXXFLAGS_ESCAPED" ]
then
    SYSTEM_CXXFLAGS=`echo "$CXXFLAGS" | sed "s/$KUMATUX_CXXFLAGS_ESCAPED //"`
    SYSTEM_CXXFLAGS=`echo "$CXXFLAGS" | sed "s/$KUMATUX_CXXFLAGS_ESCAPED//"`
else
    SYSTEM_CXXFLAGS=$CXXFLAGS
fi

FLAGS=""
for FILE in $KUMATUXETC/$KUMATUXETCINCLUDE/*.conf
do
    FLAGSNEW="-I`cat $FILE 2>/dev/null` $FLAGS"
    FLAGS=`echo $FLAGSNEW | sed 's/ $//'`
done

if [ "$FLAGS" = "-I" ]
then
    FLAGS=""
fi
export KUMATUX_CPPFLAGS="$FLAGS"
if [ "$KUMATUX_CPPFLAGS" ]
then
    ## MacBook Pro
#    export KUMATUX_CFLAGS="-O6 -march=core2 -fomit-frame-pointer -pipe -msse4.2 -msse4.1 $KUMATUX_CPPFLAGS"
    ## HTC Universal
#    export KUMATUX_CFLAGS="-O6 -fomit-frame-pointer -pipe $KUMATUX_CPPFLAGS"
    ## Nokia N900
    export KUMATUX_CFLAGS="-O6 -fomit-frame-pointer -pipe $KUMATUX_CPPFLAGS"
else
    export KUMATUX_CFLAGS="-O6 -fomit-frame-pointer -pipe"
fi
export KUMATUX_CXXFLAGS="$KUMATUX_CFLAGS"

if [ "$SYSTEM_CPPFLAGS" ]
then
    export CPPFLAGS="$KUMATUX_CPPFLAGS $SYSTEM_CPPFLAGS"
else
    export CPPFLAGS="$KUMATUX_CPPFLAGS"
fi
if [ "$SYSTEM_CFLAGS" ]
then
    export CFLAGS="$KUMATUX_CFLAGS $SYSTEM_CFLAGS"
else
    export CFLAGS="$KUMATUX_CFLAGS"
fi
if [ "$SYSTEM_CXXFLAGS" ]
then
    export CXXFLAGS="$KUMATUX_CXXFLAGS $SYSTEM_CXXFLAGS"
else
    export CXXFLAGS="$KUMATUX_CXXFLAGS"
fi
