#!/bin/sh
## ====================================================================
## Copyright (c) 1995-1998 The Apache Group.  All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
##
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer. 
##
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in
##    the documentation and/or other materials provided with the
##    distribution.
##
## 3. All advertising materials mentioning features or use of this
##    software must display the following acknowledgment:
##    "This product includes software developed by the Apache Group
##    for use in the Apache HTTP server project (http://www.apache.org/)."
##
## 4. The names "Apache Server" and "Apache Group" must not be used to
##    endorse or promote products derived from this software without
##    prior written permission. For written permission, please contact
##    apache@apache.org.
##
## 5. Products derived from this software may not be called "Apache"
##    nor may "Apache" appear in their names without prior written
##    permission of the Apache Group.
##
## 6. Redistributions of any form whatsoever must retain the following
##    acknowledgment:
##    "This product includes software developed by the Apache Group
##    for use in the Apache HTTP server project (http://www.apache.org/)."
##
## THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
## EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
## ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
## OF THE POSSIBILITY OF SUCH DAMAGE.
## ====================================================================
##
## This software consists of voluntary contributions made by many
## individuals on behalf of the Apache Group and was originally based
## on public domain software written at the National Center for
## Supercomputing Applications, University of Illinois, Urbana-Champaign.
## For more information on the Apache Group and the Apache HTTP server
## project, please see <http://www.apache.org/>.

# HISTORY:
#
# Apache configuration script, first cut --- rst.
# Don't like it?  Inspired to do something better?  Go for it.
#
# second cut --- jmj
# At this point we change what Configuration contains. It maintain
# contains comments, specific compiler flags, a list of included
# modules and "rules". These rules are used to allow Configure to
# be totally configured from Configuration.
#
# third cut --- rse
# Big cleanup of the generated Makefiles and remove of
# some old kludges

# Uses 6 supplemental scripts located in ./helpers:
#	CutRule: Determines the value for a specified Rule
#	GuessOS: Uses uname to determine OS/platform
#	PrintPath: generic "type" or "whence" replacement
#	TestCompile: Can check for libs and if $(CC) is ANSI
#	 (i.e., a simple "sanity check")
#	mfhead:
#	fp2rp:

exitcode=0
trap 'rm -f $tmpfile $tmpfile2 $tmpfile3 $tmpconfig $awkfile; exit $exitcode' 0 1 2 3 15

####################################################################
## Set up some defaults
##
file=Configuration
tmpfile=htconf.$$
tmpfile2=$tmpfile.2
tmpfile3=$tmpfile.3
awkfile=$tmpfile.4
tmpconfig=$tmpfile.5
SUBDIRS='$(OSDIR) ap main modules' 

####################################################################
## Now handle any arguments, which, for now, is -file
## to select an alternate Configuration file
##
while [ "x$1" != "x" ]; do
  if [ "x$1" = "x-file" ] ; then
    shift 1; file=$1; shift 1
    if [ ! -r $file ]; then
      echo "$file does not exist or is not readable."
      exitcode=1
      exit 1
    fi
  else
    echo "ERROR: Bad command line option '$1'"
    echo "  Please read the file INSTALL."
    exit 1
  fi
done
if [ ! -r $file ]; then
  echo "Can't see or read \"$file\""
  echo "Please copy Configuration.tmpl to $file, edit it for your platform,"
  echo "and re-run $0 again."
  exitcode=1
  exit 1
fi

####################################################################
## Now see if Configuration.tmpl is more recent than $file. If
## so, then we complain and bail out
##
if ls -lt Configuration.tmpl $file | head -1 | \
  grep 'Configuration.tmpl' > /dev/null
then
  echo "Configuration.tmpl is more recent than $file;"
  echo "Make sure that $file is valid and, if it is, simply"
  echo "'touch $file' and re-run $0 again."
  exitcode=1
  exit 1
fi

echo "Using config file: $file"

####################################################################
## From the Configuration file, create a "cleaned-up" version
## that's easy to scan
##

# Strip comments and blank lines, remove whitespace around
# "=" assignments, change Rules to comments and then remove whitespace
# before Module declarations
sed 's/#.*//' $file | \
 sed '/^[ 	]*$/d' | \
 sed 's/[ 	]*$//' | \
 sed 's/[ 	]*=[ 	]*/=/' | \
 sed '/^Rule[ 	]*/d' | \
 sed 's/^[ 	]*AddModule/AddModule/' | \
 sed 's/^[ 	]*%AddModule/%AddModule/' | \
 sed 's/^[ 	]*SharedModule/SharedModule/' | \
 sed 's/^[ 	]*Module/Module/' | \
 sed 's/^[ 	]*%Module/%Module/' > $tmpfile

# Determine if shared objects are used
using_shlib=`grep  '^SharedModule' $tmpfile >/dev/null && echo 1`

# But perhaps later via apxs when just mod_so is compiled in!
if [ ".$using_shlib" = . ]; then
    using_shlib=`grep  '^AddModule modules/standard/mod_so.o' $tmpfile >/dev/null && echo 1`
fi

# Only "assignment" ("=") statements and Module lines
# should be left at this point. If there is other stuff
# we bail out
if egrep -v '^%?Module[ 	]+[A-Za-z0-9_]+[ 	]+[^ 	]+$' $tmpfile \
 | egrep -v '^%?AddModule[ 	]+[^ 	]+$' \
 | egrep -v '^SharedModule[ 	]+[^ 	]+$' \
 | grep -v = > /dev/null
then
  echo "Syntax error --- The configuration file is used only to"
  echo "define the list of included modules or to set Makefile"
  echo "options or Configure rules, and I don't see that at all:"
  egrep -v '^%?Module[ 	]+[A-Za-z0-9_]+[ 	]+[^ 	]+$' $tmpfile \
   | egrep -v '^%?AddModule[ 	]+[^ 	]+$'  \
   | egrep -v '^%?SharedModule[ 	]+[^ 	]+$'  \
   | grep -v =
  exitcode=1
  exit 1
fi

####################################################################
## If we find the directory /usr/local/etc/httpd and there is
## no HTTPD_ROOT flag set in the Configuration file we assume
## that the user was using the old default root directory
## and issue a notice about it.
##
if [ -d /usr/local/etc/httpd/ ]
then
    if egrep '^EXTRA_CFLAGS.*HTTPD_ROOT' $file >/dev/null
    then
      :
    else
      echo " | Please note that the default httpd root directory has changed"
      echo " | from '/usr/local/etc/httpd/' to '/usr/local/apache/.'"
      echo " | You may add '-DHTTPD_ROOT=\\\"/usr/local/etc/httpd\\\"' to EXTRA_CFLAGS"
      echo " | in your Configuration file (and re-run Configure) or start"
      echo " | httpd with the option '-d /usr/local/etc/httpd' if you still"
      echo " | want to use the old root directory for your server."
    fi
fi

####################################################################
## Start creating the Makefile. We add some comments and
## then fold in the modules that were included in Configuration
##
echo "Creating Makefile"
./helpers/mfhead . $file > Makefile

