#!/bin/sh
##
##  configure -- mod_ssl configuration script
##  Written by Ralf S. Engelschall <rse@engelschall.com>
##

DIFS=' 	
'

#
#   configuration
#
prefix=" +"
prefixe="  "
apache=
certificate=
ssleay=
apaci=

#
#   determine versions
#
V_MODSSL=`cat ssl.source/libssl.version | sed -e 's;-.*;;'`
V_APACHE=`cat ssl.source/libssl.version | sed -e 's;.*-;;'`

#
#   parse argument line
#
prev=''
OIFS="$IFS" IFS="$DIFS"
for option
do
    if [ ".$prev" != . ]; then
        eval "$prev=\$option"
        prev=""
        continue
    fi
    case "$option" in
        -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
           *) optarg='' ;;
    esac
    case "$option" in
        --with-apache=*)      apache="$optarg"       ;;
        --with-certificate=*) certificate="$optarg"  ;;
        --with-ssleay=*)      ssleay="$optarg"       ;;
        * )                   apaci="$apaci $option" ;;
    esac
done
IFS="$OIFS"
if [ ".$prev" != . ]; then
    echo "$0:Error: missing argument to --`echo $prev | sed 's/_/-/g'`" 1>&2
    exit 1
fi

#
#   usage
#
if [ ".$apache" = . ]; then
    echo "$0: Bad argument line"
    echo "$0: Usage: $0 [mod_ssl options] [APACI options]"
    echo "mod_ssl options:"
    echo "  --with-apache=DIR       ...path to Apache 1.3.x source tree [NEEDED]"
    echo "  --with-ssleay=DIR       ...path to SSLeay 0.9.x source tree [OPTIONAL]"
    echo "  --with-certificate=FILE ...path to SSL certificate file [OPTIONAL]"
    echo "APACI options:"
    echo "  --prefix=DIR            ...installation prefix for Apache"
    echo "  --...                   ...see the INSTALL file of Apache for more"
    exit 1
fi

#
#   give a friendly header
#
echo "Configuring mod_ssl/$V_MODSSL for Apache/$V_APACHE" 2>&1

#
#   check for Apache 1.3
#
if [ ! -f "$apache/src/include/httpd.h" ]; then
    echo "Error: Cannot find Apache 1.3 source tree under $apache" 1>&2
    echo "Hint:  Please specify location via --with-apache=PATH" 1>&2
    exit 1
fi
APV=`cat $apache/src/include/httpd.h |\
     grep "#define SERVER_BASEVERSION" |\
     sed -e 's/^[^"]*"//' -e 's/".*$//' -e 's/^Apache\///'`
if [ ".$V_APACHE" != ".$APV" ]; then
    echo "Error: The mod_ssl/$V_MODSSL can be used for Apache/$V_APACHE only." 1>&2
    echo "Error: Your Apache source tree under $apache is version $APV." 1>&2
    echo "Hint:  Please use an extracted apache_$V_APACHE.tar.gz tarball" 1>&2
    echo "Hint:  with the --with-apache option, only." 1>&2
    exit 1
fi
echo "$prefix Using Apache $APV source tree under $apache"

#
#   check for SSLeay
#
if [ ".$ssleay" != . ]; then
    if [ ! -d "$ssleay" ]; then
        echo "Error: Cannot find SSLeay source or install tree under $ssleay" 1>&2
        echo "Hint:  Please specify location via --with-ssleay=PATH" 1>&2
        exit 1
    fi
    echo "$prefix Using SSLeay source or install tree under $ssleay"
    ssleay="`cd $ssleay; pwd`"
fi

#
#   check for SSL certificate file
#
if [ ".$certificate" != . ]; then
    if [ ! -f "$certificate" ]; then
        echo "Error: Cannot find Certificate file $certificate" 1>&2
        exit 1
    fi
fi

#
#   build `patch' program
#
if [ ! -f ./aux/patch/patch ]; then
    (cd aux/patch; ./configure; make) 2>&1 |\
    aux/prop.sh "$prefix Building auxiliary tools"
else
    echo "$prefix Building auxiliary tools (skipping - already done)"
fi

#
#   Applying mod_ssl to the Apache source tree
#
echo "$prefix Applying mod_ssl extension and patches to Apache source tree"
for file in README LICENSE INSTALL; do
    echo "$prefixe creating: [FILE] $file.SSL"
    cp -p $file $apache/$file.SSL
done
echo "$prefixe creating: [FILE] src/CHANGES.SSL"
cp -p CHANGES $apache/src/CHANGES.SSL
if [ ".`egrep ^ssl=0 $apache/configure`" = . ]; then
    cat ssl.patch/apache.patch |\
    aux/patch/patch --forward --directory=$apache 2>&1 |\
    egrep '^.Index:' | sed -e "s/.*Index: /$prefixe patching: [FILE] /"
else
    cat ssl.patch/apache.patch |\
    egrep '^Index:' | sed -e "s/.*Index: /$prefixe skipping: [FILE] /"
fi
if [ ! -d "$apache/src/modules/ssl" ]; then
    echo "$prefixe creating: [DIR]  src/modules/ssl"
    mkdir $apache/src/modules/ssl
fi
for file in `cd ssl.source; echo *`; do
    test ! -f ssl.source/$file && continue
    echo "$prefixe creating: [FILE] src/modules/ssl/$file"
    cp -p ssl.source/$file $apache/src/modules/ssl/
done
if [ ! -d "$apache/conf/sslkeys" ]; then
    echo "$prefixe creating: [DIR]  conf/sslkeys"
    mkdir $apache/conf/sslkeys
fi
if [ ! -d "$apache/conf/sslcerts" ]; then
    echo "$prefixe creating: [DIR]  conf/sslcerts"
    mkdir $apache/conf/sslcerts
fi
echo "$prefixe creating: [FILE] conf/sslcerts/server.pem"
if [ ".$certificate" != . ]; then
    cp -p $certificate $apache/conf/sslcerts/server.pem
else
    cp -p ssl.conf/server.pem $apache/conf/sslcerts/server.pem
fi
echo "$prefixe creating: [FILE] conf/sslcerts/Makefile"
cp -p ssl.conf/Makefile $apache/conf/sslcerts/Makefile
echo "$prefixe creating: [FILE] htdocs/manual/mod/mod_ssl.html"
cp -p ssl.docs/mod_ssl.html $apache/htdocs/manual/mod/mod_ssl.html
echo "$prefixe creating: [FILE] htdocs/manual/images/mod_ssl.jpg"
cp -p ssl.docs/mod_ssl.jpg $apache/htdocs/manual/images/mod_ssl.jpg
echo "$prefixe creating: [FILE] htdocs/manual/images/ssleay.gif"
cp -p ssl.docs/ssleay.gif $apache/htdocs/manual/images/ssleay.gif

#
#   Optionally configure Apache
#
if [ ".$ssleay" != . ]; then
    echo "$prefix Configuring Apache source tree"
    cd $apache
    SSL_BASE=$ssleay ./configure $apaci --enable-module=ssl
    echo "Now please switch to $apache and run:"
    echo " \$ make"
    if [ ".$certificate" = . ]; then
        echo " \$ make certificate"
    fi
    echo " \$ make install"
else
    echo "Now please switch to $apache and run:"
    echo " \$ SSL_BASE=/path/to/ssleay ./configure ... --enable-module=ssl"
    echo " \$ make"
    if [ ".$certificate" = . ]; then
        echo " \$ make certificate"
    fi
    echo " \$ make install"
fi

