#!/bin/sh
# Configuration script for GNU Emacs
#   Copyright (C) 1992 Free Software Foundation, Inc.

#This file is part of GNU Emacs.

#GNU Emacs 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 1, or (at your option)
#any later version.

#GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

# Shell script to edit files and make symlinks in preparation for
# compiling Emacs.
#
# Usage: configure machine
#
# If configure succeeds, it leaves its status in config.status.
# If configure fails after disturbing the status quo,
# 	config.status is removed.
#

# Remove any leading "." elements from the path name.  When this
# script is invoked from the config.status script, a new ./ gets added
# to the the front of the path, resulting in a "././" prefix.  That
# prefix is then recorded in the resulting config.status file, so
# basically each time config.status is used, it prepends another "./"
# to the filename.  This wouldn't be a problem, except that since
# progname gets recorded in all the Makefiles this script produces,
# move-if-changed thinks they're different when they're not.
progname="`echo $0 | sed 's:^\(\./\)*::'`"

short_usage="Type \`${progname} -usage' for more information about options."

usage_message="Usage: ${progname} MACHINENAME [-OPTION[=VALUE] ...]

This message needs to be updated.

Set compilation and installation parameters for GNU Emacs, and report.
MACHINENAME is the machine to build for.  See \`etc/MACHINES'.
Options are:
  -opsystem=SYSTEM - operating system to build for; see \`etc/MACHINES'.
  -prefix=DIR - where to install Emacs's library files
These options have reasonable defaults (in []s), and may not be needed:
  -g, -O - Passed to the compiler.  If omitted, use -O only.
  -cc=COMPILER - Which compiler to use.  Defaults to gcc if available.
  -libdir=DIR - where to look for arch-dependent library files
  -datadir=DIR - where to look for architecture-independent library files
  -bindir=DIR - where to install the Emacs executable, and some friends
  -lisppath=PATH - colon-separated list of Elisp directories
  -lockdir=DIR - where Emacs should do its file-locking stuff
  -window_system=none or [x11, if it is installed] - what window system to use
  -have_x_menu=yes or [no] - include menu support under X11
  -gnu_malloc=[yes] or no - use the GNU memory allocator
  -rel_alloc=[yes] or no - use compacting allocator for buffers
  -highpri=N - run at N points higher-than-normal priority
  -lisp_float_type=[yes] or no - Support native floating point in Elisp
If successful, ${progname} leaves its status in config.status.  If
unsuccessful after disturbing the status quo, config.status is removed."

if [ ! -r ./src/lisp.h ]; then
  echo "${progname}: Can't find Emacs sources in \`./src'.
Run this config script in the top directory of the Emacs source tree." >&2
  exit 1
fi

options=":\
usage:help:\
machine:opsystem:\
g:O:cc:\
prefix:bindir:emacsdir:datadir:lispdir:locallisppath:\
lisppath:buildlisppath:statedir:lockdir:libdir:mandir:infodir:\
gnu_malloc:rel_alloc:highpri:lisp_float_type:\
window_system:have_x_menu:\
"

boolean_opts=":\
g:O:\
gnu_malloc:rel_alloc:lisp_float_type:have_x_menu:\
"

config_h_opts=":\
highpri:gnu_malloc:rel_alloc:lisp_float_type:\
have_x_windows:have_x11:have_x_menu:\
c_switch_site:sigtype:\
"

prefix=
bindir=/usr/local/bin
gnu_malloc=yes
lisp_float_type=yes

# The default values for the following options are guessed at after other
# options have been checked and given values, so we set them to null here.
lisppath=""
datadir=""
libdir=""
lockdir=""
window_system=""
have_x_menu=""

# Record all the arguments, so we can save them in config.status.
arguments="$*"

echo "Examining options."
while [ $# != 0 ]; do
  arg="$1"
  case "${arg}" in
    -*)
      # Separate the switch name from the value it's being given.
      case "${arg}" in
        -*=* )
	  opt=`echo ${arg} | sed 's:^-\([^=]*\)=.*$:\1:'`
	  val=`echo ${arg} | sed 's:^-[^=]*=\(.*\)$:\1:'`
	  valomitted=no
	;;
        -* )
          # If FOO is a boolean argument, -FOO is equivalent to
          # -FOO=yes.  Otherwise, the value comes from the next
          # argument - see below.
	  opt=`echo ${arg} | sed 's:^-\(.*\)$:\1:'`
          val="yes"
          valomitted=yes
        ;;
      esac

      # Make sure the argument is valid and unambiguous.
      case ${options} in
        *:${opt}:* )	# Exact match.
          optvar=${opt}
        ;;
        *:${opt}*:${opt}*:* )	# Ambiguous prefix.
	  echo "\`-${opt}' is an ambiguous switch; it could be any of the following:"
	  # We can't just use tr to translate colons to newlines, since
	  # BSD sed and SYSV sed use different syntaxes for that.
	  spaced_options=`echo ${options} | tr ':' ' '`
	  echo `(for option in ${spaced_options}; do echo $option; done) \
	        | grep "^${opt}"`
	  echo ${short_usage}
	  exit 1
	;;
        *:${opt}*:* )		# Unambigous prefix.
	  optvar=`echo ${options} | sed 's/^.*:\('${opt}'[^:]*\):.*$/\1/'`
        ;;
	* )
	  (echo "\`-${opt}' is not a valid option."
	   echo "${short_usage}") | more
	  exit 1
	;;
      esac

      case "${optvar}" in
        usage | help)
          echo "${usage_message}" | more
          exit 1
        ;;
      esac

      # If the variable is supposed to be boolean, make sure the value
      # given is either "yes" or "no".  If not, make sure some value
      # was given.
      case "${boolean_opts}" in
        *:${optvar}:* )
	  case "${val}" in
	    y | ye | yes )	val=yes ;;
	    n | no )		val=no  ;;
	    * )
	      echo "The \`-${optvar}' option (\`-${opt}') is supposed to have a boolean
  value - set it to either \`yes' or \`no'." >&2
	      exit 1
	    ;;
	  esac
        ;;
        *)
	  if [ "${valomitted}" = "yes" ]; then
            if [ $# = 1 ]; then
	      (echo "${progname}: You must give a value for the \`-${opt}' option, as in
    \`-${opt}=FOO'."
	       echo "${short_usage}") | more
	      exit 1
	    fi
	    shift; val="$1"
	  fi
        ;;
      esac

      eval "${optvar}=\"${val}\""
    ;;
    *)
      machine=${arg}
    ;;
  esac
  shift
done

if [ "${machine}" = "" ]; then
  (echo "You must specify a machine name as an argument to ${progname}."
   echo "${short_usage}") | more
  exit 1
fi

echo "Checking the machine."
machfile="m/${machine}.h"
if [ ! -r src/${machfile} ]; then
  echo "${progname}: Emacs has no configuration info for the machine called
\`${machine}'.  Look at etc/MACHINES for the names of machines
that Emacs has been ported to." >&2
  exit 1
fi

echo "Checking the operating system."
if [ "${opsystem}" = "" ]; then

  echo "  No operating system explicitly specified."
  echo "  Guessing, based on machine."
  # Get the default operating system to go with the specified machine.
  opsystem=`grep 'USUAL-OPSYS="' src/${machfile} \
	    | sed 's/^.*USUAL-OPSYS="\([^"]*\)".*$/\1/'`

  if [ "${opsystem}" = "" ]; then
    echo "${progname}: Emacs's configuration files don't suggest what operating
system a \`${machine}' machine might run.  Try specifying the
operating system explicitly by passing ${progname} an
\`-opsystem=SYSTEM-NAME' flag.  Look at etc/MACHINES for the
names of operating systems that Emacs has been ported to." >&2
    exit 1
  fi

  if [ "${opsystem}" = "note" ]; then
    echo "The \`${machine}' machine can be used with more than one operating
system, and Emacs's configuration info isn't clever enough to figure
out which one you're running.  Run ${progname} with -machine and
-opsystem arguments as specified below for the appropriate system.
(This information comes from the file \`etc/MACHINES' - see that
file for more detail.)

" >&2
    sed < src/${machfile} -e '1,/NOTE-START/d' -e '/NOTE-END/,$d' | more
    echo
    exit 1
  fi

  opsysfile="s/${opsystem}.h"
  if [ ! -r src/${opsysfile} ]; then
    echo "${progname}: Emacs's configuration files say that the default
operating system for the machine \`${machine}' is \`${opsystem}',
but there is no configuration file for \`${opsystem}', so Emacs's
default info is screwed up.  Try specifying the operating system
explicitly by passing ${progname} an \`-opsystem=SYSTEM-NAME' flag." >&2
    exit 1
  fi
else
  opsysfile="s/${opsystem}.h"
  if [ ! -r src/${opsysfile} ]; then
    echo "${progname}: Emacs has no configuration info for the operating system
\`${opsystem}'.  Look at etc/MACHINES for the names of operating
systems that Emacs has been ported to." >&2
    exit 1
  fi
fi

if [ ! "${prefix}" ]; then
  prefix="/usr/local"
fi

if [ ! "${emacsdir}" ]; then
  emacsdir="${prefix}/emacs-19.0"
fi

if [ ! "${datadir}" ]; then
  datadir="${emacsdir}/etc"
fi

if [ ! "${lispdir}" ]; then
  lispdir="${emacsdir}/lisp"
fi

if [ ! "${locallisppath}" ]; then
  locallisppath="${emacsdir}/local-lisp"
fi

if [ ! "${lisppath}" ]; then
  lisppath="${locallisppath}:${lispdir}"
fi

if [ ! "${buildlisppath}" ]; then
  buildlisppath=../lisp
fi

if [ ! "${statedir}" ]; then
  statedir="${emacsdir}"
fi

if [ ! "${lockdir}" ]; then
  lockdir="${statedir}/lock"
fi

if [ "${libdir}" = "" ]; then
  libdir="${emacsdir}/arch-lib"
fi

if [ ! "${mandir}" ]; then
  mandir="/usr/man/man1"
fi

if [ ! "${infodir}" ]; then
  infodir="${prefix}/info"
fi

echo "Checking window system."
# Note that SYSV `tr' doesn't handle character ranges.
window_system="`echo ${window_system} \
                | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`"
