#!/bin/sh
trap 'rm -f $tmpfile; exit' 0 1 2 3 15

# Apache configuration script, first cut --- rst.
# Dont 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
#
# Uses 3 supplemental scripts located in ./helpers: CutRule,
# GuessOS and PrintPath
#

file=Configuration
tmpfile=htconf.$$
makefile_tmpl=Makefile.tmpl

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."
      exit 1
    fi
  elif [ "x$1" = "x-make" ] ; then
    shift 1; makefile_tmpl=$1; shift 1
    if [ ! -r $makefile_tmpl ]; then
      echo "$makefile_tmpl does not exist or is not readable."
      exit 1
    fi
  else
    echo "Ignoring command line option '$1'"
    shift 1
  fi
done
echo "Using config file: $file"
echo "Using Makefile template file: $makefile_tmpl"

if [ ! -r $file ]; then
  echo "Can't see or read \"$file\""
  exit 1
fi

# First, strip comments and blank lines and then change Rules to comments
# and then remove whitespace before Module declarations

sed 's/#.*//' $file | \
 sed '/^[ 	]*$/d' | \
 sed 's/[ 	]*$//' | \
 sed 's/^Rule[ 	]*/##Rule:/' | \
 sed 's/^[ 	]*Module/Module/' | \
 sed 's/^[        ]*%Module/%Module/' > $tmpfile

# Check for syntax errors...

if egrep -v '^%?Module[ 	]+[A-Za-z0-9_]+[ 	]+[^ 	]+$' $tmpfile \
   | 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 | \
     grep -v =
   exit 1
fi

# File is OK --- make backup copies of things and then get the new ones:

if [ -f Makefile ] ; then mv Makefile Makefile.bak; fi
if [ -f modules.c ] ; then mv modules.c modules.c.bak; fi

sed -e '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 *prelinked_modules[] = {"; \
	 for (i = 0; i < n; ++i) { \
	     printf "  &%s_module,\n", modules[i]; \
	 } \
	 print "  NULL"; \
	 print "};"; \
	 print "module *preloaded_modules[] = {"; \
	 for (i = 0; i < pn; ++i) { \
	     printf "  &%s_module,\n", pmodules[i]; \
	 } \
	 print "  NULL"; \
	 print "};"; \
   }'

#
# Add module set only
#
echo "#" > Makefile
echo "# Makefile automatically generated from $makefile_tmpl" >> Makefile
echo "# and configuration file by Apache config script." >> Makefile
echo "# Hand-edited changes will be lost if the config script" >> Makefile
echo "# is re-run" >> Makefile
echo "#" >> Makefile

awk >>Makefile <$tmpfile '\
   /^Module/ { modules[n++] = $3 } \
   /^%Module/ { modules[n++] = $3 } \
   END { print "MODULES=\\"; \
	 for (i = 0; i < n; ++i) { \
	     if (i < n-1) printf ("  %s \\\n", modules[i]); \
	     else printf ("  %s\n", modules[i]); \
	 } \
	 print "" \
       }'
#
# Now add Makefile additions and Rules
#
awk >>Makefile <$tmpfile '\
    BEGIN { print "# Makefile options inherited from Configure"; \
	    print "###############"; \
	  } \
    /\=/ { print } \
    END { 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`

#
# Now we determine the OS/Platform automagically, thanks to
# GuessOS, a home-brewed OS-determiner ala config.guess
#
# We adjust CFLAGS, LIBS, LFLAGS 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`

# Preset DBM_LIB. Can be overridden on a per-platform basis.

DBM_LIB="-ldbm"

#
# Look for ranlib. Do it early in case we want to override it below
#
if ./helpers/PrintPath -s ranlib; then
    RANLIB="ranlib"
else
    RANLIB="true"
fi

#
# We now look for popular compilers. As with ranlib, we
# do this early because some options may depend
# on which compiler we use/find
#
for compilers in "gcc" "cc" "acc" "c89"
do
    lookedfor="$lookedfor $compilers"
    if ./helpers/PrintPath -s $compilers; then
	COMPILER="$compilers"
    	break
    fi
done

#
SHELL="/bin/sh"

