#!/bin/sh

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

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

if [ -n "${RPM_BUILD_ROOT}" ]; then
	if [ -f "${KERNELSRC}/kernelcompiler" ]; then
		kstr="`cat \"${KERNELSRC}/kernelcompiler\"`"
	else
		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: unable to extract gcc version from /proc/version"
	exit 1
	;;
esac

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

#echo "Looking for $kgccver"

for c in gcc kgcc egcs gcc3 gcc296 gcc-2.96; 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: unable to find compiler matching /proc/version:"
echo 1>&2 "$0: $kstr"
echo 1>&2 "$0: set CC variable manually in modules/common.mak"

exit 2
