#!/bin/sh
# ludeconf --- configure autoconfiscated packages for installation in lude

# Copyright (C) 1997, 1998 Noah S. Friedman

# Author: Noah Friedman <friedman@splode.com>
# Created: 1997-06-03

# $Id: ludeconf,v 2.7 1999/05/22 16:40:22 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:

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

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

# This string is expanded by rcs automatically when this file is checked out.
rcs_revision='$Revision: 2.7 $'
revision=`set fnord $rcs_revision; echo $3`

usage="Usage: $progname {$progname-options} {-- {configure-options}}
(Version $revision)

$progname options:
-c, --cc, --with-cc  {CC}    Force use of CC for C compiler.
                             If optional arg CC not specified, use ${bq}gcc$eq.
--configure          CONFPRG Run the CONFPRG ${bq}configure$eq program.
                             If not specified, guess location.

-x, --x, --with-x    {XDIR}  Use X Window System; look for libraries in
                             XDIR/lib and header files in XDIR/include.
                             If optional arg XDIR not specified, guess
                             location.
-X, --no-x, --without-x      Force configure not to use X Window System.

-C, --class          CLASS   Use CLASS as the lude system architecture.
                             If not specified, guess using ${bq}ludeclass$eq.
-D, --depot          DEPOT   Use DEPOT as the lude system depot.
                             If not specified, guess.
-M, --modification   MOD     Use MOD as the package modification.
                             If not specified, guess (usually ${bq}default$eq).
-S, --soft           PKG     Use PKG as the lude software package.
                             If not specified, guess.

-h, --help                   You're looking at it.
-n, --no-exec                Run configure script as a subprocess of this
                             program, rather than doing an ${bq}exec$eq.
                             See comments at the end of this shell script
                             for an explanation, but you will probably
                             never need this option.
-v, --verbose                Show lude parameters and configure command to
                             run. This option also caused $bq--verbose$eq to be
                             passed to the ${bq}configure$eq command as well.
-V, --version                Show version number and exit.
--debug                      Turn on shell debugging ($bq${bq}set -x$eq$eq).
"

##########

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

configure_program=
configure_x=

lude_class=${LUDE2_CLASS}
lude_depot=${LUDE2_DEPOT}
lude_mod=${LUDE2_MODIFICATION}
lude_soft=${LUDE2_SOFTWARE}

##########

# 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'.
# For optional args, you must use the `--foo=bar' long option syntax
# if the argument starts with `-', otherwise the argument will be ignored
# and treated as the next option.
#
# 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}:$optarg_optional" in
          set: ) optarg="$2" ; shift ; shift ;;
          set:?* )
            case "$2" in
              -* ) shift ;;
              * )  optarg="$2"; shift; shift ;;
            esac
           ;;
          : )
            option="$1"
            case "$option" in
              --*=* ) option=`echo "$option" | sed -e "1s/=.*//;q"` ;;
            esac
            echo "$progname: option $bq$option$eq requires argument." 1>&2
            echo "$progname: use $bq--help$eq to list option syntax." 1>&2
            exit 1
           ;;
          * ) shift ;;
        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
    -C | --class* | --lude-class* | --cl* | --lude-c* )
       eval "$getopt"
       lude_class="$optarg"
     ;;
    -D | --depot* | --lude-depot* | --dep* | --lude-d* )
       eval "$getopt"
       lude_depot="$optarg"
     ;;
    -M | --modification* | --lude-modification* | --mod* | --lude-m* )
       eval "$getopt"
       lude_mod="$optarg"
     ;;
    -S | --soft* | --lude-soft* | --s* | --lude-s* )
       eval "$getopt"
       lude_soft="$optarg"
     ;;

    -c | --cc* | --with-cc* )
       optarg_optional=t
       eval "$getopt"
       case "$optarg" in
         '' ) CC=gcc       ;;
         * )  CC="$optarg" ;;
       esac
       export CC
     ;;

    --configure* | --co* )
       eval "$getopt"
       configure_program="$optarg"
     ;;

    --debug | --deb* )
      debug=t
      shift
     ;;
    -h | --help | --h* | -\? )
      echo "$usage" 1>&2
      exit 0
     ;;
    -n | --no-exec | --no-e* )
      execp=
      shift
     ;;
    -v | --verbose | --verb* )
      verbose=t
      shift
     ;;
    -V | --version | --vers* )
      echo "$progname version $revision" 1>&2
      exit 0
     ;;
    -x | --x* | --with-x* )
      optarg_optional=t
      eval "$getopt"
      x11dir="$optarg"
      configure_x=yes
     ;;
    -X | --no-x | --without-x )
      configure_x=no
      shift
     ;;
    -- )     # 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 "$lude_depot" in
  '' )
    depots='
      /tools/ns
      /opt/lude
      /opt/local
      /opt/X11
      /opt/gnu
      /usr/local
    '
    for dir in $depots ; do
      if test -d "$dir/soft" ; then
        lude_depot="$dir/soft"
        break
      fi
    done

    case "$lude_depot" in
      '' )
        echo "$progname: Cannot determine LUDE depot." 1>&2
        exit 1
       ;;
    esac
   ;;