case "${window_system}" in
  "none" | "x11" | "x10" ) ;;
  "x" ) window_system=x11 ;;
  "" )
    echo "  No window system specifed.  Looking for X Windows."
    window_system=none
    if [ -r /usr/lib/libX11.a -o -d /usr/include/X11 ]; then
      window_system=x11
    fi
  ;;
  * )
    echo "The \`-window_system' option must be set to \`none' or \`X11'." >&2
    exit 1
  ;;
esac

case "${window_system}" in
  x11 )
    have_x_windows=yes
    have_x11=yes
  ;;
  x10 )
    have_x_windows=yes
    have_x11=no
  ;;
  none )
    have_x_windows=no
    have_x11=no
  ;;
esac

if [ "${have_x_menu}" = "" ]; then
  have_x_menu=no
fi

echo "Checking for GCC."
case "${cc}" in
  "" )
    temppath=`echo $PATH | sed 's/^:/.:/
				s/::/:.:/g
				s/:$/:./
				s/:/ /g'`
    cc=`(
      for dir in ${temppath}; do
        if [ -f ${dir}/gcc ]; then echo gcc; exit 0; fi
      done
      echo cc
    )`
  ;;
esac

case "${cc}" in
  "gcc" )
    # With GCC, both O and g should default to yes, no matter what
    # the other is.
    case "${O},${g}" in
      ,  ) O=yes; g=yes ;;
      ,* ) O=yes;	;;
      *, )        g=yes ;;
    esac
  ;;
  "*" )
    # With other compilers, treat them as mutually exclusive,
    # defaulting to debug.
    case "${O},${g}" in
      ,    ) O=no ; g=yes ;;
      ,no  ) O=yes;       ;;
      ,yes ) O=no ;       ;;
      no,  )        g=yes ;;
      yes, )        g=no  ;;
    esac
  ;;