case "$PLAT" in
    *MPE/iX*)
        OS='MPE/iX'
	CFLAGS="$CFLAGS -DMPE -D_POSIX_SOURCE -D_SOCKET_SOURCE"
	LIBS="$LIBS -lsocket"
	LFLAGS="$LFLAGS -Xlinker \"-WL,cap=ia,ba,ph,pm;nmstack=1024000\""
	;;
    *-apple-aux3*)
	OS='A/UX 3.1.x'
	CFLAGS="$CFLAGS -DAUX -D_POSIX_SOURCE"
	LIBS="$LIBS -lposix -lbsd"
	LFLAGS="$LFLAGS -s"
	DEF_WANTHSREGEX=no
	;;
    i386-ibm-aix*)
    	OS='IBM AIX PS/2'
	CFLAGS="$CFLAGS -DAIX -U__STR__ -DUSEBCOPY"
	DEF_WANTHSREGEX=no
	;;
    *-ibm-aix*)
	OS='IBM AIX'
	CFLAGS="$CFLAGS -DAIX -U__STR__"
	;;
    *-apollo-*)
	OS='Apollo Domain'
	CFLAGS="$CFLAGS -DAPOLLO"
	;;
    *-dg-dgux*)
	OS='DG/UX 5.4'
	CFLAGS="$CFLAGS -DDGUX"
	DEF_WANTHSREGEX=yes
	;;
    *OS/2*)
	DEF_WANTHSREGEX=yes
	OS='EMX OS/2'
	CFLAGS="$CFLAGS -Zbsd-signals -Zbin-files -DTCPIPV4 -g"
	LIBS="$LIBS -lsocket -llibufc -lbsd"
	DBM_LIB="-lgdbm"
	;;
    *-hi-hiux)
	OS='HI-UX'
	CFLAGS="$CFLAGS -DHIUX"
	# if we're using the HIUX compiler, add a few flags.
	if [ "$CC" = "cc" ] || [ "$COMPILER" = "cc" ]; then
	    CFLAGS="$CFLAGS -Aa -D_HIUX_SOURCE"
	    OPTIM=" "
	fi
	;;
    *-hp-hpux10.*)
	OS='HP-UX 10'
	CFLAGS="$CFLAGS -DHPUX10"
	# if we're using the HPUX compiler, add a few flags.
	if [ "$CC" = "cc" ] || [ "$COMPILER" = "cc" ]; then
	    CFLAGS="$CFLAGS -Aa -D_HPUX_SOURCE"
	    OPTIM=" "
	fi
	;;
    *-hp-hpux*)
	OS='HP-UX'
	CFLAGS="$CFLAGS -DHPUX"
	if [ "$CC" = "cc" ] || [ "$COMPILER" = "cc" ]; then
	    CFLAGS="$CFLAGS -Aa -D_HPUX_SOURCE"
	    OPTIM=" "
	fi
	;;
    *-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."
	CFLAGS="$CFLAGS -n32"
	LFLAGS="$LFLAGS -n32"
	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
	;;
    *-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"
	;;
    *-linux1)
	DEF_WANTHSREGEX=yes
	OS='Linux'
	CFLAGS="$CFLAGS -DLINUX=1"
	;;
    *-lynx-lynxos*)
	OS='LynxOS'
	CFLAGS="$CFLAGS -DLYNXOS"
	LIBS="$LIBS -lbsd -ldes -lc_p"
	;;
    *486-*-bsdi*)
	OS='BSDI w/486'
	CFLAGS="$CFLAGS -m486"
	DBM_LIB=""
	;;
    *-bsdi*)
	OS='BSDI'
	DBM_LIB=""
	;;
    *486-*-freebsd*|*486-*-netbsd*)
	OS='FreeBSD/NETBSD on 486'
	LIBS="$LIBS -lcrypt"
	DBM_LIB=""
	;;
    *-freebsd*|*-netbsd*)
	OS='FreeBSD/NetBSD'
	LIBS="$LIBS -lcrypt"
	DBM_LIB=""
	;;
    *-openbsd*)
	OS='OpenBSD'
	;;
    *-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"
	DEF_WANTHSREGEX=yes
	;;
    *-qnx32)
	OS='QNX32'
	CFLAGS="$CFLAGS -DQNX -mf -3"
	LIBS="$LIBS -N128k -lsocket"
	DEF_WANTHSREGEX=yes
	;;
    *-isc4*)
	OS='ISC 4'
	CC='gcc'
	CFLAGS="$CFLAGS -posix -DISC"
	LFLAGS="$LFLAGS -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"
	OSBPRINTF="-K noinline"
	DEF_WANTHSREGEX=no
	;;
    *-solaris2*)
	OS='Solaris 2'
	CFLAGS="$CFLAGS -DSOLARIS2"
	LIBS="$LIBS -lsocket -lnsl"
	DBM_LIB=""
	DEF_WANTHSREGEX=yes
	;;
    *-sunos4*)
	OS='SunOS 4'
	CFLAGS="$CFLAGS -DSUNOS4 -DUSEBCOPY"
	DEF_WANTHSREGEX=yes
	;;
    *-unixware1)
	DEF_WANTHSREGEX=yes
	OS='Unixware'
	CFLAGS="$CFLAGS -DSVR4 -DNO_LINGCLOSE"
	LIBS="$LIBS -lsocket -lnsl -lcrypt"
	;;
    *-unixware2)
	DEF_WANTHSREGEX=yes
	OS='Unixware'
	CFLAGS="$CFLAGS -DSVR4 -DNO_LINGCLOSE"
	LIBS="$LIBS -lsocket -lnsl -lcrypt"
	;;
    *-unixware211)
	OS='Unixware 2.1.1'
	CFLAGS="$CFLAGS -DUW"
	LIBS="$LIBS -lsocket -lnsl -lcrypt"
	;;
    *-sni-sysv4*)
	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4"
	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
	;;
    *-sysv4*)
	OS='SVR4'
	CFLAGS="$CFLAGS -DSVR4"
	LIBS="$LIBS -lsocket -lnsl -lc"
	;;
    *-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'
	LFLAGS="$LFLAGS -Xlstack=0x14000 -Xldelcsect"
	;;
    *-machten*)
	OS='MachTen 68K'
	LFLAGS="$LFLAGS -stack 0x14000"
	DEF_WANTHSREGEX=yes
	;;
    *convex-v11*)
	OS='CONVEXOS11'
	CFLAGS="$CFLAGS -DCONVEXOS11"
	CC='cc'
	DEF_WANTHSREGEX=yes
	;;
    i860-intel-osf1)
	DEF_WANTHSREGEX=yes
	OS='Paragon OSF/1'
	CFLAGS="$CFLAGS -DPARAGON"
	;;
    *) # 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://www.apache.org/bugdb.cgi - or, if your browser
	echo isn\'t 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
	exit 1
	;;
