#! /bin/sh

# Apache Debian package configuration script. Not finished yet...
#
# Copyright (C) 1996 Yves Arrouye <arrouye@debian.org>

me=`basename $0`

usage() {
    >&2 echo usage: $me "[ -v, --version ] [ --backup ] [ --force ] [ --force-modules ] [ --update ] [ --configfile httpdconf ] [ --port portnum ] [ --serverroot serverroot ] [ --serveradmin | --webmaster webmaster ] [ --servername altname ]"
    exit 1
}

backup=0
update=0
modulesconfig=0
reconfig=0

webmaster=-1

while [ $# -ne 0 ]
do
    case "$1" in
	-v|--version)
	    echo $me version 0.1 by Yves Arrouye "<arrouye@debian.org>"
	    exit 0
	    ;;

	--update)
	    update=1
	    ;;

	--backup)
	    backup=1
	    ;;

	--force)
	    reconfig=1
	    modulesconfig=1
	    ;;

	--force-modules)
	    modulesconfig=1
	    ;;

	--port)
	    test ! -z "$2" || usage
	    port="$2"
	    if [ ! -z "`echo $port | tr -d '[0-9]'`" ]
	    then
		>&2 echo `basename $0`: the port number must be a number
		exit 2
	    fi
	    shift
	    ;;

	--serverroot)
	    test ! -z "$2" || usage
	    serverroot="$2"
	    shift
	    ;;

	--serveradmin)
	    test ! -z "$2" || usage
	    serveradmin="$2"
	    webmaster=0
	    shift
	    ;;

	--webmaster)
	    test ! -z "$2" || usage
	    serveradmin="$2"
	    webmaster=1
	    shift
	    ;;

	--servername)
	    test ! -z "$2" || usage
	    servername="$2"
	    shift
	    ;;

	--configfile)
	    test ! -z "$2" || usage
	    httpd="$2"
	    shift
	    ;;

	--installin)
	    test ! -z "$2" || usage
	    installin="$2"
	    shift
	    ;;

	*)
	    usage
	    ;;
    esac
    shift
done

apacheconfig=/usr/lib/apache/config/scripts

if [ -r ${apacheconfig}/apache.sh ]
then
    . ${apacheconfig}/apache.sh
     _apache_sh_testloaded
else
    >&2 echo $me: cannot load informations from ${apacheconfig}
    exit 2
fi

# Serious things begin here...

cleanup() {
    if [ ! -z "$backmdsum" ]
    then
    	newmdsum=`md5sum ${httpd} | sed 's/ .*//'`
    	if [ "$newmdsum" = "$backmdsum" ]
    	then
	    rm -f ${httpd_saved}
        fi
    fi
}

thconf=$TMPDIR/.httpd.conf.$$
trap "rm -rf $thconf; cleanup"

if [ -z "$PAGER" -a -x /bin/more ]
then
    PAGER=/bin/more
fi

: {$PAGER:=cat}

httpd_orig="${httpd}"

if [ $reconfig -ne 0 ]
then
    cat <<EOF
When it starts, Apache reads its initial configuration from a file that
contains configuration directives.  Please confirm the path where this
file can be found."
EOF
    _httpd="$httpd"
    echon "Configuration file? [$_httpd] "
    read ans
    : ${ans:=$_httpd}
    httpd="$ans"
    echo
    readed=yes
fi

until [ -e "${httpd}" ]
do
    if [ -z "${httpd}" -a ! -z "$readed" ]
    then
	cat <<EOF
Please give the path to the configuration file that will be used by
Apache.
EOF
    else
    	cat <<EOF
