#!/bin/bash
#
#set -x

MAKE_FLAGS="mmx mmx2 mmxext sse sse2 3dnow 3dnowext"
CPU_FLAGS=$(grep "^flags" /proc/cpuinfo)

MAKECONF="/etc/make.conf"

#USE_FLAGS=$(grep "^USE=" $MAKECONF | sed -e "s/USE=\"/ /" -e "s/\"/ /g" -e "s/ mmx / /g" -e "s/ sse / /g" -e "s/ sse2 / /g" -e "s/ mmx2 / /g" -e "s/ mmxext / /g" -e "s/ 3dnow / /g" -e "s/ 3dnowext / /g")
USE_FLAGS=$(grep "^USE=" $MAKECONF | cut -f 2 -d '"' )
echo "<$USE_FLAGS>"
echo "<$CPU_FLAGS>"

for i in $MAKE_FLAGS ; do
   USE_FLAGS=$(echo $USE_FLAGS | sed -e "s/ $i / /g")
   TEST=$(echo " $CPU_FLAGS " | grep -i " $i ")
   if [ "$TEST" != "" ] ; then
      echo "$i detected"
      USE_FLAGS="$USE_FLAGS $i"
   else
      echo "$i not detected"
   fi
done
echo "<$USE_FLAGS>"

sed -i $MAKECONF -e "s/^USE=.*/USE=\"$USE_FLAGS\"/"


