#!/bin/sh
#
# $Id$
# Copyright (C) 2000-2002  Dmitry V. Levin <ldv@altlinux.org>
#
# GNU Compiler Collection (GCC) version selection utility.
#
# 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.
#

PROG="${0##*/}"

: ${ALTDIR:=/etc/alternatives}
: ${ADMINDIR:=/var/lib/rpm/alternatives}
: ${TARGET:=i586-alt-linux}
: ${VERBOSE:=--verbose}

Usage()
{
	local args='display|auto'
	local n
	for n in `/bin/egrep -sB1 '^[0-9]+$' "$ADMINDIR/$TARGET-gcc" |/bin/sed -ne "s,^/usr/bin/$TARGET-gcc-\\(.*\\)\$,\\1,pg"`; do
		args="$args|$n"
	done
	echo "Usage: $PROG {$args}"
	exit $1
}

case "$1" in
	help|usage|--help|--usage)
		Usage 0
		;;
	display|auto)
		for n in cpp gcc g++ g77 gcj chill; do
			if [ -f "$ADMINDIR/$n" ]; then
				/usr/sbin/update-alternatives $VERBOSE --$1 "$n"
				echo
			fi
			n="$TARGET-$n"
			if [ -f "$ADMINDIR/$n" ]; then
				/usr/sbin/update-alternatives $VERBOSE --$1 "$n"
				echo
			fi
		done
		;;
	*)
		[ -f "/usr/bin/$TARGET-gcc-$1" ] || Usage 1

		for n in cpp gcc g++ g77 gcj chill; do
			n="$TARGET-$n"
			if [ -f "$ADMINDIR/$n" ]; then
				/usr/sbin/update-alternatives $VERBOSE --config "$n" "/usr/bin/$n-$1"
				echo
			fi
		done
		;;
esac