esac

#
# See if we need to override WANTHSREGEX
#
if [ "$RULE_WANTHSREGEX" = "default" ]; then
	if [ "x$DEF_WANTHSREGEX" = "x" ]; then
		RULE_WANTHSREGEX=no
	else
		RULE_WANTHSREGEX=$DEF_WANTHSREGEX
	fi
fi

# Show the final values of the rules

echo "###############" > Makefile.config
echo "# Platform: $OS" >> Makefile.config
echo "# Final Rules:" >> Makefile.config
echo "#  Rule WANTHSREGEX=$RULE_WANTHSREGEX" >> Makefile.config
echo "###############" >> Makefile.config

#
# Now that _that's_ done, get on with it
#

echo " + configured for $OS platform"

#
# 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 use the "found" location of COMPILERS above
# and set a "safe" optimization level
#

if egrep "^CC[ 	]*=" Makefile > /dev/null; then
    CC=""	# clear it just in case
else
    if [ "x$CC" = "x" ]; then
    	if [ "x$COMPILER" = "x" ]; then
	    echo "Error: could not find any of these C compilers"
	    echo " anywhere in your PATH: $lookedfor"
	    echo "Configure terminated"
	    exit 1
	fi
	CC=$COMPILER
    fi
    echo " + setting C compiler to $CC"
fi

#
# Ditto for optimization
#
if  egrep "^OPTIM[ 	]*=" Makefile > /dev/null; then
    OPTIM=""	# ditto
else
    if [ "x$OPTIM" = "x" ]; then
	OPTIM="-O2"
    fi
    echo " + setting C compiler optimization-level to $OPTIM"
fi