esac

# What is the return type of a signal handler?  We run
# /usr/include/signal.h through cpp and grep for the declaration of
# the signal function.  Yuck.
echo "Looking for return type of signal handler functions."
signal_h_file=''
if [ -r /usr/include/signal.h ]; then
  signal_h_file=/usr/include/signal.h
elif [ -r /usr/include/sys/signal.h ]; then
  signal_h_file=/usr/include/sys/signal.h
fi
sigtype=void
if [ "${signal_h_file}" ]; then
  sigpattern='[ 	]*([ 	]*\*[ 	]*signal[ 	]*('

  # We make a copy whose name ends in .c, so the compiler
  # won't complain about having only been given a .h file.
  tempcname="configure.tmp.$$.c"
  cp ${signal_h_file}  ${tempcname}
  if ${cc} -E ${tempcname} | grep "int${sigpattern}" > /dev/null; then
    sigtype=int
  fi
  rm -f ${tempcname}
fi

echo "Examining the machine- and system-dependent files to find out"
echo " - which libraries the lib-src programs will want, and"
echo " - whether the GNU malloc routines are usable."
tempcname="configure.tmp.$$.c"
echo '#include "src/'${machfile}'"
#include "src/'${opsysfile}'"
#ifndef LIBS_MACHINE
#define LIBS_MACHINE
#endif
#ifndef LIBS_SYSTEM
#define LIBS_SYSTEM
#endif
@configure@ libsrc_libs=LIBS_MACHINE LIBS_SYSTEM
#ifdef SYSTEM_MALLOC
@configure@ system_malloc=yes
#else
@configure@ system_malloc=no
#endif
' > ${tempcname}
eval `${cc} -E ${tempcname} \
      | grep '@configure@' \
      | sed -e 's/^@configure@//'`