####################################################################
## Now we create a stub file, called Makefile.config, which
## just includes those assignments (eg: CC=gcc) in Configuration
##
awk >Makefile.config <$tmpfile '
    BEGIN {
	print "##"
	print "##  Inherited Makefile options from Configure script"
	print "##  (Begin of automatically generated section)"
	print "##"
	print "SRCDIR=."
    } 
    /\=/ { print } 
    '

####################################################################
## Extract the rules.
##
RULE_WANTHSREGEX=`./helpers/CutRule WANTHSREGEX $file`
RULE_STATUS=`./helpers/CutRule STATUS $file`
RULE_SOCKS4=`./helpers/CutRule SOCKS4 $file`
RULE_IRIXNIS=`./helpers/CutRule IRIXNIS $file`
RULE_IRIXN32=`./helpers/CutRule IRIXN32 $file`
RULE_PARANOID=`./helpers/CutRule PARANOID $file`
RULE_SHARED_CORE=`./helpers/CutRule SHARED_CORE $file`

####################################################################
## Rule SHARED_CORE implies required DSO support
##
if [ "$RULE_SHARED_CORE" = "yes" ]; then
	using_shlib=1
fi

####################################################################
## Preset some "constants";
## can be overridden on a per-platform basis below.
##
DBM_LIB="-ldbm"
DB_LIB="-ldb"
SHELL="/bin/sh"
TARGET="httpd"
SUBTARGET="target_static"
SHLIB_SUFFIX_LIST=""
AP_PRINTPATH=PrintPath
export AP_PRINTPATH
CAT="cat"
if ./helpers/$AP_PRINTPATH -s ranlib; then
    RANLIB="ranlib"
else
    RANLIB="true"
fi

####################################################################
## Now we determine the OS/Platform automagically, thanks to
## GuessOS, a home-brewed OS-determiner ala config.guess
##
## We adjust CFLAGS, LIBS, LDFLAGS and INCLUDES (and other Makefile
## options) as required. Setting CC and OPTIM here has no effect
## if they were set in Configure.
##
## Also, we set DEF_WANTHSREGEX and to the appropriate
## value for each platform.
##
## As more PLATFORMs are added to Configuration.tmpl, be sure to
## add the required lines below.
##
PLAT=`./helpers/GuessOS`
SHELL="/bin/sh"
OSDIR="os/unix"

