#! /bin/sh
# gcl-fe --- front end driver for multiple installations of GCL

# Copyright (C) 1996 Noah S. Friedman

# $Id: gcl-fe,v 1.2 1996/06/29 22:10:38 friedman Exp $

# 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, 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, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# Inspired by a similar, earlier script by Charles Sandel <sandel@cli.com>

# Front-end shell script for GNU GCL, used to
# manage multiple versions of GCL.

# Strategy: Install this script as "$prefix/bin/gcl".
# This is what users should normally execute to run "gcl".

# Give each version of GCL a separate hierarchy under
# "$prefix/lib", with the name "gcl-NN.NN" where NN.NN is the version number.
# This script looks at what versions are available, and selects a version,
# currently $DEFAULTVERSION.

# However, users can specify their own choice to force the selection of a
# particular version by setting the environment variable GCLVERSION to have
# a value which is the version number of the GCL that they want to use
# (just the numeric value), or to specify either the NEWEST or OLDEST
# versions.

# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

DEFAULTVERSION="${DEFAULTVERSION-NEWEST}"

if [ "$prefix" = "" ] ; then
  # root of the GNU installed tree
  prefix=/usr/local/gnu
fi

if [ ! -d "$prefix" ] ; then
  echo "Cannot find root of GNU tree ($prefix)."
  exit 1
fi

gclprefix="$prefix/lib/gcl-"

# Find out which versions are available on the system and sort them
# in numeric order.
#
# The largish sed script prefixes all version numbers with a sort key.
# That key is constructed by padding out any single or double digits to 3
# digits from the version number, then converting all occurences of `.' to
# `0', and prefixing and suffixing the entire result with an additional
# zero.  After sorting, the sort key is stripped from the output.
# We do all this because `sort' cannot numerically sort decimal numbers and
# will stop on the first `.'.
# This may not work correctly if the version number has more than 4 levels
# of minor versions (e.g. "1.2.3.4.5" may cause problems).


availversions=`ls -1d $gclprefix*/unixport/saved_gcl 2> /dev/null \
                | sed -n \
                      -e "s#^$gclprefix\([0-9.][0-9.]*\)/unixport.*#\1#" \
                      -e 'h
                          s/[^.]*[^0-9.][^.]*\.//g
                          :0
                          /[0-9.][0-9.]*\.[0-9.][0-9.]*\.[0-9.][0-9.]*\.[0-9.][0-9.]*/!{
                            s/$/.0/
                            b 0
                          }
                          s/^/./
                          s/$/./
                          :1
                          s/\.\([0-9]\)\./.00\1./g
                          s/\.\([0-9][0-9]\)\./.0\1./g
                          t 1
                          s/\./0/g
                          G
                          s/\n/ /' \
                       -e 'p' \
                 | sort -nu \
                 | sed -e 's/.* //'`

if [ "$availversions" = "" ] ; then
	echo "No version of gcl found in $prefix."
	exit 1
fi

# This sets `oldest' to the oldest version available, and `newest'
# to the newest version available.
# On line 1, we save the original pattern in the hold space and restore it
# in case it is the only line of input.
eval `echo "$availversions" \
       | sed -ne '1{h;s/^/oldest=/p;g;}
                  ${s/^/newest=/p;}
                 '`

version="${GCLVERSION-$DEFAULTVERSION}"

case "$version" in
  [Oo][Ll][Dd][Ee][Ss][Tt]) version="$oldest" ;;
  [Nn][Ee][Ww][Ee][Ss][Tt]) version="$newest" ;;
  '') version="$oldest" ;;
  *)
    if [ ! -d "$gclprefix$version" ] ;  then
      echo "$progname: $version: Cannot find requested version." 1>&2
      version=
    fi
   ;;
esac

# If we don't have a version by now, then give up.
if [ "$version" = "" ] ; then
  exec 1>&2
  echo "$progname: Cannot determine which version to use."
  case "$availversions" in
    */* )
      echo "Available versions are:"
      for f in $availversions; do
        echo "   $f"
      done | sort
     ;;
    * )
      echo "Available versions are:" $availversions
     ;;
  esac
  exit 1
fi

libdir=$gclprefix$version
exec "$libdir/unixport/saved_gcl" \
   -dir "$libdir/unixport/" \
   -libdir "$libdir/" \
   -eval '(setq si::*allow-gzipped-file* t)' \
   -eval '(setq si::*tk-library* "/usr/lib/tk")' \
   ${1+"$@"}
# other options: -load /tmp/foo.o -load jo.lsp -eval "(joe 3)"

# gcl-fe ends here