Apache requires a configuration file to start, but apparently the file
\`${httpd}' does not exist.
EOF
    fi
    echon 'Path of Apache configuration file [] '
    read ans
    httpd="$ans"
    echo
done

if [ "${httpd_orig}" != "${httpd}" ]
then
    rm -f $thconf

    sed '/^: \${httpd:=/,$d' ${apacheconfig}/variables.sh >>$thconf
    echo ": \${httpd:=${httpd}}" >>$thconf
    sed '1,/^: \${httpd:=/,d' ${apacheconfig}/variables.sh >>$thconf

    if [ -s $thconf ]
    then
	mv -f $thconf ${apacheconfig}/variables.sh
	. ${apacheconfig}/variables.sh
    else
        rm -f $thconf
    fi
fi

httpd_saved=${httpd}.inst-orig

addloadmodulesamples() {
    if [ -r /usr/doc/apache/modules/LoadModule.samples ]
    then
    	cat /usr/doc/apache/modules/LoadModule.samples
    else
    	echo "$loadmodulemagicline"
    fi
}

# Ok, we start.

if [ $backup -ne 0 ]
then
    backmdsum=`2>/dev/null md5sum ${httpd} | sed 's/ .*//'`

    if [ -e ${httpd_saved} ]
    then
    	cat <<EOF
Oops.  A saved configuration file already exists.  You may want to
abort this configuration step if you're not sure to want to change
the new configuration file, otherwise this saved configuration file
${httpd_saved} will be removed.
EOF
	yorn n 'Do you want to restore the saved configuration?'
    	if [ $boolean_answer = y ]
    	then
	    cp ${httpd_saved} ${httpd}
	    . ${apacheconfig}/variables.sh
    	else
    	    yorn n 'Do you want to remove the saved configuration?'
            if [ $boolean_answer = n ]
	    then
            	echo
	    	echo 'Configuration aborted.'
	    	exit 3
	    else
		rm -f ${httpd_saved}
	    fi
        fi
        echo
    fi

    # echo Saving existing Apache configuration...
    # echo

    cp ${httpd} ${httpd_saved}
fi

# Setting of the document root.

if [ -z "$conf_serverroot" ]
then
    cat <<EOF
The Apache server will serve documents from a directory called the
document root or server root.  You must specify such a directory for
the server to work.
EOF
    serverroot="$dflt_serverroot"
    echon "Document root? [$serverroot] "
    read ans
    : ${ans:=$serverroot}
    while ! mkdir -p ${ans} 2>/dev/null
    do
	echo "The directory \`$ans' cannot be created."
        echon "Document root? [$serverroot] "
        read ans
        : ${ans:=$serverroot}
    done
    serverroot="$ans"
    echo
fi

if [ "$serverroot" != "$conf_serverroot" ]
then
    sed "s,^[# 	]*ServerRoot[ 	].*,ServerRoot $serverroot," \
    	${httpd} >$thconf && mv $thconf ${httpd}
fi

if [ "$serverroot" != "$conf_serverroot" -a -d "$conf_serverroot" \
    -a -d "$serverroot" ]
then
    echon "Moving document root from \`$conf_serverroot' to \`$serverroot'..."
    ((cd $conf_serverroot; tar cf - .) | (cd $serverroot; tar xpf -)) && \
	rm -rf "$conf_serverroot"
    echo ' done'
fi

# Webmaster.

# wentry=`grep '^www-data:' /etc/passwd`

serveradminalias=`2>/dev/null grep -i webmaster /etc/aliases | sed 's/.*:[ 	]*\([^ 	]*\).*$/\1/'`

: ${serveradmin:=`2>/dev/null sed -n 's/^[ 	]*ServerAdmin[ 	]*\([^ 	]*\).*/\1/p' ${httpd} | head -1`}

b=`echo $serveradmin | tr '[A-Z]' '[a-z]'`
if [ "$b" = webmaster -o "$b" = webmaster@`hostname --fqdn` \
    -o "$b" = webmaster@`hostname` ]
then
    serveradmin=$serveradminalias
fi

: ${serveradmin:=$serveradminalias}

if [ $reconfig -eq 1 -o -z "$serveradmin" ]
then
    a=$serveradmin

    cat <<EOF
Enter the email address of your server administrator.  This address
will be used in error messages allowing users to submit reports of
faulty links or misconfigured cgi-programs to you.  It should be an
email address that correspond to a human.
EOF
    echo -n "Webmaster email address [$a]: "
    read b
    : ${b:=$a}
    b=`echo $b | tr '[A-Z]' '[a-z]'`
    if [ "$b" != webmaster -a "$b" != webmaster@`hostname --fqdn` \
	-a "$b" != webmaster@`hostname` ]
    then
        sed -e "s/^[# 	]*ServerAdmin[ 	][ 	]*.*/ServerAdmin $a/" \
	    ${httpd} >$thconf && \
        mv -f $thconf ${httpd}
	serveradmin=$b
    else
	serveradmin=
    fi
    echo
fi

if [ ! -z "$serveradmin" ]
then
    runaliases=0

    if [ $reconfig -eq 0 ]
    then
	if [ -z "$serveradminalias" ]
	then
	    webmaster=0
	else
	    webmaster=1
	fi
    fi

    if [ $webmaster -eq -1 ]
    then
        cat <<EOF
A webmaster address has been chosen.  It can either be used directly
by the Apache server or defined as an alias so that anybody can reach
the corrected people by mailing to webmaster, which is recommended.
EOF
    	yorn y 'Set an alias for the webmaster?'
    	echo
    	if [ $boolean_answer = y ]
	then
	    webmaster=1
	else
	    webmaster=0
	fi
    fi

    if [ $webmaster -eq 1 ]
    then
	if [ "$serveradmin" != "$serveradminalias" ]
	then
    	    echo "Updating webmaster alias (webmaster: $serveradmin)..."
    	    if [ -f /etc/aliases ] && \
	        grep -i -q '^[ 	]*webmaster[ 	]*' /etc/aliases
    	    then
	        sed "s/^[ 	]*[wW][eE][bB][mM][aA][sS][tT][eE][rR][ 	]*:.*/webmaster: $serveradmin/" /etc/aliases \
	    	    >${TMPDIR}/.aliases.$$ && mv -f ${TMPDIR}/.aliases.$$ /etc/aliases
            else
	        if [ -f /etc/aliases ]
	        then
	            aliases=/etc/aliases
	        fi

	        cat $aliases - <<EOF >${TMPDIR}/.aliases.$$

# Maintainer of the Web server
webmaster: $serveradmin

EOF
	        if [ -s ${TMPDIR}/.aliases.$$ ]
	        then
	            mv -f ${TMPDIR}/.aliases.$$ /etc/aliases
	        fi
	    fi

	    runaliases=1

	    echo Updating server administrator address...
	    sed 's/^[# 	]*ServerAdmin[ 	][ 	]*\(.*\)/ServerAdmin webmaster/' ${httpd} >${thconf} && mv -f ${thconf} ${httpd}
	fi
    else
    	if [ -f /etc/aliases ] && grep -i -q '^[ 	]*webmaster[ 	]*'/etc/aliases
	then
	    echo Removing existing webmaster alias...
	    sed "/^[ 	]*[wW][eE][bB][mM][aA][sS][tT][eE][rR][ 	]*:/d" /etc/aliases \
	    	>${TMPDIR}/.aliases.$$ && mv -f ${TMPDIR}/.aliases.$$ /etc/aliases
	    runaliases=1
	fi
	echo Updating server administrator address...
	sed "s/^[# 	]*ServerAdmin[ 	][ 	]*.*/ServerAdmin $serveradmin/" ${httpd} >${thconf} && mv -f ${thconf} ${httpd}
    fi

    if [ $runaliases -eq 1 ]
    then
        newaliases=`which newaliases`
    	if [ ! -z "$newaliases" ]
    	then
	    $newaliases
    	else
	    sendmail=`which sendmail`
	    if [ ! -z "$sendmail" ]
	    then
	    	$sendmail -bi
	    fi
	fi
    fi
fi

# Setup the server name.

if [ ! -z "$servername" ] || grep -q '^##[ 	]*ServerName[ 	]*' ${httpd}
then

: ${servername:=`2>/dev/null sed -n 's/^ServerName[ 	]*\([^ 	]*\)[ 	]*$/\1/p' ${httpd}`}

if [ -z "$servername" ]
then
   host=`hostname`
   domain=`hostname --domain`
   : ${host:=bart}
   : ${domain:=foo.com}

   cat <<EOF

You may want to choose an alternate name for your server.  This name is
used to make the server report itself as being someone else, for example
to replace $host.$domain by www.$domain which looks nicer.
Please note that you may still need to make sure the alternate name will
be recognized by the Domain Name Service if you want your server to be
reached!
EOF
    echo -n "Server alternate name []: "
    read servername
    echo
fi

if [ "$servername" != "" ]
then
    sed -e "s/^##[ 	]*ServerName[ 	]*[^ 	]*[ 	]*$/ServerName $servername/" \
	< ${httpd} >$thconf && mv -f $thconf ${httpd}
else
    sed -e "s/^##[ 	]*\(ServerName[ 	]*[^ 	]*\)[ 	]*$/#\1/" \
	< ${httpd} >$thconf && mv -f $thconf ${httpd}
fi

fi

# Setup the port number.

if grep -q "^[ 	]*#[# 	]*Port[ 	]" ${httpd}
then
    port=80
    cat <<EOF
Please choose a port number on which the Apache server will listen
for incoming connections.  This port number is usually 80, but you
may want to choose another one if you have another server already
listening on that port, or if you want to listen on an unpriviledged
port; in this case, the port 8080 might be a good choice.
EOF
    echon "Port to listen on? [$port]"
    ans=$port
    read port
    : ${port:=$ans}
fi

if [ ! -z "$port" ]
then
    sed -e "s/^[# 	]*Port[ 	][ 	]*[0-9][0-9]*[# 	]*$/Port $port/" \
	< ${httpd} > ${thconf} && mv -f ${thconf} ${httpd}
fi

#

missing=

    if [ $update -eq 0 ]
    then
	cat <<EOF
The release of the Apache server you are using supports dynamically loaded
modules providing its functionality.  Please wait while the modules needed
by your configuration are determined...
EOF
    else
	echo Updating Apache server configuration...
    fi

    addmoduleline() {
	# We used to look for the magic line, now we rather look for
	# the last LoadModule directive.

	magic="`grep -n '^[ 	#]*LoadModule[ 	]' ${httpd} | tail -1`"
	if [ -z "$magic" ]
	then
	    aftermagic=1
	    magic="`grep -n "^$loadmodulemagicline" ${httpd}`"
	else
	    aftermagic=0
	fi

	if [ ! -z "$magic" ]
	then
	    magic=`echo $magic | sed 's/[^ 0-9].*//'`
	    if [ $aftermagic -eq 1 ]
	    then
	        magic=`expr $magic + 1`
	    fi

	    (exec 1>$thconf;
		sed -n "1,${magic}p" ${httpd};
		echo "$1";
		sed "1,${magic}d" ${httpd}) &&
	    mv -f $thconf ${httpd}
	else
	    (exec 1>$thconf; echo "$1"; cat ${httpd}) >$thconf && \
		mv -f $thconf ${httpd}
	fi
    }

    modules=''
    needed=''
    unused=''

    apache_dirs=`echo $APACHE_MODULES_PATH | tr ':' ' '`

    sofiles=`(2>/dev/null find $apache_dirs -type f -name '*.so' -print)`

    echon Checking for available modules...

    for line in `2>/dev/null grep '^[^#]*LoadModule[ 	][ 	]*' \
	${httpd}`
    do
	f=`httplinefilename "$line"`
	if [ -e "$f" -a ! -d "$f" ]
	then
	    mod=`echo "$line" |
		sed 's/.*LoadModule[ 	][ 	]*\([^ 	]*\).*/\1/'`
	    name=`/usr/sbin/apachemodname $m`
	    : ${name:=$mod}
    	    if [ -z "`echo \"$loadable\" | grep \" $name \"`" ]
	    then
	    	eval "module_$name='$m'"
            	loadable="$loadable $name "
	    fi
	fi
	echon .
    done

    for m in $sofiles
    do
	mod=`basename $m .so`
	if [ -f /usr/lib/apache/modules/$mod.so ]
	then
	    name=`/usr/sbin/apachemodname $m`
	    : ${name:=$mod}
    	    if [ -z "`echo \"$loadable\" | grep \" $name \"`" ]
	    then
	        eval "module_$name='$m'"
                loadable="$loadable $name "
	    fi
	fi
	echon .
    done

    echo ' done.'

    lmlstline=`wc -l $httpd | sed 's/[^ 0-9].*//'`
    lmline=$lmlstline

    # The determine function looks for a regexp and if it finds it,
    # updates the needed and lmline variables to indicate which
    # modules should be loaded and before which line the LoadModule
    # commands should be put.

    determine() {
	reclinenum=0

	if [ x"$1" = x'-l' ]
	then
	    reclinenum=0
	    shift
	fi

	if [ $# -lt 3 ]
	then
	    echo ' oops!'
	    >&2 echo "Apache installation: internal error: bad arg count in determine $1"
	    exit 1
	fi

	module="$1"
	regexp="$2"

	shift 2

	_needed=0

	for file
	do
	    cline=`egrep -n "$regexp" "$file" | head -1`
	    if [ ! -z "$cline" ]
	    then
		_needed=1

	        if [ $reclinenum -eq 1 -a "$file" = "$httpd" ]
	        then
	            lnum=`echo "$cline" | sed 's/[^ 0-9].*//'`
	    	    if [ $lnum -lt $lmline ]
		    then
		        lmline=$lnum
		    fi
		fi

		break
	    fi
	done

	if [ $_needed -eq 0 ]
	then
	    unused="$unused $module "
	else
	    needed="$needed $module "
	    # put it in front of the list
	    loadable="`echo \"$loadable\" | sed \"s/ $module / /\"`"
	    loadable=" $module $loadable"
	fi

	echon .
    }

    echo Checking for needed modules...

    # htaccess will be a list of .htaccess files.

    accdirs=`2>/dev/null \
	sed -n 's/<Directory[ 	][ 	]*\(.*\)>/\1/p' $access`
    for d in $accdirs
    do
	if [ -d $d ]
	then
            htaccess="$htaccess `2>/dev/null find $serverroot \
		-name .htaccess -print`"
	fi
    done

    determine alias_module ' *(Alias|ScriptAlias|Redirect)' $httpd $srm
    determine userdir_module ' *UserDir' $httpd $srm

    determine auth_module ' *Auth(User|Group)File' $access $htaccess $httpd
    determine dbm_auth_module ' *AuthDBM(User|Group)File' $access $htaccess \
	$httpd
    determine db_auth_module ' *AuthDB(User|Group)File' $access $htaccess $httpd
    determine anon_auth_module ' *Anonymous' $access $htaccess $httpd
    determine msql_auth_module ' *Auth_MSQL' $access $htaccess $httpd

    determine digest_module ' *AuthDigestFile' $access $htaccess $httpd

    determine config_log_module ' *TransferLog' $httpd
    determine referer_log_module ' *(RefererLog|RefererIgnore)' $httpd
    determine agent_log_module ' *AgentLog' $httpd
    
    determine cookies_module ' *CookieLog' $httpd

    determine proxy_module ' *(Cache|NoCache)' $httpd

    determine status_module '[ 	]*<Location[ 	][ 	]*/status[ 	]*>' $access
    determine info_module '[ 	]*<Location[ 	][ 	]*/info[ 	]*>' $access

    # Source modules-specific files.

    for d in $apache_dirs
    do
	for m in `2>/dev/null find $d/config/determine ! -type d -print`
	do
	    . $m
	done
    done

    oldlmline=$lmline
    determine -l cgi_module '[^#]*ExecCGI' $access
    if [ $oldlmline -ne $lmline ]  
    then
        lmline=$oldlmline 
    fi               

    echon ' wait...'

    # Now inserts a LoadModule: section if there is no one...

    if ! grep -q "^$loadmodulemagicline" ${httpd}
    then
	echo ' paused.'
    	echo "Adding samples LoadModule directives to $httpd"

	if [ $lmline -eq $lmlstline ]
	then
	    lmline=

            : ${lmline:=`grep -n '^ *#*.*BindAddress  *' ${httpd}`}
            : ${lmline:=`grep -n '^ *#*.*ServerRoot  *' ${httpd}`}

	    lmline=`echo $lmline | sed 's/[^ 0-9].*//'`
	fi

        if [ -z "$lmline" -o 0$lmline -eq $lmlstline ]
        then
	    (exec 1>$thconf; echo; addloadmodulesamples; cat $httpd) &&
	    mv -f $thconf $httpd
        else
	    lmline=`echo $lmline | sed 's/[^ 0-9].*//'`
	    lmnxtline=`expr $lmline + 1`

            (exec 1>$thconf; sed -e "$lmnxtline,\$d" $httpd;
		addloadmodulesamples;
                sed -e "1,${lmline}d" $httpd) &&
	        mv -f $thconf $httpd
	fi

    	echon Resuming check for required modules...
    fi

    # Do some special-case handling.

    if [ -z "`echo \"$needed\" | grep \" db_auth_module \"`" ]
    then
	libdb=`2>/dev/null ls /usr/lib/libdb.so.*.*`
	if [ -f $libdb -a -f `modulefilename db_auth_module` ]
	then
    	    if [ ! -z "`echo \"$needed\" | grep \" dbm_auth_module \"`" ]
	    then
		echo ' paused.'
		cat <<EOF

You are using DBM authentication though the Berkeley DB library is
installed on your system.  If you can use DB instead of DBM it is
recommended that you do that; in this case, you will have to change
the AuthDBM directives to AuthDB ones with the names of your new
authentication files.

EOF
		yorn n 'Do you want to use DB rather than DBM?'
		if [ "$boolean_answer" = y ]
		then
		    modules="$modules auth_db_module "
		    needed="`echo \"$needed\" | sed 's/ auth_dbm_module / /'`"
		fi
    		echon Resuming check for required modules...
	    fi
	fi
    fi

    # Remove predefined modules from the list of needed ones.

    for m in $needed
    do
	if [ ! -z "`echo \"$predefined\" | grep \" $m \"`" ]
	then
	    needed="`echo $needed | sed \"s/ $m / /\"`"
	fi
    done

    echo ' done.'

    # Warn about unavailable modules.

    for module in $needed $modules
    do
	if [ -z "`grep \"^ *LoadModule  *$module \" ${httpd}`" ]
	then
	    : ${m:=`modulefilename $module`}
	    : ${m:=$module}

	    if [ -z "`echo \"$loadable\" | grep \" $m \"`" ]
	    then
	        if [ -z "`echo \"$missing\" | grep \" $m \"`" ]
	        then
		    missing="$missing $m "
	            cat <<EOF

Warning! Your configuration file needs the \`$m' module.
But this module was not found in a known place. If you do not
edit your configuration to correct the problem, Apache will fail
to start because of unknown directives.
EOF
		fi
	    fi
	else
	    if [ $modulesconfig -eq 0 ]
	    then
	        loadable=`echo "$loadable" | sed "s/ $module / /"`
	    fi
	fi
    done

    if [ ! -z "$missing" ]
    then
	echo
    fi

    # Remove unwanted modules from the list we will pronpt for.

    echon Checking for unwanted modules...

    for m in `sed -n 's/^# *LoadModule  *\([^ ][^ ]*\).*/\1/p' ${httpd}`
    do
	if [ -z "`echo \"$needed\" | grep \" $m \"`" ]
	then
	    loadable=`echo "$loadable" | sed "s/ $m / /"`
	    unwanted="$unwanted $m "
	fi
	echon .
    done

    for m in `sed -n 's/^ *LoadModule  *\([^ ][^ ]*\).*/\1/p' ${httpd}`
    do
	if [ -z "`echo \"$needed\" | grep \" $m \"`" ]
	then
	    loadable=`echo "$loadable" | sed "s/ $m / /"`
	fi
	modules=`echo "$modules" | sed "s/ $m / /"`
	echon .
    done

    echo ' done.'
    echo

    loadable=`echo $loadable | sed 's/^ $//'`

    if [ ! -z "$loadable" ]
    then
        cat <<EOF
