#!/bin/sh
#
# Sebastien Koechlin - <seb.debian@koocotte.org>
#
# This script set (return) a class depending of the CPU read via /proc/cpuinfo
# Supported values are:
#	SMP			Does not work if installation kernel is not SMP aware
#	HYPER_THREADING
#	CPU_I686, CPU_I586, CPU_I486, CPU_I386
#	CPU_K8, CPU_K7, CPU_K6
# TODO: Add another level of switch for non-intel CPU
#
# 2005-11-01: Initial version
# 2005-12-05: Add comments
#


# Get a value in /proc/cpuinfo
function getline() {
	RES=`echo $(egrep "$1" /proc/cpuinfo | head -1 | cut -d':' -f2-)`
}


# Failed to determine CPU
function failed() {
	echo "Unable to identify CPU" >2
	cat /proc/cpuinfo >2
	echo "CPU_I386";
	exit 1
}


# Try to detect SMP motherboard
# Will not work, install kernel is not SMP
getline '^processor.*[1-9]$'
if [ "$RES" ]; then
	echo "SMP";
fi;


# Try to detect HyperThreading CPU
getline '^flags.*\bht\b'
if [ "$RES" ]; then
	echo "HYPER_THREADING";
fi;


# Try to determine CPU type
getline '^vendor_id'
vendor=$RES
getline '^cpu family'
family=$RES
getline '^model'
model=$RES
getline '^model name'
name=$RES
getline '^stepping'
stepping=$RES
getline '^cpu MHz'
freq=$RES

# The big matrice
case $vendor in

	# Intel
	GenuineIntel)
	
		case $family in
	
			15)	# Intel(R) P4 generation
				echo "CPU_I686";;
			
			6)	# Intel(R) Pentium II/III generation
				echo "CPU_I686";;
			
			5)	# Pentium / Pentium MMX
				echo "CPU_I586";;
				
			4)	# 486
				echo "CPU_I486";;
				
			*)	# Unknown
				failed;;
		esac
	
	
	;;
	
	# AMD
	AuthenticAMD)
		
		case $family in
		
			15)	# AMD Opteron(tm) / Athlon(tm) 64
				echo "CPU_K8";;

			6)	# AMD Athlon(TM) / mobile AMD Duron
				echo "CPU_K7";;
			
			5)	# AMD-K6tm
				echo "CPU_K6";;
			
			*)	# Unknown
				failed;;
		esac
	;;
	
	# Old ones
	unknown)
		
		if [ "$family" = "4" && "$name" = "486" ]; then
			#return "CPU_AMD486";
			return "CPU_I486";
		else
			echo "Unknow cpu" >2
			cat /proc/cpuinfo >2
			exit 1
		fi;
	
	;;
	
	# Other
	CentaurHauls|CyrixInstead|NexGenDriven|RiseRiseRise|GenuineTMx86|TransmetaCPU|'UMC UMC UMC')
	
		# Unknown
		failed;
	;;
	
	# Unknown
	*)
		# Unknown
		failed;
	
	;;

esac
