#!/bin/sh
#
# $Id$
# Copyright (C) 2000-2003  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2003       Stanislav Ievlev <inger@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##*/}"

: ${TARGET:=i586-alt-linux}
: ${BINDIR:=/usr/bin}
: ${COMPILERS:='cpp gcc g++ g77 gcj chill'}
: ${ALT_LIST:='/usr/sbin/alternatives-list -s'}
: ${ALT_SETUP:='/usr/sbin/alternatives-setup'}
: ${ALT_UPDATE:='/usr/sbin/alternatives-update'}

Usage()
{
	[ "$1" = 0 ] || exec >&2
	local args='display|auto'
	local n
	for n in `$ALT_LIST -C "$BINDIR/$TARGET-gcc" |sed -ne "s,[^:]\+: $BINDIR/$TARGET-gcc-\\(.*\)$,\\1,pg" |sort -u`; do
		args="$args|$n"
	done
	echo "Usage: $PROG {$args}"
	exit $1
}

case "$1" in
	help|usage|--help|--usage)
		Usage 0
		;;
	display)
		for n in $COMPILERS; do
			if [ -f "$BINDIR/$TARGET-$n" ]; then
				$ALT_LIST "$BINDIR/$TARGET-$n"
			fi
		done
		;;
	auto)
		for n in $COMPILERS; do
			if [ -f "$BINDIR/$TARGET-$n" ]; then
				$ALT_SETUP -a "$BINDIR/$TARGET-$n" >/dev/null
			fi
		done
		$ALT_UPDATE
		;;
	*)
		[ -f "$BINDIR/$TARGET-gcc-$1" ] || Usage 1

		for n in $COMPILERS; do
			if [ -f "$BINDIR/$TARGET-$n" -a -f "$BINDIR/$TARGET-$n-$1" ]; then
				$ALT_SETUP -m "$BINDIR/$TARGET-$n-$1" "$BINDIR/$TARGET-$n" >/dev/null
			fi
		done
		$ALT_UPDATE
		;;
esac
