#!/bin/sh

##  configure -- mod_accel configuration script.
##  Copyright (c) 2001-2003 Igor Sysoev. All rights reserved.

apache=''
eapi=''
depend=''
mod_charset=yes
mod_rewrite=yes
mod_randban=no
mod_quoted=no
mod_freeze=no
cachemgr=yes
patch=''
help=no

for option
do
    case "$option" in
        -*=*) value=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
           *) value='' ;;
    esac
    case "$option" in
        --help)                 help=yes                         ;;
        --with-apache=*)        apache="$value"                  ;;
        --with-eapi=*)          eapi="$value"                    ;;
        --without-mod_rewrite)  mod_rewrite=no                   ;;
        --with-mod_randban)     mod_randban=yes                  ;;
        --with-mod_quoted)      mod_quoted=yes                   ;;
        --with-mod_freeze)      mod_freeze=yes                   ;;
        --without-cachemgr)     cachemgr=no                      ;;
        --with-patch=*)         patch="$value"                   ;;
        *)
            echo "$0: Error: invalid option \"$option\""
            exit 1
            ;;
    esac
done

if [ ".$apache" = . -o $help = yes ]; then
    if [ $help != yes ]; then
        if [ ".$apache" = . ]; then
            echo "$0: Error: Bad argument line: missing --with-apache=DIR"
            echo "$0: Usage: $0 [mod_accel options]"
        fi
    fi
    echo "  --help                  this message"
    echo "  --with-apache=DIR       path to Apache 1.3.x source tree"
    echo "  --with-eapi=DIR         path to Apache EAPI"
    echo "  --without-mod_rewrite   don't patch mod_rewrite"
    echo "  --with-mod_randban      install mod_randban source"
    echo "  --with-mod_quoted       install mod_quoted source"
    echo "  --with-mod_freeze       install mod_freeze source"
    echo "  --without-cachemgr      don't build mod_accel cache manager"
    echo "  --with-patch=FILE       path to patch program"
    exit 1
fi

echo "Checking for EAPI"
if grep 'Rule EAPI' $apache/src/Configuration.tmpl >/dev/null; then
    if grep 'ap_kill_alloc_shared()' $apache/src/main/alloc.c >/dev/null; then
        echo " + broken EAPI found, patch configured"
        depend="eapi_patch accel apache mod_proxy"
    else
        echo " + EAPI found"
        depend='accel apache mod_proxy'
    fi
else
    echo " + EAPI not found"

    if [ ".$eapi" = . ]; then
        echo "$0: Error: You need specify EAPI with --with-eapi=DIR"
        exit 1
    else
        echo " + EAPI in $eapi installation configured"
        depend='eapi accel apache mod_proxy'

        if [ `grep -c 'ap_kill_alloc_shared()' $eapi/eapi.patch` = 2 ]; then
            echo " + EAPI is broken, patch configured"
            depend="$depend eapi_patch"
        fi
    fi
fi

echo "Configuring mod_accel installation"

echo "Checking for Apache version"

version=`grep "#define APACHE_RELEASE" $apache/src/include/httpd.h | \
         sed -e 's/^.*RELEASE[ ]*//'`

if [ ".$version" = . ]; then
    echo "$0: Error: can't find Apache version"
    exit 1
fi

ver=1.3.12

if [ $version -ge 10323100 ]; then
    ver=1.3.23
fi

echo "Checking for mod_charset"
if [ -f $apache/src/modules/extra/mod_charset.c ]; then
    echo " + mod_charset found"
    echo " + mod_charset patch configured"
    depend="$depend mod_charset"
else
    echo " + mod_charset not found"
    mod_charset=no
fi

if [ $mod_rewrite = yes ]; then
    echo "Configuring mod_rewrite patch"
    depend="$depend mod_rewrite"
fi

echo "Checking for mod_ssl"
if [ -f $apache/src/modules/ssl/ssl_engine_io.c ]; then
    echo " + mod_ssl found"
    echo " + mod_ssl patch configured"
    depend="$depend mod_ssl"
else
    echo " + mod_ssl not found"
fi

if [ $mod_randban = yes ]; then
    echo "Configuring mod_randban installation"
    depend="$depend randban"
fi

if [ $mod_quoted = yes ]; then
    echo "Configuring mod_quoted installation"
    if [ $mod_charset = yes ]; then
        depend="$depend quoted"
    else
        echo "$0: Error: mod_quoted requires mod_charset"
        exit 1
    fi
fi

if [ $mod_freeze = yes ]; then
    echo "Configuring mod_freeze installation"
    depend="$depend freeze"
fi

if [ $mod_randban = yes -o $mod_quoted = yes -o $mod_freeze = yes ]; then
    cflags="-DMOD_ACCEL_API"
fi

if [ $cachemgr = yes ]; then
    cflags="$cflags -DMOD_ACCEL_CACHEMGR"
fi

if [ ".$cflags" != . ]; then
    echo "CFLAGS=\"\$CFLAGS $cflags\"" > mod_accel.conf
fi

if [ ".$patch" != . ]; then
    if [ ! -f $patch ]; then
        echo "$0: Error: patch program $patch not found"
        exit 1
    fi
else
    # Solaris 8 GNU patch
    patch=`which gpatch | sed 's/ .*//'`
    if [ ".$patch" = . -o ".$patch" = .no ]; then
        patch="patch"
    fi
fi
echo "Using patch program: $patch"

sed -e "s%@DEPEND@%$depend%" \
    -e "s%@APACHE@%$apache%" \
    -e "s%@VER@%$ver%" \
    -e "s%@EAPI@%$eapi%" \
    -e "s%@PATCH@%$patch%" \
 < Makefile.in > Makefile