case "$PLAT" in
    *mint)
        OS="MiNT"
        CFLAGS="-DMINT"
        LIBS="$LIBS -lportlib -lsocket"
        DEF_WANTHSREGEX=yes
        ;;
    *MPE/iX*)
        OS='MPE/iX'
	CFLAGS="$CFLAGS -DMPE -D_POSIX_SOURCE -D_SOCKET_SOURCE"
	LIBS="$LIBS -lsocket -lsvipc"
	LDFLAGS="$LDFLAGS -Xlinker \"-WL,cap=ia,ba,ph,pm;nmstack=1024000\""
	CAT="/bin/cat" # built-in cat is badly broken for stdin redirection
	;;
    *-apple-aux3*)
	OS='A/UX 3.1.x'
	CFLAGS="$CFLAGS -DAUX3 -D_POSIX_SOURCE"
	LIBS="$LIBS -lposix -lbsd"
	LDFLAGS="$LDFLAGS -s"
	DEF_WANTHSREGEX=no
	;;
    i386-ibm-aix*)
    	OS='IBM AIX PS/2'
	CFLAGS="$CFLAGS -DAIX -U__STR__ -DUSEBCOPY"
	DEF_WANTHSREGEX=no
	;;
    *-ibm-aix[1-3].*|*-ibm-aix4.[0-1])
        OS='IBM AIX < v4.2'
        CFLAGS="$CFLAGS -DAIX -DNEED_RLIM_T -U__STR__"
        ;;
    *-ibm-aix*)
        OS='IBM AIX >= 4.2'
        CFLAGS="$CFLAGS -DAIX -U__STR__"
	LDFLAGS="$LDFLAGS -lm"
        ;;
    *-apollo-*)
	OS='Apollo Domain'
	CFLAGS="$CFLAGS -DAPOLLO"
	;;
    *-dg-dgux*)
	OS='DG/UX 5.4'
	CFLAGS="$CFLAGS -DDGUX"
	DEF_WANTHSREGEX=yes
	;;
    *OS/2*)
	OSDIR="os/emx"
	DEF_WANTHSREGEX=yes
	OS='EMX OS/2'
	CFLAGS="$CFLAGS -Zbsd-signals -Zbin-files -DTCPIPV4 -g"
	LDFLAGS="$LDFLAGS -Zexe"
	LIBS="$LIBS -lsocket -lufc -lbsd"
	DBM_LIB="-lgdbm"
	SHELL=sh
	AP_PRINTPATH=PrintPathOS2
	export AP_PRINTPATH
	;;
    *-hi-hiux)
	OS='HI-UX'
	CFLAGS="$CFLAGS -DHIUX"
	;;
    *-hp-hpux10.*)
	OS='HP-UX 10'
	CFLAGS="$CFLAGS -DHPUX10"
 	case "$PLAT" in
 	  *-hp-hpux10.01)
	       # We know this is a problem in 10.01.
               # Not a problem in 10.20.  Otherwise, who knows?
               CFLAGS="$CFLAGS -DSELECT_NEEDS_CAST"
               ;;	     
 	esac
	DEF_WANTHSREGEX=yes
	;;
    *-hp-hpux*)
	OS='HP-UX'
	CFLAGS="$CFLAGS -DHPUX"
	DEF_WANTHSREGEX=yes
	LIBS="$LIBS -lm"
	;;
    *-sgi-irix64)
        # Note: We'd like to see patches to compile 64-bit, but for now...
	echo "You are running 64-bit Irix. For now, we will compile 32-bit"
	echo "but if you would care to port to 64-bit, send us the patches."
	DEF_WANTHSREGEX=yes
	DBM_LIB=""
	if [ "$RULE_IRIXNIS" = "yes" ]; then
	    OS='SGI IRIX-64 w/NIS'
	    CFLAGS="$CFLAGS -DIRIX"
	    LIBS="$LIBS -lsun"
	else
	    OS='SGI IRIX-64'
	    CFLAGS="$CFLAGS -DIRIX"
	fi
	;;
    *-sgi-irix32)
	DEF_WANTHSREGEX=yes
	DBM_LIB=""
	if [ "$RULE_IRIXN32" = "yes" ]; then
	    if [ "$RULE_IRIXNIS" = "yes" ]; then
		OS='SGI IRIX-32 w/NIS'
	    else
		OS='SGI IRIX-32'
	    fi
	else
	    if [ "$RULE_IRIXNIS" = "yes" ]; then
		OS='SGI IRIX w/NIS'
	    else
		OS='SGI IRIX'
	    fi
	fi
	CFLAGS="$CFLAGS -DIRIX"
	;;
    *-sgi-irix)
	DEF_WANTHSREGEX=yes
	DBM_LIB=""
	if [ "$RULE_IRIXNIS" = "yes" ]; then
	    OS='SGI IRIX w/NIS'
	    CFLAGS="$CFLAGS -DIRIX"
	    LIBS="$LIBS -lsun"
	else
	    OS='SGI IRIX'
	    CFLAGS="$CFLAGS -DIRIX"
	fi
	;;
    *-linux2)
	DEF_WANTHSREGEX=yes
	OS='Linux'
	CFLAGS="$CFLAGS -DLINUX=2"
	LIBS="$LIBS -lm"
	;;
    *-linux1)
	DEF_WANTHSREGEX=yes
	OS='Linux'
	CFLAGS="$CFLAGS -DLINUX=1"
	;;
    *-lynx-lynxos)
	OS='LynxOS 2.x'
	CFLAGS="$CFLAGS -D__NO_INCLUDE_WARN__ -DLYNXOS"
	LIBS="$LIBS -lbsd -lcrypt"
	DEF_WANTHSREGEX=yes
	;;
    *486-*-bsdi*)
	OS='BSDI w/486'
	CFLAGS="$CFLAGS -m486"
	DBM_LIB=""
	DB_LIB=""
	;;
    *-bsdi*)
	OS='BSDI'
	DBM_LIB=""
	DB_LIB=""
	;;
    *-netbsd*)
	OS='NetBSD'
	CFLAGS="$CFLAGS -DNETBSD"
	LIBS="$LIBS -lcrypt"
	DBM_LIB=""
	DB_LIB=""
	;;
    *-freebsd*)
    	PLATOSVERS=`echo $PLAT | sed 's/^.*freebsd//'`
	OS="FreeBSD $PLATOSVERS"
	case "$PLATOSVERS" in
	    [23]*)
		DEF_WANTHSREGEX=no
		CFLAGS="$CFLAGS -funsigned-char"
		;;
	esac
	LIBS="$LIBS -lcrypt"
	DBM_LIB=""
	DB_LIB=""
	;;
    *-openbsd*)
	OS='OpenBSD'
	DBM_LIB=""
	;;
    *-next-nextstep*)
	OS='NeXT'
	CFLAGS="$CFLAGS -DNEXT"
	DEF_WANTHSREGEX=yes
	RANLIB="sleep 5; /bin/ranlib"
	# ranlib on most NeXTs sets the time wrong. 5 secs wait does much good
	;;
    *-dec-osf*)
	OS='DEC OSF/1'
	CFLAGS="$CFLAGS -DOSF1"
	LIBS="$LIBS -lm"
	;;
    *-qnx)
	OS='QNX'
	CFLAGS="$CFLAGS -DQNX"
	LIBS="$LIBS -N128k -lsocket -lunix"
	DEF_WANTHSREGEX=yes
	;;
    *-qnx32)
	OS='QNX32'
	CFLAGS="$CFLAGS -DQNX -mf -3"
	LIBS="$LIBS -N128k -lsocket -lunix"
	DEF_WANTHSREGEX=yes
	;;
    *-isc4*)
	OS='ISC 4'
	CC='gcc'
	CFLAGS="$CFLAGS -posix -DISC"
	LDFLAGS="$LDFLAGS -posix"
	LIBS="$LIBS -linet"
	DEF_WANTHSREGEX=yes
	;;
    *-sco3*)
	OS='SCO 3'
	CFLAGS="$CFLAGS -DSCO -Oacgiltz"
	LIBS="$LIBS -lPW -lsocket -lmalloc -lcrypt_i"
	DEF_WANTHSREGEX=yes
	;;
    *-sco5*)
	OS='SCO 5'
	CFLAGS="$CFLAGS -DSCO5"
	LIBS="$LIBS -lsocket -lmalloc -lprot"
	DEF_WANTHSREGEX=no
	;;
    *-sco_sv*)
	OS='SCO SV'
	CFLAGS="$CFLAGS -DSCO"
	LIBS="$LIBS -lPW -lsocket -lmalloc -lcrypt_i"
	DEF_WANTHSREGEX=yes
	;;
    *-solaris2*)
    	PLATOSVERS=`echo $PLAT | sed 's/^.*solaris2.//'`
	OS="Solaris $PLATOSVERS"
	CFLAGS="$CFLAGS -DSOLARIS2=$PLATOSVERS"
	LIBS="$LIBS -lsocket -lnsl"
	DBM_LIB=""
	case "$PLATOSVERS" in
	    2[01234]*)
		DEF_WANTHSREGEX=yes
		;;
	    *)
		DEF_WANTHSREGEX=no
		;;
	esac
	;;
    *-sunos4*)
	OS='SunOS 4'
	CFLAGS="$CFLAGS -DSUNOS4 -DUSEBCOPY"
	DEF_WANTHSREGEX=yes
	;;
    *-unixware1)
	DEF_WANTHSREGEX=yes
	OS='UnixWare 1.x'
	CFLAGS="$CFLAGS -DUW=100"
	LIBS="$LIBS -lsocket -lnsl -lcrypt"
	;;
    *-unixware2)
	DEF_WANTHSREGEX=yes
	OS='UnixWare 2.x'
	CFLAGS="$CFLAGS -DUW=200"
	LIBS="$LIBS -lsocket -lnsl -lcrypt -lgen"
	;;
    *-unixware211)
	OS='UnixWare 2.1.1'
	CFLAGS="$CFLAGS -DUW=211"
	LIBS="$LIBS -lsocket -lnsl -lcrypt -lgen"
	;;
    *-unixware212)
	OS='UnixWare 2.1.2'
	CFLAGS="$CFLAGS -DUW=212"
	LIBS="$LIBS -lsocket -lnsl -lcrypt -lgen"
	DBM_LIB=""
	;;
    maxion-*-sysv4*)
    	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4"
	DEF_WANTHSREGEX=yes
	LIBS="$LIBS -lsocket -lnsl -lc -lgen"
	;;
    BS2000*-sni-sysv4*)
	OS='BS2000'
	OSDIR='os/bs2000'
	CC='c89 -XLLML -XLLMK'
	CFLAGS="$CFLAGS -DCHARSET_EBCDIC -DSVR4 -D_XPG_IV"
	DEF_WANTHSREGEX=yes
	LIBS="$LIBS -lsocket -lnsl -lc"
	;;
    *-sni-sysv4*)
	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4 -D_XPG_IV -DHAS_DLFCN -DUSE_MMAP_FILES -DUSE_SYSVSEM_SERIALIZED_ACCEPT -DNEED_UNION_SEMUN"
	DEF_WANTHSREGEX=yes
	LIBS="$LIBS -lsocket -lnsl -lc"
	;;
    DS/90\ 7000-*-sysv4*)
	OS='UXP/DS'
	CFLAGS="$CFLAGS -DUXPDS"
	LIBS="$LIBS -lsocket -lnsl"
	DEF_WANTHSREGEX=yes
	;;
    *-tandem-sysv4*)
	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4"
	LIBS="$LIBS -lsocket -lnsl"
	DEF_WANTHSREGEX=yes
	;;
    *-sysv4*)
	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4"
	LIBS="$LIBS -lsocket -lnsl -lc"
	;;
    88k-encore-sysv4)
	OS='Encore UMAX V'
	CFLAGS="$CFLAGS -DSVR4 -DENCORE"
	DEF_WANTHSREGEX=yes
	LIBS="$LIBS -lPW"
	;;
    *-uts*)
	OS='Amdahl UTS'
	CFLAGS="$CFLAGS -Xa -eft -DUTS21"
	LIBS="$LIBS -lsocket -lbsd -la"
	;;
    *-ultrix)
	OS='ULTRIX'
	CFLAGS="-DULTRIX"
	DEF_WANTHSREGEX=yes
	SHELL="/bin/sh5"
	;;
    *powerpc-tenon-machten*)
	OS='MachTen PPC'
	LDFLAGS="$LDFLAGS -Xlstack=0x14000 -Xldelcsect"
	;;
    *-machten*)
	OS='MachTen 68K'
	LDFLAGS="$LDFLAGS -stack 0x14000"
	DEF_WANTHSREGEX=yes
	;;
    *convex-v11*)
	OS='CONVEXOS11'
	CFLAGS="$CFLAGS -ext -DCONVEXOS11"
	OPTIM="-O1" # scalar optimization only
	CC='cc'
	DEF_WANTHSREGEX=yes
	;;
    i860-intel-osf1)
	DEF_WANTHSREGEX=yes
	OS='Paragon OSF/1'
	CFLAGS="$CFLAGS -DPARAGON"
	;;
    *DYNIX*)
	DEF_WANTHSREGEX=yes
	OS='SEQUENT'
	CFLAGS="$CFLAGS -DSEQUENT"
	;;
    *NEWS-OS*)
	DEF_WANTHSREGEX=yes
	OS='SONY NEWS-OS'
	CFLAGS="$CFLAGS -DNEWSOS"
	;;
    *-riscix)
	OS='Acorn RISCix'
	CFLAGS="$CFLAGS -DRISCIX"
	OPTIM="-O"
	MAKE="make"
	DEF_WANTHSREGEX=yes
	;;
    *-BeOS*)
	OS='BeOS';
	CFLAGS="$CFLAGS -DBEOS"
	DEF_WANTHSREGEX=yes
	;;
    *) # default: Catch systems we don't know about
    	echo Sorry, but we cannot grok \"$PLAT\"
	echo uname -m
	uname -m
	echo uname -r
	uname -r
	echo uname -s
	uname -s
	echo uname -v
	uname -v
	echo uname -X
	uname -X
	echo Ideally, read the file PORTING, do what it says, and send the
	echo resulting patches to The Apache Group by filling out a report
	echo form at http://bugs.apache.org/ - or, if your browser isn\'t 
	echo forms-capable, you can send them via email to 
	echo apache-bugs@apache.org.  If you don\'t wish to do the port
	echo yourself, please submit this output rather than the patches.
	echo Thank you
	exitcode=1
	exit 1
	;;