#
# Are they using the status monitor module? If so, check
# for STATUS rule...
#
STAT_MOD="mod_status"
if grep "$STAT_MOD" Makefile > /dev/null; then
    if [ "$RULE_STATUS" = "yes" ]; then
	CFLAGS="$CFLAGS -DSTATUS"
    fi
fi

#
# Are they using dbm auth? If so, add DBM library.
#
if grep mod_auth_dbm Makefile > /dev/null; then
    LIBS="$LIBS $DBM_LIB"
fi

#
# Now HS's POSIX regex implementation if needed/wanted
#
if [ "$RULE_WANTHSREGEX" = "yes" ]; then
    REGLIB="regex/libregex.a"
    INCLUDES="$INCLUDES -Iregex"
fi

#
# Now SOCKS4.
#  NOTE: We assume that if they are using SOCKS4, then they've
#   adjusted EXTRA_LIBS and/or EXTRA_LFLAGS 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 [ "$OS" = "Solaris 2" ]; then
	LIBS="$LIBS -lresolv"
    fi
    if grep "EXTRA_" Makefile | grep "\-lsocks" > /dev/null; then : ;
    else
	LIBS="$LIBS -L/usr/local/lib -lsocks"
    fi
fi

#
# Good enough
#
echo  >> Makefile
if [ "x$CC" != "x" ]; then
    echo "CC=$CC" >> Makefile.config
fi
if [ "x$OPTIM" != "x" ]; then
    echo "OPTIM=$OPTIM" >> Makefile.config
fi
echo "CFLAGS1=$CFLAGS">> Makefile.config
echo "INCLUDES1=$INCLUDES">> Makefile.config
echo "LIBS1=$LIBS">> Makefile.config
echo "LFLAGS1=$LFLAGS">> Makefile.config
echo "BROKEN_BPRINTF_FLAGS=$OSBPRINTF">> Makefile.config
echo "REGLIB=$REGLIB">> Makefile.config
echo "RANLIB=$RANLIB">> Makefile.config
echo "SHELL=$SHELL">> Makefile.config
echo >> Makefile.config
echo "#### End of Configure created section ####">> Makefile.config


# Now (finish) creating the makefiles
cat Makefile.config >> Makefile
sed -e "s#@@Configuration@@#$file#" "$makefile_tmpl" >>Makefile
awk >>Makefile <$tmpfile \
   '($1 == "Module" && $3 ~ /modules\//) { printf "%s: modules/last-built ; @cat /dev/null\n\n", $3, $3}'
cat Makefile.config ../support/Makefile.tmpl > ../support/Makefile

cat << EOF > modules/Makefile
# 
# Simple Makefile for modules in src/modules.
# Generated by src/Configure according to rules in src/Configuration;
# hand-edit at your own risk!
# 

SHELL=$SHELL
EOF

if [ "$RULE_WANTHSREGEX" = "yes" ]; then
    INCLUDES2="-I../../regex"
fi

echo "INCLUDES2=$INCLUDES2">> modules/Makefile
echo "MOD_CFLAGS=\$(INCLUDES2) \$(AUX_CFLAGS)">> modules/Makefile

awk >> modules/Makefile < $tmpfile '\
   BEGIN {printf "MODULES="} \
   ($1 == "Module" && $3 ~ /modules\//) {split ($3, pp, "/"); printf "%s ", pp[2]} \
   END {printf "\n"}'

awk >> modules/Makefile < $tmpfile '\
   BEGIN {printf "CLEANERS="} \
   ($1 == "Module" && $3 ~ /modules\//) {split ($3, pp, "/"); printf "%s_clean ", pp[2]} \
   END {printf "\n"}'

cat << EOF >> modules/Makefile

default: \$(MODULES)
	@echo "Done building module subdirectories"

clean: \$(CLEANERS)
	@echo "Done cleaning module subdirectories"

\$(MODULES): ForceMe
	(cd \$@; \$(MAKE) CC=\$(CC) AUX_CFLAGS='\$(MOD_CFLAGS)' RANLIB='\$(RANLIB)')

ForceMe:

EOF

awk >>modules/Makefile <$tmpfile \
   '($1 == "Module" && $3 ~ /modules\//) { split ($3, pp, "/"); \
   printf "%s_clean:\n\t(cd %s; $(MAKE) clean)\n\n", pp[2], pp[2]}'

