#!/bin/sh
# econfig --- front end to configure an emacs build tree for compilation

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

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

# $Id: econfig,v 1.10 1997/07/09 14:11:25 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:

# If using this at splode.com, read the file
# /usr/local/src/gnu/emacs/fsf-dev/README

# Code:

rootdir=/usr/local/src/gnu/emacs/fsf-dev
srcdir=$rootdir/current
prefix=/var/project/emacs

# 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} {{--} other args to canonical configure}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-c, --configuration CONFIG   Use CONFIG as the configuration type.
                             This is a string of the form {cpu}-{vendor}-{os}.
                             Read the file ${bq}INSTALL$eq in the emacs
                             distribution for more details.
-g, --no-guess               Do not guess configuration type based on
                             current directory; instead, let the emacs
                             configure program do it itself with the
                             ${bq}config.guess$eq program supplied in the
                             distribution.
-h, --help                   You're looking at it.
-P, --prefix        PREFIX   Use PREFIX for installation prefix path.
                             Note: you probably don't need to set this
                             since the devopment versions of emacs should
                             not be installed.  If not specified, the
                             prefix is set to $bq$prefix$eq.
-p, --program       PROG     Run PROG to configure emacs.
-q, --quiet                  Turn off verbose messages from configure.
-s, --srcdir        SRCDIR   Current sources are in SRCDIR.
-t, --toolkit       TOOLKIT  Use an emacs compiled with the TOOLKIT window
                             toolkit.
--                           End argument list to $progname and begin
                             arguments to the configuration program.
                             This is only necessary if passing arguments to
                             configure that begin with $bq-$eq.

The default configure program to run is
$bq$srcdir/configure$eq

The default configuration and toolkit are deduced from the current
directory of the build tree.  For example, if you are in
$rootdir/build/sparc-sun-sunos4.1.3/lucid, the
configuration type is assumed to be ${bq}sparc-sun-sunos4.1.3$eq, and the
default toolkit to be ${bq}lucid$eq."

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
configuration=
guessp=t
toolkit=
quiet=

# 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"
      configuration=$optarg
     ;;
    -g | --no-guess | -n* )
      guessp=
      shift
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -P | --prefix* | --pre* )
      eval "$getopt"
      prefix="$optarg"
     ;;
    -p | --program* | --pro* )
      eval "$getopt"
      configure=$optarg
     ;;
    -q | --quiet | --q* )
      quiet=--quiet
      shift
     ;;
    -s | --silent | --s* )
      eval "$getopt"
      srcdir=$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 "$quiet" in '--quiet' ) exec 1> /dev/null ;; esac

case "$guessp" in
  t )
    if test ".$configuration" = '.' \
        || test ".$toolkit" = '.'
    then
      error=
      eval `pwd \
             | sed -ne '/\/build\//!{
                          s/.*/error=t/p
                          q
                        }
                        s/.*\/build\//guess_configuration=/
                        s/\// guess_toolkit=/
                        s/\/.*//
                        p'`

      case "$error" in
        t )
          exec 1>&2
          echo "$progname: your must be in a subdirectory of ${bq}build/$eq"
          exit 1
         ;;
      esac

      case "$configuration" in '' ) configuration=$guess_configuration ;; esac
      case "$toolkit"       in '' ) toolkit=$guess_toolkit             ;; esac
      case "$toolkit"       in '' ) toolkit=no                         ;; esac
    fi
   ;;
  * )
    # Don't molest configuration if it was actually specified
    #configuration=

    case "$toolkit" in '' ) toolkit=no ;; esac
   ;;
esac

# $configuration should come last
set fnord "$srcdir/configure" \
          --prefix=$prefix \
          $quiet \
          --with-x-toolkit=$toolkit \
	  ${1+"$@"} \
          $configuration
shift

echo ${PS4-+} ${1+"$@"}
${1+"$@"}

# econfig ends here