esac

####################################################################
## Show user what OS we came up with
##
echo " + configured for $OS platform"

####################################################################
## And adjust/override WANTHSREGEX as needed
##
if [ "$RULE_WANTHSREGEX" = "default" ]; then
	if [ "x$DEF_WANTHSREGEX" = "x" ]; then
		RULE_WANTHSREGEX=yes
	else
		RULE_WANTHSREGEX=$DEF_WANTHSREGEX
	fi
fi

####################################################################
## Now we determine the C-compiler and optimization level
## to use. Settings of CC and OPTIM in Configuration have
## the highest precedence; next comes any settings from
## the above "OS-specific" section. If still unset,
## then we look for a known compiler somewhere in PATH
##

# First, look for a CC=<whatever> setting in Configuration (recall, we
# copied these to Makefile.config)
#
# If $TCC is null, then no such line exists in Configuration
#
TCC=`egrep '^CC=' Makefile.config | tail -1 | awk -F= '{print $2}'`
if [ "x$TCC" = "x" ]; then
    if [ "x$CC" = "x" ]; then
        # At this point, CC is not set in Configuration or above, so we
        # try to find one
	for compilers in "gcc" "cc" "acc" "c89"
	do
	    lookedfor="$lookedfor $compilers"
	    if ./helpers/$AP_PRINTPATH -s $compilers; then
		COMPILER="$compilers"
		break
	    fi
	done
	if [ "x$COMPILER" = "x" ]; then
	    echo "Error: could not find any of these C compilers"
	    echo " anywhere in your PATH: $lookedfor"
	    echo "Configure terminated"
	    exitcode=1
	    exit 1
	fi
	CC=$COMPILER
    fi
    echo " + setting C compiler to $CC"
fi

####################################################################
## Write the value of $CC to Makefile.config... We only do this
## is not done already (ie: a 'CC=' line was in Configuration).
## If there was an entry for it, then set $CC for our own internal
## use.
##
if [ "x$TCC" = "x" ]; then
    echo "CC=$CC" >> Makefile.config
else
    CC=$TCC
fi

####################################################################
## Look for OPTIM and save for later
##
TOPTIM=`egrep '^OPTIM=' Makefile.config | tail -1 | awk -F= '{print $2}'`

####################################################################
## Check for user provided flags for shared object support
##
TLDFLAGS_SHLIB=`egrep '^LDFLAGS_SHLIB=' Makefile.config | tail -1 | awk -F= '{print $2}'`
TCFLAGS_SHLIB=`egrep '^CFLAGS_SHLIB=' Makefile.config | tail -1 | awk -F= '{print $2}'`

