#!/bin/sh
# yemacs --- front end to find right development version of compiled emacs

# Copyright (C) 1994, 1995, 1996 Noah S. Friedman

# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1994-10-25

# $Id: yemacs,v 1.9 1996/03/03 23:05:47 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:
# Code:

prefix=/var/project/emacs/build
config=${MACHTYPE-`hosttype`}
toolkit=lucid

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

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

usage="Usage: $progname {options} {{--} args to emacs}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-c, --configuration CONFIG   Use an emacs configured for system of type CONFIG.
                             This is a string of the form {cpu}-{vendor}-{os}.
-h, --help                   You're looking at it.
-l, --list-available         List all available configurations and toolkits.
-P, --print-file-name        Print the complete file name of the emacs
                             executable to stdout and exit.
-p, --prefix        PREFIX   Build-tree prefix; this is the base where
                             all uninstalled build trees reside.
-r, --revision      REV      Run revision REV; either a numeric version,
                             the keyword ${bq}oldest$eq, or ${bq}newest$eq.
-q, --quick                  Tell emacs not to load any init files.
-t, --toolkit       TOOLKIT  Use an emacs compiled with the TOOLKIT window
                             toolkit.

--                           End argument list to $progname and begin
                             arguments to genuine emacs program.
                             This is only necessary if passing arguments to
                             emacs that begin with $bq-$eq.

Default prefix is $bq$prefix$eq
Default configuration is $bq$config$eq
Default toolkit is $bq$toolkit$eq
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
listp=
quick=
printp=
rev=

# Usage: eval "$getopt"; value=$optarg
# or     optarg_optional=t; eval "$getopt"; value=$optarg
#
# This function automatically shifts the positional args as appropriate.
# The argument to an option is optional if the variable `optarg_optional'
# is non-empty.  Otherwise, the argument is required and getopt will cause
# the program to exit on an error.  optarg_optional is reset to be empty
# after every call to getopt.  The argument (if any) is stored in the
# variable `optarg'.
#
# Long option syntax is `--foo=bar' or `--foo bar'.  2nd argument
# won't get used if first long option syntax was used.
#
# Note: because of broken bourne shells, using --foo=bar syntax can
# actually screw the quoting of args that end with trailing newlines.
# Specifically, most shells strip trailing newlines from substituted
# output, regardless of quoting.
getopt='
  {
    optarg=
    case "$1" in
      --*=* )
        optarg=`echo "$1" | sed -e "1s/^[^=]*=//"`
        shift
       ;;
      * )
        case ${2+set} in
          set )
            optarg="$2"
            shift
            shift
           ;;
          * )
            case "$optarg_optional" in
              "" )
                case "$1" in
                  --*=* ) option=`echo "$1" | sed -e "1s/=.*//;q"` ;;
                  * ) option="$1" ;;
                esac
                exec 1>&2
                echo "$progname: option $bq$option$eq requires argument."
                echo "$progname: use $bq--help$eq to list option syntax."
                exit 1
               ;;
           esac
         ;;
        esac
     ;;
    esac
    optarg_optional=
  }'

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug )
      debug=t
      shift
     ;;
    -c | --configuration* | --c* )
      eval "$getopt"
      config=$optarg
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -l | --list-available | --l* )
      listp=t
      shift
     ;;
    -P | --print-file-name | --pri* )
      printp=t
      shift
     ;;
    -p | --prefix* | --pre* )
      eval "$getopt"
      prefix=$optarg
     ;;
    -q | --quick | --q* )
      # Pass this option to emacs; this is just for the sake of convenience.
      quick=-q
      shift
     ;;
    -r | --revision* | --r* )
      eval "$getopt"
      rev="$optarg"
     ;;
    -t | --toolkit* | --t* )
      eval "$getopt"
      toolkit=$optarg
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

case "$listp" in
  '' )
    emacs="$prefix/$config/$toolkit/src/emacs"

    case "$rev" in
      '' ) : ;;
      [Oo][Ll][Dd][Ee][Ss][Tt] )
        emacs=`ls -1 ${emacs}-[0-9]* | sed -ne '1p'`
       ;;
      [Nn][Ee][Ww][Ee][Ss][Tt] )
        emacs=`ls -1 ${emacs}-[0-9]* | sed -ne '$p'`
       ;;
      * ) emacs="$emacs$rev" ;;
    esac

    case "$printp" in
      t )
        echo $emacs
        exit 0
       ;;
      * ) exec $emacs $quick ${1+"$@"} ;;
    esac
   ;;
  * )
    cd $prefix || exit 1
    echo "Available configurations/toolkits:"
    for config in `find * -type d -prune -print`; do
      for toolkit in `cd $config && find * -type d -prune -print`; do
        if test -f $config/$toolkit/src/emacs ; then
          echo "  --configuration=$config --toolkit=$toolkit"
        fi
        for rev in `{ cd $config/$toolkit/src \
                       && ls -1d emacs-[0-9]* 2> /dev/null; } \
                    | sed 's/emacs-//'`
        do
          echo "  --configuration=$config --toolkit=$toolkit --revision=$rev"
        done
      done
    done
   ;;
esac

# yemacs ends here
