#!/bin/sh

# This small script tries to guess which compiler should
# be used to build kernel modules.
#
# Written by Marc Boucher <marc@linuxant.com>

KERNELVER="$1"
KERNELSRC="$2"

if [ -n "${RPM_BUILD_ROOT}" ]; then
	if [ -f "${KERNELSRC}/kernelcompiler" ]; then
		kstr="`cat \"${KERNELSRC}/kernelcompiler\"`"
	else
		kstr="gcc version 3.2"
#
#		case "${KERNELVER}" in
#		2.4.19-16mdk | 2.4.19-19mdk | 2.4.18-14* | 2.4.1*.8.0* | 2.4.19-4GB)
#			kstr="gcc version 3.2"
#			;;
#		*)
#			kstr="gcc version 2.96"
#			;;
#		esac
	fi
else
	kstr="`cat /proc/version | sed -e 's/[^)]*) (//' -e 's/)[^)]*$//'`"
fi

case "$kstr" in
"gcc version "*)
	;;
*)
	echo 1>&2 "$0: cannot determine version, using gcc"
	echo gcc
	exit 0
	;;
esac

kgccver="`echo \"$kstr\" | awk '{print $3}'`"

#echo "Looking for $kgccver"

for c in gcc kgcc egcs gcc3 gcc296 gcc-2.96 gcc295 gcc-2.95; do
	if which $c >/dev/null 2>&1; then
		v="`eval $c -v 2>&1 | tail -1`"
		case "$v" in
		"gcc version "*)
			;;
		*)
			continue
			;;
		esac

		set -- $v
		if [ "$3" = "$kgccver" ]; then
			echo $c
			exit 0
		fi
	fi
done

echo 1>&2 "$0: using gcc; cannot find compiler matching:"
echo 1>&2 "$0: $kstr"

echo gcc
exit 0