####################################################################
## We adjust now CFLAGS_SHLIB, LDFLAGS_SHLIB and LDFLAGS_SHLIB_EXPORT as
## required.  For more platforms just add the required lines below.
##
if [ "x$using_shlib" = "x1" ] ; then
    DEF_SHARED_CORE=no
    SHLIB_SUFFIX_DEPTH=all
    case "$PLAT" in
        *-linux1)
            CFLAGS_SHLIB="-fpic"
            LDFLAGS_SHLIB="-Bshareable"
            LDFLAGS_SHLIB_EXPORT="-rdynamic"
            ;;
        *-linux2)
            CFLAGS_SHLIB="-fpic"
            LDFLAGS_SHLIB="-Bshareable"
            LDFLAGS_SHLIB_EXPORT="-rdynamic"
            SHLIB_SUFFIX_DEPTH=0
            ;;
        *-freebsd*)
            CFLAGS_SHLIB="-fpic"
            LDFLAGS_SHLIB="-Bshareable"
            LDFLAGS_SHLIB_EXPORT=""
            SHLIB_SUFFIX_DEPTH=2
            ;;
        *-solaris2*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-G"
            LDFLAGS_SHLIB_EXPORT=""
            SHLIB_SUFFIX_DEPTH=1
            ;;
        *-sunos4*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */acc|acc ) CFLAGS_SHLIB="-pic" ;;
            esac
            LDFLAGS_SHLIB="-assert pure-text"
            LDFLAGS_SHLIB_EXPORT=""
            ;;
        *-sgi-irix32)
            case $CC in
                */gcc|gcc )
		    CFLAGS_SHLIB="-fpic"
		    N32FLAG=""
		    ;;
                */cc|cc )
		    CFLAGS_SHLIB="-KPIC"
		    N32FLAG="-n32"
		    ;;
            esac
            if [ "$RULE_IRIXN32" = "yes" ]; then
                LDFLAGS_SHLIB="$N32FLAG -shared"
            else
                LDFLAGS_SHLIB="-shared"
            fi
            LDFLAGS_SHLIB_EXPORT=""
            ;;
        *-sgi-irix)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-shared"
            LDFLAGS_SHLIB_EXPORT=""
            ;;
        *-dec-osf*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="" ;;
            esac
            LDFLAGS_SHLIB="-shared -expect_unresolved '*' -msym -s"
            LDFLAGS_SHLIB_EXPORT=""
            ;;
        *-unixware2)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-Bdynamic -G"
            LDFLAGS_SHLIB_EXPORT="-Wl,-Bexport"
            ;;
        *-unixware21*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-Bdynamic -G"
            LDFLAGS_SHLIB_EXPORT="-Wl,-Bexport"
            ;;
        RM*-sni-sysv4*)
            # MIPS hosts can take advantage of the LDFLAGS_SHLIB_EXPORT switch
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-G"
            LDFLAGS_SHLIB_EXPORT="-Wl,-Blargedynsym"
            ;;
        *-sni-sysv4*)
            # Older SINIX machines must be linked as "shared core"-Apache
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-G"
            LDFLAGS_SHLIB_EXPORT=""
            SHLIB_SUFFIX_DEPTH=0
            DEF_SHARED_CORE=yes
            ;;
        *-sysv4*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="-KPIC" ;;
            esac
            LDFLAGS_SHLIB="-G"
            LDFLAGS_SHLIB_EXPORT=""
            DEF_SHARED_CORE=yes
            ;;
        *-hp-hpux9.*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="+z" ;;
            esac
            LDFLAGS_SHLIB="-b"
            LDFLAGS_SHLIB_EXPORT="-Wl,-E -Wl,-B,deferred"
            ;;
        *-hp-hpux10.*)
            case $CC in
                */gcc|gcc ) CFLAGS_SHLIB="-fpic" ;;
                */cc|cc   ) CFLAGS_SHLIB="+z" ;;
            esac
            LDFLAGS_SHLIB="-b"
            LDFLAGS_SHLIB_EXPORT="-Wl,-E -Wl,-B,deferred"
            ;;
        *)
            ##  ok, no known explict support for shared objects
            ##  on this platform, but we give not up immediately.
            ##  We take a second chance by guessing the compiler
            ##  and linker flags from the Perl installation
            ##  if it exists.
            PERL=
            for dir in `echo $PATH | sed -e 's/:/ /g'`
            do
                if [ -f "$dir/perl5" ]; then
                    PERL="$dir/perl5"
                    break
                fi
                if [ -f "$dir/perl" ]; then
                    PERL="$dir/perl"
                    break
                fi
            done
            if [ ".$PERL" != . ]; then
                #   cool, Perl is installed on this platform...
                if [ ".`$PERL -V:dlsrc 2>/dev/null | grep dlopen`" != . ]; then
                    #   ...and actually uses the dlopen-style interface,
                    #   so we can guess the flags from its knowledge
                    CFLAGS_SHLIB="`perl -V:cccdlflags | cut -d\' -f2`"
                    LDFLAGS_SHLIB="`perl -V:lddlflags | cut -d\' -f2`"
                    LDFLAGS_SHLIB_EXPORT="`perl -V:ccdlflags | cut -d\' -f2`"
                    #   but additionally we have to inform the
                    #   user that we are just guessing the flags
                    echo ""
                    echo "** WARNING: We have no explicit knowledge about shared object"
                    echo "** support for your particular platform. But perhaps you have"
                    echo "** luck: We were able to guess the compiler and linker flags"
                    echo "** for creating shared objects from your Perl installation."
                    echo "** If they actually work, please send the following information"
                    echo "** for inclusion into later releases to apache@apache.org or"
                    echo "** make a suggestion report at http://bugs.apache.org/:"
                    echo "**     PLATFORM=$PLAT"
                    echo "**     CFLAGS_SHLIB=$CFLAGS_SHLIB"
                    echo "**     LDFLAGS_SHLIB=$LDFLAGS_SHLIB"
                    echo "**     LDFLAGS_SHLIB_EXPORT=$LDFLAGS_SHLIB_EXPORT"
                    echo ""
                fi
            fi
            ;;
    esac
fi

####################################################################
## Check if we really have some information to compile
## the shared objects if SharedModule was used.
##
if [ "x$using_shlib" = "x1" ] ; then
    if [ "x$TCFLAGS_SHLIB"  = x -a "x$CFLAGS_SHLIB"  = x  -a \
         "x$TLDFLAGS_SHLIB" = x -a "x$LDFLAGS_SHLIB" = x ]; then
        echo ""
        echo "** FAILURE: Sorry, no shared object support available."
        echo "** Either compile all modules statically (use AddModule instead"
        echo "** of SharedModule in the Configuration file) or at least provide"
        echo "** us with the appropriate compiler and linker flags via the"
        echo "** CFLAGS_SHLIB, LDFLAGS_SHLIB and LDFLAGS_SHLIB_EXPORT entries"
        echo "** in the Configuration file."
        echo ""
        exit 1
    fi
fi

####################################################################
## Set the value of the shared libary flags, if they aren't explicitly
## set in the configuration file
##
if [ "x$using_shlib" = "x1" ] ; then
    if [ "x$TCFLAGS_SHLIB" = "x" ]; then
        echo "CFLAGS_SHLIB=$CFLAGS_SHLIB -DSHARED_MODULE" >> Makefile.config
    fi
    if [ "x$TLDFLAGS_SHLIB" = "x" ]; then
        echo "LDFLAGS_SHLIB=$LDFLAGS_SHLIB" >> Makefile.config
    fi
    if [ "x$TLDFLAGS_SHLIB_EXPORT" = "x" ]; then
        echo "LDFLAGS_SHLIB_EXPORT=$LDFLAGS_SHLIB_EXPORT" >> Makefile.config
    fi
fi

####################################################################
## Now we do some OS specific adjustments... for some OSs, we need
## to adjust CFLAGS and/or OPTIM depending on which compiler we
## are going to use. This is easy, since this can be gleamed from
## Makefile.config
##
case "$OS" in
    'ULTRIX')
	case "$CC" in
	    */cc|cc ) CFLAGS="$CFLAGS -std" ;;
	esac
	;;
    'SCO 5')
	case "$CC" in
	    */cc|cc ) OSBPRINTF="-K noinline" ;;
	esac
	;;
    'HI-UX')
	case "$CC" in
	    */cc|cc )
		CFLAGS="$CFLAGS -Aa -D_HIUX_SOURCE"
		OPTIM=" "
		TOPTIM=""
	    ;;
	esac
	;;
    'HP-UX'|'HP-UX 10')
	case "$CC" in
	    */cc|cc )
		CFLAGS="$CFLAGS -Aa -D_HPUX_SOURCE"
		OPTIM=" "
		TOPTIM=""
	    ;;
	esac
	;;
    *IRIX-64*)
	case "$CC" in
	    */cc|cc )
		CFLAGS="$CFLAGS -n32"
		LDFLAGS="$LDFLAGS -n32"
	    ;;
	esac
	;;
    *IRIX-32*)
	case "$CC" in
	    */cc|cc )
		CFLAGS="$CFLAGS -n32"
		LDFLAGS="$LDFLAGS -n32"
	    ;;
	esac
	;;
esac

####################################################################
## OK, now we can write OPTIM
##
if [ "x$TOPTIM" = "x" ]; then
    echo "OPTIM=$OPTIM" >> Makefile.config
fi