esac

sed_re_quote='s/\([][*.\\\/?|^$]\)/\\\1/g'

case "$lude_soft" in
  '' )
    re=`{ cd "$lude_depot" && pwd; } | sed -e "$sed_re_quote"`
    lude_soft=`pwd | sed -e "s/^$re\///" -e 's/\/.*//'`

    case "$lude_soft" in
      '' | */* )
        echo "$progname: Cannot determine LUDE software package name." 1>&2
        exit 1
       ;;
    esac
   ;;
esac

case "$lude_mod" in
  '' )
    re=`{ cd "$lude_depot/$lude_soft" && pwd; } | sed -e "$sed_re_quote"`
    lude_mod=`pwd | sed -e "s/^$re\/[^/]*\///" -e 's/\/.*//'`

    case "$lude_mod" in
      orig )
        exec 1>&2
        echo "$progname: ${bq}orig$eq: improper modification."
        echo "$progname: Specify ${bq}--modification orig$eq to force this."
        exit 1
       ;;
      '' | */* )
        echo "$progname: Can't determine LUDE software modification type." 1>&2
        exit 1
       ;;
    esac
   ;;
esac

case "$lude_class" in
  '' )
    re=`{ cd "$lude_depot/$lude_soft/src/$lude_mod" && pwd; } \
          | sed -e "$sed_re_quote"`
    lude_class=`pwd | sed -e "s/^$re\///" -e 's/\/.*//'`

    case "$lude_class" in
      '' | */* )
        lude_class=`ludeclass`
        echo "$progname: warning: Can't autodetect LUDE software class." 1>&2
        echo "$progname: warning: Defaulting to $bq$lude_class$eq." 1>&2
       ;;
    esac
esac

##########

case "$configure_program" in
  '' )
    configure_program=./configure
    for dir in . ../../orig "$lude_depot/$lude_soft/src/orig" ; do
      if test -f "$dir/configure" ; then
        configure_program="$dir/configure"
        break
      fi
    done
   ;;
esac

case "$configure_x:$x11dir" in
  no:* )
    set fnord --with-x-toolkit=no --without-x ${1+"$@"}
    shift
    x11dir=
   ;;
  yes: )
    x11dirs='
      /usr/X11R6
      /opt/X11
      /usr/local/X11
      /usr/openwin
      /usr
    '
    libexts='a so'
    x11dir=
    for dir in $x11dirs; do
      for ext in $libexts ; do
        if test -f $dir/lib/libX11.$ext ; then
          x11dir=$dir
          break
        fi
      done
    done

    case "$x11dir" in
      '' )
        echo "$progname: warning: Can't find X headers and libraries" 1>&2
       ;;
    esac
   ;;
esac

case "$x11dir" in
  '' ) : ;;
   * )
     set fnord \
         --with-x-toolkit-yes --with-x11 \
         --x-includes="$x11dir/include" --x-libraries="$x11dir/lib" \
         ${1+"$@"}
     shift
    ;;
esac

case "$verbose" in
  t )
    set fnord --verbose ${1+"$@"}
    shift
   ;;
esac

##########

install_prefix="$lude_depot/$lude_soft/run/$lude_mod"

set fnord \
    "$configure_program" \
    --prefix="$install_prefix/share" \
    --exec-prefix="$install_prefix/$lude_class" \
    ${1+"$@"}
shift

case "$verbose" in
  t )
    echo "$progname:" \
         "depot=$lude_depot," \
         "soft=$lude_soft," \
         "modification=$lude_mod," \
         "class=$lude_class"
    echo ${1+"$@"}
   ;;
esac

# When doing an `exec' from some bourne shells (this has been observed in
# Bash 1.14), the relative pathname of the command to be invoked may be
# canonicalized in argv[0] so that the configure script invoked will see $0
# set to e.g. "/opt/local/soft/.../configure" instead of just
# "./configure".  This may have the side effect of causing GNU-style
# makefiles to use VPATH and setting $top_srcdir and other variables to
# absolute names instead of relative names.  Normally this is harmless, but
# some makefiles use these variables explicitly and assume they are always
# relative.  This is a bug in those makefiles but it is easier to
# compensate for them here.  In general, you will never need to use the
# --no-exec option, but when you do, avoiding the use of `exec' usually
# clears up the problem.

case "$execp" in
  t ) exec ${1+"$@"} ;;
  * ) ${1+"$@"} ;;
esac

# ludeconf ends here