rm ${tempcname}

# Do the opsystem or machine files prohibit the use of the GNU malloc?
if [ "${system_malloc}" = "yes" ]; then
  gnu_malloc=no
  gnu_malloc_reason="
  (The GNU allocators don't work with this machine and/or operating system.)"
fi

if [ ! "${rel_alloc}" ]; then
  rel_alloc=${gnu_malloc}
fi

rm -f config.status
set -e

# Make the proper settings in the config file.
echo "Making src/config.h from src/config.h.in"
if [ "${highpri}" != "" ]; then
  highpri="(-${highpri})"
fi
case "${g}" in
  "yes" ) c_switch_site="${c_switch_site} -g" ;;
esac
case "${O}" in
  "yes" ) c_switch_site="${c_switch_site} -O" ;;
esac
sed_flags="-e 's:@machine@:${machfile}:'"
sed_flags="${sed_flags} -e 's:@opsystem@:${opsysfile}:'"
for flag in `echo ${config_h_opts} | tr ':' ' '`; do
  # Note that SYSV `tr' doesn't handle character ranges.
  cflagname=`echo ${flag} \
	     | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
  val=`eval echo '$'${flag}`
  case ${val} in
    no | "")
      f="-e 's:.*#define ${cflagname}.*:/\\* #define ${cflagname} \\*/:'"
    ;;
    yes)
      f="-e 's:.*#define ${cflagname}.*:#define ${cflagname}:'"
    ;;
    *)
      f="-e 's:.*#define ${cflagname}.*:#define ${cflagname} ${val}:'"
    ;;
  esac
  sed_flags="${sed_flags} ${f}"
done
rm -f src/config.h.tmp
eval '/bin/sed '${sed_flags}' < src/config.h.in > src/config.h.tmp'
./move-if-change src/config.h.tmp src/config.h
# Remind people not to edit this.
chmod -w src/config.h

# Modify the parameters in the top makefile.
echo "Producing ./Makefile from ./Makefile.in."
rm -f Makefile.tmp
(echo "# This file is generated by \`${progname}' from \`./Makefile.in'.
# If you are thinking about editing it, you should seriously consider
# editing \`./Makefile.in' itself, or running \`${progname}' instead."
 /bin/sed < Makefile.in					\
 -e '/^# DIST: /d'					\
 -e 's;^\(prefix=\).*$;\1'"${prefix};"			\
 -e 's;^\(bindir=\).*$;\1'"${bindir};"			\
 -e 's;^\(emacsdir=\).*$;\1'"${emacsdir};"		\
 -e 's;^\(datadir=\).*$;\1'"${datadir};"		\
 -e 's;^\(lispdir=\).*$;\1'"${lispdir};"		\
 -e 's;^\(locallisppath=\).*$;\1'"${locallisppath};"	\
 -e 's;^\(lisppath=\).*$;\1'"${lisppath};"		\
 -e 's;^\(buildlisppath=\).*$;\1'"${buildlisppath};"	\
 -e 's;^\(statedir=\).*$;\1'"${statedir};"		\
 -e 's;^\(lockdir=\).*$;\1'"${lockdir};"		\
 -e 's;^\(libdir=\).*$;\1'"${libdir};"			\
 -e 's;^\(mandir=\).*$;\1'"${mandir};"			\
 -e 's;^\(infodir=\).*$;\1'"${infodir};"		\
) > ./Makefile.tmp
./move-if-change Makefile.tmp Makefile
# Remind people not to edit this.
chmod -w ./Makefile

# Modify the parameters in the `build-install' script.
echo "Producing ./build-install from ./build-install.in."
rm -f ./build-install.tmp
(echo "# This file is generated by \`${progname}' from \`./build-install.in'.
# If you are thinking about editing it, you should seriously consider
# editing \`./build-install.in' itself, or running \`${progname}' instead."
 /bin/sed < build-install.in				\
 -e 's;^\(prefix=\).*$;\1'"${prefix};"			\
 -e 's;^\(bindir=\).*$;\1'"${bindir};"			\
 -e 's;^\(lisppath=\).*$;\1'"${lisppath};"		\
 -e 's;^\(datadir=\).*$;\1'"${datadir};"		\
 -e 's;^\(lockdir=\).*$;\1'"${lockdir};"		\
 -e 's;^\(libdir=\).*$;\1'"${libdir};") > ./build-install.tmp