####################################################################
## Now we do some general checks and some intelligent Configuration
## control.

# Use TestCompile to look for various LIBS
case "$PLAT" in
    *-linux*)
	# newer systems using glibc 2.x need -lcrypt
	if ./helpers/TestCompile lib crypt; then
	    LIBS="$LIBS -lcrypt"
	fi
	;;

    *-dg-dgux*)
	# R4.11MU02 requires -lsocket -lnsl ... no idea if it's earlier or
	# later than what we already knew about.  PR#732
	if ./helpers/TestCompile lib socket; then
	    LIBS="$LIBS -lsocket"
	fi
	if ./helpers/TestCompile lib nsl; then
	    LIBS="$LIBS -lnsl"
	fi
	;;
esac

# Now SOCKS4.
# NOTE: We assume that if they are using SOCKS4, then they've
#       adjusted EXTRA_LIBS and/or EXTRA_LDFLAGS as required,
#       otherwise we assume "-L/usr/local/lib -lsocks"
if [ "$RULE_SOCKS4" = "yes" ]; then
    # Set flag and check Makefile for -lsocks line
    CFLAGS="$CFLAGS -Dconnect=Rconnect -Dselect=Rselect"
    CFLAGS="$CFLAGS -Dgethostbyname=Rgethostbyname"
    if grep "EXTRA_" Makefile | grep "\-lsocks" > /dev/null; then : ;
    else
	LIBS="$LIBS -L/usr/local/lib -lsocks"
    fi
    if [ "$OS" = "Solaris 2" ]; then
	LIBS="$LIBS -lresolv"
    fi
fi

####################################################################
## Find out what modules we want and try and configure things for them
## Module lines can look like this:
##
##  Module  name_module    some/path/mod_name[.[oa]]
##  AddModule              some/path/mod_name[.[oa]]
##
## In both cases, the some/path can either be an arbitrary path (including
## an absolute path), or a path like "modules/DIR", in which case we _might_
## auto-generate a Makefile in modules/DIR (see later).
##
## The first case is the original style, where we give the module's
## name as well as it's binary file location - either a .o or .a.
##
## The second format is new, and means we do not repeat the module
## name, which is already part of the module source or definition.
## The way we find the module name (and other optional information about
## the module) is like this:
##
##  1 If extension is not given or is .c, assume .o was given and goto 3
##  2 If extension if .module, go to D1
##  3 If extension is .o, look for a corresponding .c file and if
##      found, go to C1
##  4 If no .c file was found, look for a .module file (Apache module
##      definition file). If found, go to D1
##  5 Assume module name is the "name" part of "mod_name", as in
##      name_module.
##
## If a C file is found:
##
## C1 Look for module name given by an MODULE: line (e.g. MODULE: name_module)
##      If found assume module contains a definition, and go to D1
## C2 If not found, look for a module name given on the declaration of the
##      module structure (e.g. module name_module).
## C3 If neither given, go to 4 above.
##
## If a definition file is found, or a .c file includes a module definition:
##
## D1 Get the module name from the MODULE: name= line
## D2 Get other module options (libraries etc). To be done later.
##
##
## For now, we will convert the AddModule lines into Module format
## lines, so the rest of Configure can do its stuff without too much
## additional hackery. It would be nice to reduce the number of times
## we have to awk the $tmpfile, though.

## MODFILES contains a list of module filenames (could be .c, .o, .so, .a
##    or .module files) from AddModule lines only
## MODDIRS contains a list of subdirectories under 'modules' which
##    contain modules we want to build from both AddModule and Module
##    lines

echo " + adding selected modules"

MODFILES=`awk <$tmpfile '($1 == "AddModule" || $1 == "SharedModule") { printf "%s ", $2 }'`
MODDIRS=`awk < $tmpfile '
	($1 == "Module" && $3 ~ /^modules\//) {
	    split ($3, pp, "/")
	    if (! SEEN[pp[2]]) {
		printf "%s ", pp[2]
		SEEN[pp[2]] = 1
	    }
    	}
	(($1 == "AddModule" || $1 == "SharedModule") && $2 ~ /^modules\//) { 
	    split ($2, pp, "/")
	    if (! SEEN[pp[2]]) {
		printf "%s ", pp[2]
		SEEN[pp[2]] = 1
	    } 
    	}'`
MODDIRS_NO_SO=`awk < $tmpfile '
	($1 == "Module" && $3 ~ /^modules\//) {
	    split ($3, pp, "/")
	    if (! SEEN[pp[2]]) {
		printf "%s ", pp[2]
		SEEN[pp[2]] = 1
	    }
    	}
	(($1 == "AddModule") && $2 ~ /^modules\//) { 
	    split ($2, pp, "/")
	    if (! SEEN[pp[2]]) {
		printf "%s ", pp[2]
		SEEN[pp[2]] = 1
	    } 
    	}'`

# Now autoconfigure each of the modules specified by AddModule.
# Use tmpfile2 for the module definition file, and tmpfile3 for the
# shell commands to be executed for this module.

for modfile in $MODFILES ; do
	rm -f $tmpfile2 $tmpfile3
	modname=''

	ext=`echo $modfile | sed 's/^.*\.//'`
	modbase=`echo $modfile | sed 's/\.[^.]*$//'`
	if [ x$ext = x$modfile ]; then ext=o; modbase=$modfile; modfile=$modbase.o; fi
	if [ x$ext = x ] ; then ext=o; modbase=$modfile; fi
	if [ x$ext = xc ] ; then ext=o; fi

	# modbase is the path+filename without extension, ext is the
	# extension given, or if none, o
	if [ -r $modbase.module ] ; then
		$CAT $modbase.module > $tmpfile2
	else
	    if [ -f $modbase.c ] ; then
		# Guess module structure name in case there is not
		# module definition in this file
		modname=`egrep '^module .*;' $modbase.c | head -1 |\
			sed 's/^module.*[ 	][ 	]*//' | \
			sed 's/[ 	]*;[ 	]*$//'`
		# Get any module definition part
		if grep "MODULE-DEFINITION-" $modbase.c > /dev/null; then
		$CAT $modbase.c | \
		sed '1,/MODULE-DEFINITION-START/d;/MODULE-DEFINITION-END/,$d' \
			> $tmpfile2
		fi
	    fi
	fi		
	if [ -r $tmpfile2 ] ; then
		# Read a module definition from .module or .c
		modname=`grep "Name:" $tmpfile2 | sed 's/^.*Name:[ 	]*//'`
		if grep "ConfigStart" $tmpfile2 > /dev/null \
		 && grep "ConfigEnd" $tmpfile2 > /dev/null; then
		    sed '1,/ConfigStart/d;/ConfigEnd/,$d' $tmpfile2 > \
		     $tmpfile3
		    echo "    o $modname uses ConfigStart/End"
		    if [ "$RULE_PARANOID" = "yes" ]; then
			sed 's/^/>> /' $tmpfile3
		    fi
		    . ./$tmpfile3
		fi
		rm -f $tmpfile2 $tmpfile3
		if [ $ext != so ]; then
		    ext=o
		fi
	fi
	if [ "x$modname" = "x" ] ; then
		modname=`echo $modbase | sed 's/^.*\///' | \
			sed 's/^mod_//' | sed 's/^lib//' | sed 's/$/_module/'`
	fi
	if [ $ext != so ]; then
		echo "Module $modname $modbase.$ext" >>$tmpfile
	fi
