#!/bin/sh 

#      /*
#       *  pb_gcc
#       *
#       *  Copyright (c) 2007 by Kuscsik Peter <kuscsikp@gmail.com>
#       *
#       *  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.
#       */ 

if [ $# = "0" ]			#no parameters
then
	echo "pb_gcc 0.0.1a, Peter Kuscsik, 2007"
	echo "Experimental release, use at your own risk!"
	echo "Source: http://gentoo-wiki.com/Safe_Cflags"
	echo
	echo "Use:	\"pb_gcc --opt-flags\" to display optimisation flags"
	echo "	\"pb_gcc [options] file\" to compile with optimisation flags"
fi


if [ -f "procbench" ]
then
  	v=$(./procbench -si | cut -d "," -f 1)
  	f=$(./procbench -si | cut -d "," -f 2)
  	m=$(./procbench -si | cut -d "," -f 3)
  	s=$(./procbench -si | cut -d "," -f 4)
else
   	if [ -f "/bin/procbench" ]
	then
		v=$(procbench -si | cut -d "," -f 1)
		f=$(procbench -si | cut -d "," -f 2)
		m=$(procbench -si | cut -d "," -f 3)
		s=$(procbench -si | cut -d "," -f 4)
	else
		echo "Procbench not found!"
		exit 1
	fi
fi

if [ "$v" = "AuthenticAMD" ]
then
   	if [ -f "amd.gcc.txt" ]
	then
	    	g_file="amd.gcc.txt"
	else
	    	g_file="/usr/share/procbench/amd.gcc.txt"
	fi
fi

if [ "$v" = "GenuineIntel" ]
then
   	if [ -f "intel.gcc.txt" ]
	then
	    	g_file="intel.gcc.txt"
	else
	    	g_file="/usr/share/procbench/intel.gcc.txt"
	fi

fi

line=$(cat $g_file | grep $f,$m,$s | head -n 1)

if [ -z "$line" ] 
then
	line=$(cat $g_file | grep "$f,$m,*" | head -n 1)
fi

if [ -z "$line" ]
then
	line=$(cat $g_file | grep "$f,*,*" | head -n 1)
fi

flag=$(echo $line | cut -d "," -f 4)

if [ "$1" = "--opt-flags" ]
	then
	echo $flag
	exit 0
fi

if [ $# != "0" ]			#if no parameters
then
	gcc $flag $@
fi