./move-if-change build-install.tmp build-install
# Remind people not to edit this.
chmod -w build-install
chmod +x build-install

# Modify the parameters in the src makefile.
echo "Producing src/Makefile from src/Makefile.in."
rm -f src/Makefile.tmp
(echo "# This file is generated by \`${progname}' from \`Makefile.in'.
# If you are thinking about editing it, you should seriously consider
# editing \`Makefile.in' itself, or running \`${progname}' instead."
 /bin/sed < src/Makefile.in				\
 -e 's;^\(CC[ ]*=\).*$;\1'"${cc};") > src/Makefile.tmp
./move-if-change src/Makefile.tmp src/Makefile
# Remind people not to edit this.
chmod -w src/Makefile

# Modify the parameters in the lib-src makefile.
echo "Producing lib-src/Makefile from lib-src/Makefile.in."
rm -f lib-src/Makefile.tmp
(echo "# This file is generated by \`${progname}' from \`Makefile.in'.
# If you are thinking about editing it, you should seriously consider
# editing \`Makefile.in' itself, or running \`${progname}' instead."
 /bin/sed < lib-src/Makefile.in				\
 -e 's;^\(CFLAGS=\).*$;\1'"${c_switch_site};"		\
 -e 's;^\(LOADLIBES=\).*$;\1'"${libsrc_libs};"		\
 -e 's;^\(CC=\).*$;\1'"${cc};") > lib-src/Makefile.tmp
./move-if-change lib-src/Makefile.tmp lib-src/Makefile
# Remind people not to edit this.
chmod -w lib-src/Makefile


# Create a verbal description of what we have done.
if [ "${highpri}" ]; then
  desc_highpri="${highpri}"
else
  desc_highpri="none"
fi
message="Configured for machine \`${machine}' running \`${opsystem}'.
The following values have been set in ./Makefile and ./build-install:
  \`make install' or \`build-install' will placed executables in
        ${bindir}.
  Emacs's lisp search path will be
        \`${lisppath}'.
  Emacs will look for its architecture-independent data in
        ${datadir}.
  Emacs will look for its utility programs and other architecture-
  dependent data in
        ${libdir}.
  Emacs will keep track of file-locking in
        ${lockdir}.
The following values have been set in src/config.h:
  At how much higher than normal priority should Emacs run? ${desc_highpri}
  Should Emacs use the GNU version of malloc?             ${gnu_malloc}${gnu_malloc_reason}
  Should Emacs use the relocating allocator for buffers?  ${rel_alloc}
  Should Emacs support a floating point Elisp type?       ${lisp_float_type}
  What window system should Emacs use?                    ${window_system}
  Should Emacs support mouse menus, which require X11?    ${have_x_menu}
  What compiler should emacs be built with?               ${cc}
  Should the compilation use \`-g' and/or \`-O'?           ${c_switch_site- neither}"

# Document the damage we have done by writing config.status.

echo '#!/bin/sh' > config.status

echo "# This file is generated by \`${progname}.'
# If you are thinking about editing it, you should seriously consider
# running \`${progname}' instead.
" >> config.status
echo "${message}" | sed -e 's/^/# /' >> config.status
echo "'${progname}' ${arguments} "'$@' >> config.status
# Remind people not to edit this.
chmod -w config.status
chmod +x config.status

# Print the description.
echo
echo "${message}"

exit 0