done
# $tmpfile now contains Module lines for all the modules we want

####################################################################
## Now HS's POSIX regex implementation if needed/wanted. We do it
## now since AddModule may have changed it
##
if [ "$RULE_WANTHSREGEX" = "yes" ]; then
    REGLIB="regex/libregex.a"
    SUBDIRS="regex $SUBDIRS"
    CFLAGS="$CFLAGS -DUSE_HSREGEX"
fi

####################################################################
## Now the SHARED_CORE stuff
##
if [ "x$using_shlib" = "x1" ] ; then
    if [ ".$RULE_SHARED_CORE" = .default ] ; then
        RULE_SHARED_CORE=$DEF_SHARED_CORE
    fi
    if [ ".$RULE_SHARED_CORE" = .yes ]; then
    echo " + enabling generation of Apache core as DSO"
        #    shuffle compiler flags from shlib variant to standard
        CFLAGS="$CFLAGS $CFLAGS_SHLIB"
        CFLAGS_SHLIB=""
        #    indicate that Rule SHARED_CORE is active
        CFLAGS="$CFLAGS -DSHARED_CORE"
        #    select the special subtarget for shared core generation
        SUBTARGET=target_shared
        #    determine additional suffixes for libhttpd.so
        V=1 R=3 P=0
        if [ ".$SHLIB_SUFFIX_DEPTH" = .0 ]; then
            SHLIB_SUFFIX_LIST=""
        fi
        if [ ".$SHLIB_SUFFIX_DEPTH" = .1 ]; then
            SHLIB_SUFFIX_LIST="$V"
        fi
        if [ ".$SHLIB_SUFFIX_DEPTH" = .2 ]; then
            SHLIB_SUFFIX_LIST="$V.$R"
        fi
        if [ ".$SHLIB_SUFFIX_DEPTH" = .3 ]; then
            SHLIB_SUFFIX_LIST="$V.$R.$P"
        fi
        if [ ".$SHLIB_SUFFIX_DEPTH" = .all ]; then
            SHLIB_SUFFIX_LIST="$V $V.$R $V.$R.$P"
        fi
    fi
fi

####################################################################
## Now create modules.c
##
sed 's/_module//' $tmpfile | awk >modules.c '
    BEGIN {
	modules[n++] = "core"
	pmodules[pn++] = "core"
    } 
    /^Module/ { modules[n++] = $2 ; pmodules[pn++] = $2 } 
    /^%Module/ { pmodules[pn++] = $2 } 
    END {
	print "/* modules.c --- automatically generated by Apache"
	print " * configuration script.  DO NOT HAND EDIT!!!!!"
	print " */"
	print ""
	print "#include \"httpd.h\""
	print "#include \"http_config.h\""
	print ""
	for (i = 0; i < pn; ++i) {
	    printf ("extern module %s_module;\n", pmodules[i])
	}
	print ""
	print "module *ap_prelinked_modules[] = {"
	for (i = 0; i < n; ++i) {
	    printf "  &%s_module,\n", modules[i]
	}
	print "  NULL"
	print "};"
	print ""
	print "module *ap_preloaded_modules[] = {"
	for (i = 0; i < pn; ++i) {
	    printf "  &%s_module,\n", pmodules[i]
	}
	print "  NULL"
	print "};"
	print ""
    }'

####################################################################
## figure out which module dir require use to autocreate a Makefile.
## for these dirs we must not list the object files from the AddModule
## lines individually since the auto-generated Makefile will create
## a library called libMODDIR.a for it (MODDIR is the module dir
## name). We create two variable here:
##
##   AUTODIRS   Space separated list of module directories, relative to
##              src
##   AUTOLIBS   Space separated list of auto-generated library files
##
for moddir in $MODDIRS 
do
	if [ -f modules/$moddir/Makefile.tmpl ] ; then
		AUTODIRS="$AUTODIRS modules/$moddir"
	fi
done
for moddir in $MODDIRS_NO_SO
do
	if [ -f modules/$moddir/Makefile.tmpl ] ; then
		AUTOLIBS="$AUTOLIBS modules/$moddir/lib$moddir.a"
	fi
done

####################################################################
## Add the module targets to the Makefile. Do not add individual object
## targets for auto-generated directories.
##
$CAT > $awkfile <<EOF1
    BEGIN {
	split ("$AUTODIRS", tmp, " ")
EOF1
$CAT >> $awkfile <<'EOF2'
	for ( key in tmp ) {
	    autodirs[tmp[key]] = 1
	}
     }
    /^Module/ { modules[n++] = $3 }
    /^%Module/ { modules[n++] = $3 }
    END {
	print "MODULES= \\"
	for (i = 0; i < n; ++i) {
	    split (modules[i], pp, "/")
	    dir = pp[1] "/" pp[2] 
	    inthere = 0
	    for ( tdir in autodirs ) {
	        if (tdir == dir) 
		    inthere = 1
	    }
	    if (inthere == 1)
		continue
	    else
		printf ("  %s \\\n", modules[i])
	}
    }
EOF2
awk -f $awkfile >>Makefile <$tmpfile

####################################################################
## Now add the auto-generated library targets.  Need to use awk so we
## don't hang a continuation on the last line.
##
$CAT > $awkfile <<EOF3
    BEGIN {
	split ("$AUTOLIBS", libs)
EOF3
$CAT >> $awkfile <<'EOF4'
	z = 0
	for ( lib in libs ) {
	    if (z != 0)
		printf (" \\\n")
	    z++
	    printf ("  %s", libs[lib])
	}
    }
    END {
	printf ("\n")
    }
EOF4
awk -f $awkfile >>Makefile </dev/null
echo "" >>Makefile

####################################################################
## Now add the target for the main Makefile
##
echo "TARGET=$TARGET" >> Makefile
echo "SUBTARGET=$SUBTARGET" >> Makefile
echo "SHLIB_SUFFIX_LIST=$SHLIB_SUFFIX_LIST" >> Makefile
echo "" >> Makefile

####################################################################
## Now add -DSERVER_SUBVERSION if $SUBVERSION is set
##
if [ "x$SUBVERSION" != "x" ] ; then
        SUBVERSION=`echo $SUBVERSION | sed 's/^ +//'`
	CFLAGS="$CFLAGS -DSERVER_SUBVERSION=\\\"$SUBVERSION\\\""
fi

####################################################################
## Determine GNU Make variant because
## it uses ugly looking built-in directory walk messages
## while we are already using our own messages
##
if [ ".`make -v 2>/dev/null | grep 'GNU Make'`" = . ]; then
	MFLAGS_STATIC=
else
	MFLAGS_STATIC=--no-print-directory
fi