As was indicated before, the Apache server can dynamically load modules
when it starts up, adding features (and directives) augmenting its power.
EOF
    	yorn y 'Do you want to be prompted for modules to load?'
	echo

    	if [ "$boolean_answer" = y ]
    	then
	    cat << EOF
For each module, a short module description will be displayed and
you will be asked whether to load the module or not.  You will also be
able to obtain a longer documentation for help.  In the interaction
that follows, a module will be called required if your actual Apache
configuration needs it: if you do not load such modules, you will have
to change your configuration files (but because the guess for the need
for certain modules may be wrong, it may be okay to answer no).

EOF

	    for m in $loadable
	    do
		f=`modulefilename $m`
		d=`basename $f .so`
		if [ -s `dirname $f`/../descr/$d ]
		then
		    descr=`dirname $f`/../descr/$d
		else
		    if [ -s `dirname $f`/../descr/$m ]
		    then
			descr=`dirname $f`/../descr/$d
		    fi
		fi
		echo
		if [ ! -z "$descr" ]
		then
		    echo "Short description for module \`${m}':"
		    cat $descr |
			sed -e 's/^ *//' -e 's/^/    /' | uniq
		else
		    echo "No description is available for module \`$m'".
		fi
		if [ -z "`echo \"$needed\" | grep \" $m \"`" ]
		then
		    qualif=
		    if [ $update -eq 0 ]
		    then
		        ans=n
		    else
			ans=y
		    fi
		else
		    qualif=' *required*'
		    ans=y
		fi
		yorn -h /usr/doc/apache/modules/${m} \
		    $ans "Load the$qualif \`$m' module?"
		if [ "$boolean_answer" = y ]
		then
		    if [ -z "`echo \"$modules\" | grep \" $m \"`" ]
		    then
		        loadmod="$loadmod $m "
		        modules="$modules $m "
		    fi
		else
		    if [ -z "`echo \"$unwanted\" | grep \" $m \"`" ]
		    then
		        unloaded="$unloaded $m "
		        unwanted="$unwanted $m "
		    fi
		fi
		echo
	    done
	else
	    for m in $loadable
	    do
		if [ -z "`echo \"$modules\" | grep \" $m \"`" ]
		then
		    loadmod="$loadmod $m "
		    modules="$modules $m "
		fi
	    done
    	fi
    fi

    # Resolve modules conflicts.

    has_log=x"`echo \"$modules\" | grep \" log_module \"`"
    has_log_config=x"`echo \"$modules\" | grep \" log_config_module \"`"

    if [ $has_log != x -a $has_log_config != x ]
    then
