#    switch.sh - shell script for switching between compilation environments
#    Copyright (c) 1997  Frodo Looijaard <frodol@dds.nl>
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.

# Created November 13, 1997 (version 1.0)
# Small changes November 14, 1997 (version 1.1)
#   Copyright notice added

# This program can only be used if bash is your shell!
#
# Call switch.sh once, and after that you can use the switch instruction
# Syntax:
#   switch <environment>
#
# gcc must be installed so that /usr/lib/gcc-lib contains the gcc library
# files (this is the default).
# It only looks for targets under /usr
# It needs sed(1)

function switch()
{
  if [ $# != 1 ] ; then
    echo 'Switch version 1.1'
    echo 'Syntax: switch <environment>'
    return 1
  elif [ ! -d "/usr/lib/gcc-lib/$1" ] ; then
    echo "Not a valid environment ($1)"
    return 1
  fi

  local curpath="$PATH"

  pushd . >/dev/null
  cd /usr/lib/gcc-lib

  for file in *; do
    if [ "$file" != dummy ] && [ -d "$file" ] ; then
      curpath=`echo "$curpath" | sed -e "s,/usr/$file/bin,,"`
    fi
  done

  local oldpath=""
  while [ "$curpath" != "$oldpath" ] ; do
    oldpath="$curpath"
    curpath=`echo "$curpath" | sed -e 's,::,:,g'`
  done

  curpath=`echo "$curpath" | sed -e 's,^:,,g' -e 's,:$,,g'`
  export PATH="/usr/$1/bin:$curpath"
  popd >/dev/null
}