####################################################################
## Continue building Makefile.config.
##
echo "CFLAGS1=$CFLAGS">> Makefile.config
echo "INCDIR=\$(SRCDIR)/include" >>Makefile.config
echo "INCLUDES0=-I\$(SRCDIR)/$OSDIR -I\$(SRCDIR)/include">> Makefile.config
echo "INCLUDES1=$INCLUDES">> Makefile.config
echo "LIBS1=$LIBS">> Makefile.config
echo "LDFLAGS1=$LDFLAGS">> Makefile.config
echo "BROKEN_BPRINTF_FLAGS=$OSBPRINTF">> Makefile.config
echo "MFLAGS_STATIC=$MFLAGS_STATIC">> Makefile.config
echo "REGLIB=$REGLIB">> Makefile.config
echo "RANLIB=$RANLIB">> Makefile.config
echo "SHELL=$SHELL">> Makefile.config
echo "OSDIR=$OSDIR">> Makefile.config
echo "SUBDIRS=$SUBDIRS">> Makefile.config
echo "##" >> Makefile.config
echo "##  (End of automatically generated section)">> Makefile.config
echo "##" >> Makefile.config
echo "" >> Makefile.config

####################################################################
## Continue building include/ap_config.h
##
## We pick out all -D's from CFLAGS and create ap_config.h which
## can be used by external modules needing to include Apache
## header files.
##
for cflag in $CFLAGS; do
	echo $cflag | sed 's/\\\"/\"/g' >>$tmpconfig ;
done
awk > include/ap_config.h < $tmpconfig '
	BEGIN {
		printf "/* Automatically generated file - do not edit */\n\n"
	}
	/^-D.*/ {
		split(substr($1,3,length($1)),parts,"=")
		printf ("#ifndef %s\n#define %s %s\n#endif\n", parts[1],parts[1],parts[2])
	}
'

####################################################################
## Use TestCompile to see if $(CC) is ANSI and as a "final" sanity
## check
##
echo " + doing sanity check on compiler and options"
if ./helpers/TestCompile sanity; then
    :
else
   echo "** A test compilation with your Makefile configuration"
   echo "** failed. This is most likely because your C compiler"
   echo "** is not ANSI. Apache requires an ANSI C Compiler, such"
   echo "** as gcc. The above error message from your compiler"
   echo "** will also provide a clue."
   echo " Aborting!"
   exitcode=1
   exit 1
fi

####################################################################
## Now (finish) creating the makefiles
##

# ./Makefile
$CAT Makefile.config >> Makefile
sed -e "s#@@Configuration@@#$file#" "Makefile.tmpl" >>Makefile

# xxx/Makefile
MAKEDIRS="support main ap regex $OSDIR"
for dir in $MAKEDIRS ; do
	echo Creating Makefile in $dir
	./helpers/mfhead $dir $file > $dir/Makefile
	$CAT Makefile.config $dir/Makefile.tmpl |\
	sed -e "s:^SRCDIR=.*:SRCDIR=`./helpers/fp2rp $dir`:" >> $dir/Makefile
done

####################################################################
## Now create the modules/Makefile
##
./helpers/mfhead modules $file > modules/Makefile
$CAT Makefile.config | sed -e 's:^SRCDIR=.*:SRCDIR=..:' >> modules/Makefile

$CAT << EOF >> modules/Makefile
MODULES=$MODDIRS
CFLAGS=\$(OPTIM) \$(CFLAGS1) \$(EXTRA_CFLAGS)

default: all

all clean distclean depend :: 
	@for i in \$(MODULES); do \\
	    echo "===> \$(SDP)modules/\$\$i"; \\
		(cd \$\$i && \$(MAKE) \$(MFLAGS_STATIC) SDP='\$(SDP)' CC='\$(CC)' AUX_CFLAGS='\$(CFLAGS)' RANLIB='\$(RANLIB)' \$@) || exit 1; \\
		echo "<=== \$(SDP)modules/\$\$i"; \\
	done

EOF

####################################################################
## Now create modules/xxx/Makefile
##
for moddir in $AUTODIRS ; do
	echo "Creating Makefile in $moddir"

    ./helpers/mfhead $moddir $file > $moddir/Makefile
	$CAT Makefile.config |\
	sed -e "s:^SRCDIR=.*:SRCDIR=`./helpers/fp2rp $moddir`:" >> $moddir/Makefile
	$CAT << 'EOF' >> $moddir/Makefile
##
##  Default Makefile options from Configure script
##  (Begin of automatically generated section)
##
CFLAGS=$(OPTIM) $(CFLAGS1) $(EXTRA_CFLAGS)
LIBS=$(EXTRA_LIBS) $(LIBS1)
INCLUDES=$(INCLUDES1) $(INCLUDES0) $(EXTRA_INCLUDES)
LDFLAGS=$(LDFLAGS1) $(EXTRA_LDFLAGS)
INCDIR=$(SRCDIR)/include
EOF
	if [ -f $moddir/Makefile.libdir ]; then
	    basedir=`echo $moddir | sed 's@^[^/]*/@@g'`
	    awk >> $moddir/Makefile < $tmpfile '
		($2 ~ /^modules\/'$basedir'\//) {
		    split($2, pp, "/");
		    split(pp[3], parts, ".");
		    libext=parts[2];
		}
		END { 
		    printf "LIBEXT=%s\n", libext;
		}'
	    # it's responsible for the rest of its Makefile...
	else
	    basedir=`echo $moddir | sed 's@^[^/]*/@@g'`
	    OBJS=`awk < $tmpfile '
		($1 == "Module" && $3 ~ /^modules\/'$basedir'\//) { 
		    split ($3, pp, "/")
		    printf "%s ", pp[3] 
		} 
		'`
	    echo "OBJS=$OBJS" >> $moddir/Makefile
	    if [ ".$OBJS" != . ]; then
		echo "LIB=lib$basedir.a" >> $moddir/Makefile
	    else
		#   essential!
		echo "LIB=" >> $moddir/Makefile
	    fi
	    awk >> $moddir/Makefile < $tmpfile '
	    ($1 == "SharedModule" && $2 ~ /^modules\/'$basedir'\//) {
		split($2, pp, "/")
		shlibs=shlibs " " pp[3]
		so=pp[3]
		split(pp[3], parts, ".")
		base=parts[1]
		shlibsobj=shlibsobj " " base "-so.o"
	    }
	    END { 
		printf "SHLIBS=%s\n", shlibs;
		printf "SHLIBS_OBJ=%s\n", shlibsobj;
            }'

	    $CAT << 'EOF' >> $moddir/Makefile

all: lib shlib

lib:	$(LIB) 

shlib:	$(SHLIBS)

$(LIB): $(OBJS)
	rm -f $@
	ar cr $@ $(OBJS)
	$(RANLIB) $@

.SUFFIXES: .o .so

.c.o:
	$(CC) -c $(INCLUDES) $(CFLAGS) $(SPACER) $<

.c.so:
	$(CC) -c $(INCLUDES) $(CFLAGS) $(CFLAGS_SHLIB) $(SPACER) $< && mv $*.o $*-so.o
	$(LD) $(LDFLAGS_SHLIB) -o $@ $*-so.o

clean:
	rm -f $(OBJS) $(SHLIBS) $(SHLIBS_OBJ) $(LIB) $(SHLIB)

distclean: clean
	rm -f Makefile

$(OBJS) $(SHLIBS) $(SHLIBS_OBJ): Makefile

EOF
	fi

	$CAT << 'EOF' >> $moddir/Makefile
##
##  (End of automatically generated section)
##
EOF
    $CAT >> $moddir/Makefile < $moddir/Makefile.tmpl

done