cat <<EOF
Two conflicting modules (log_module and log_config_module) have been
selected for loading.  Only log_config_module, which effectively
supersedes log_module, will be kept.

EOF
	modules="`echo \"$modules\" | sed 's, log_module ,,'`"
    else
	modules="$modules $needed"
    fi

    # Now install the modules loading directives.

    if [ ! -z "$loadmod$unloaded" ]
    then
	echon Updating modules loading directives...

	for m in $loadmod
	do
	    f=`modulefilename $m`
	    if grep -q "^ *#*.*LoadModule  *$m " ${httpd}
	    then
		sed "s,^ *#*.*LoadModule  *$m .*,LoadModule $m $f," ${httpd} >$thconf && mv -f $thconf ${httpd}
	    else
		addmoduleline "LoadModule $m `apachefilename $f`"
	    fi

	    echon .
	done

	for m in $unloaded
	do
	    f=`modulefilename $m`
	    f=`apachefilename $f`
	    if grep -q "^ *#*.*LoadModule  *$m " ${httpd}
	    then
	        sed "s, *#*.*\\( *LoadModule $m\\) *.*,#\\1 $f," \
		    ${httpd} >$thconf && mv -f $thconf ${httpd}
	    else
		addmoduleline "#LoadModule $m $f"
	    fi

	    echon .
	done

	echo ' done.'
    fi

if [ $backup -ne 0 ]
then
    newmdsum=`md5sum ${httpd} | sed 's/ .*//'`
    if [ "$newmdsum" = "$backmdsum" ]
    then
	rm -f ${httpd_saved}
    else
	cat <<EOF
Despite the care taken to design them, the configuration steps may
eventually have produced an incorrect configuration file.  If anything
goes wrong, you may want to recover your previous configuration file
which was saved as ${httpd_saved}.

Also note that authentication mechanisms needed by .htaccess files may
be wrong: either authentication methods were selected from unused files
(this is not a problem) or some authentication methods selected outside
of the document root tree were not seen...
EOF
    fi
fi

if [ ! -z "$installin" ]
then
    echo $modules >"$installin".modules
    echo $missing >"$installin".missing
fi

exit 0

