##
##  libssl.module -- mod_ssl Apache source tree stub
##
##  ====================================================================
##  Copyright (c) 1998 Ralf S. Engelschall. 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 
##      Ralf S. Engelschall <rse@engelschall.com> for use in the
##      mod_ssl project (http://www.engelschall.com/sw/mod_ssl/)."
## 
##  4. The names "mod_ssl" must not be used to endorse or promote
##     products derived from this software without prior written
##     permission. For written permission, please contact
##     rse@engelschall.com.
## 
##  5. Products derived from this software may not be called "mod_ssl"
##     nor may "mod_ssl" appear in their names without prior
##     written permission of Ralf S. Engelschall.
## 
##  6. Redistributions of any form whatsoever must retain the following
##     acknowledgment:
##     "This product includes software developed by 
##      Ralf S. Engelschall <rse@engelschall.com> for use in the
##      mod_ssl project (http://www.engelschall.com/sw/mod_ssl/)."
## 
##  THIS SOFTWARE IS PROVIDED BY RALF S. ENGELSCHALL ``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 RALF S. ENGELSCHALL OR
##  HIS 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.
##  ====================================================================
##

Name: ssl_module
ConfigStart

    #
    #   interface to the src/Configure script   
    #
    my_dir="`echo ${modfile} | sed -e 's:/[^/]*$::'`"
    my_version="$my_dir/libssl.version"
    my_outfile="Makefile.config"
    my_prefix="      +"
    SSL_CFLAGS=''

    #
    #   determine mod_ssl version
    #
    V_STR=`cat $my_version | sed -e 's;-.*;;'`
    case $V_STR in
        *.*b* )
            V_HEX=`echo "$V_STR" | sed -e 's/b.*//' | awk -F. '{ printf("%d%02d", $1, $2); }' &&
                   echo "$V_STR" | sed -e 's/.*b//' | awk '{ printf("0%02d", $1); }'`
            ;;
        *.*.* )
            V_HEX=`echo "$V_STR" | awk -F. '{ printf("%d%02d1%02d", $1, $2, $3); }'`
            ;;
    esac
    echo "$my_prefix SSL interface version: mod_ssl/$V_STR"

    #
    #   complain for DSO case
    #
    case $modfile in
        *.so ) echo "Error: The SSL module cannot be build as a DSO" 1>&2 ; exit 1 ;;
    esac

    #
    #   determine DBM support library
    #
    if [ ".$found_dbm" = .1 ]; then
        echo "$my_prefix SSL interface plugin: Vendor DBM (implicitly configured)"
    else
        . ./helpers/find-dbm-lib
        if [ ".$found_dbm" = .1 ]; then
            echo "$my_prefix SSL interface plugin: Vendor DBM"
        else
            echo "$my_prefix SSL interface plugin: Built-in SDBM (fallback)"
            SSL_CFLAGS="$SSL_CFLAGS -DSSL_USE_SDBM"
        fi
    fi

    #
    #   determine SSL_BASE
    #
    if [ ".$SSL_BASE" = . ]; then
        SSL_BASE=`egrep '^SSL_BASE=' $file | tail -1 | awk -F= '{print $2}'`
        if [ ".$SSL_BASE" = . ]; then
            SSL_BASE="/usr/local/ssl"
        fi
    fi
    case $SSL_BASE in
        SYSTEM ) ;;
        /* ) ;;
         * ) SSL_BASE="`cd ../$SSL_BASE; pwd`" ;;
    esac
    if [ ".$SSL_BASE" = .SYSTEM ]; then
        echo "$my_prefix SSL library path: [SYSTEM]"
    else
        if [ ! -d "$SSL_BASE" ]; then
            echo "Error: Cannot find SSL installation in $SSL_BASE" 1>&2  
            echo "Hint:  Please provide us with the location of SSLeay" 1>&2
            echo "       via the environment variable SSL_BASE." 1>&2
            exit 1
        fi
        echo "$my_prefix SSL library path: $SSL_BASE"
    fi

    #
    #   determine location of SSLeay binaries
    #
    if [ ".$SSL_BASE" = .SYSTEM ]; then
        SSL_BINDIR=""
        for p in . `echo $PATH | sed -e 's/:/ /g'`; do
            if [ -f "$p/ssleay" ]; then
                SSL_PROGRAM="$p/ssleay"
                SSL_BINDIR="$p"
                break
            fi
        done
        if [ ".$SSL_BINDIR" = . ]; then
            echo "Error: Cannot find SSL binaries in $PATH" 1>&2
            exit 1
        fi
    else
        if [ -f "$SSL_BASE/bin/ssleay" ]; then
            SSL_PROGRAM="$SSL_BASE/bin/ssleay"
            SSL_BINDIR='$(SSL_BASE)/bin'
        else
            if [ -f "$SSL_BASE/apps/ssleay" ]; then
                SSL_PROGRAM="$SSL_BASE/apps/ssleay"
                SSL_BINDIR='$(SSL_BASE)/apps'
            else
                echo "Error: Cannot find SSL binaries under $SSL_BASE" 1>&2
                exit 1
            fi
        fi
    fi

    #
    #   determine location of SSLeay headers
    #
    if [ ".$SSL_BASE" = .SYSTEM ]; then
        SSL_INCDIR=""
        for p in . /usr/include /usr/include/ssl/ /usr/local/include /usr/local/include/ssl; do
            if [ -f "$p/ssl.h" ]; then
                SSL_INCDIR="$p"
                break
            fi
        done
        if [ ".$SSL_INCDIR" = . ]; then
            echo "Error: Cannot find SSL header files in any of the following dirs:" 1>&2
            echo "Error: . /usr/include /usr/include/ssl/ /usr/local/include /usr/local/include/ssl" 1>&2
            exit 1
        fi
    else
        if [ -f "$SSL_BASE/include/ssl.h" ]; then
            SSL_INCDIR='$(SSL_BASE)/include'
        else
            if [ -f "$SSL_BASE/ssl.h" ]; then
                SSL_INCDIR='$(SSL_BASE)'
            else
                echo "Error: Cannot find SSL header files under $SSL_BASE" 1>&2
                exit 1
            fi
        fi
    fi
    SSL_CFLAGS="$SSL_CFLAGS -I\$(SSL_INCDIR)"

    #
    #  determine location of SSLeay libraries
    #
    if [ ".$SSL_BASE" = .SYSTEM ]; then
        SSL_LIBDIR=""
        for p in . /lib /usr/lib /usr/local/lib; do
            if [ -f "$p/libssl.a" -o -f "$p/libssl.so" ]; then
                SSL_LIBDIR="$p"
                my_real_ssl_libdir="$p"
                break
            fi
        done
        if [ ".$SSL_LIBDIR" = . ]; then
            echo "Error: Cannot find SSL library files in any of the following dirs:" 1>&2
            echo "Error: . /lib /usr/lib /usr/local/lib" 1>&2
            exit 1
        fi
    else
        if [ -f "$SSL_BASE/lib/libssl.a" ]; then
            SSL_LIBDIR='$(SSL_BASE)/lib'
            my_real_ssl_libdir="$SSL_BASE/lib"
        else
            if [ -f "$SSL_BASE/libssl.a" ]; then
                SSL_LIBDIR='$(SSL_BASE)'
                my_real_ssl_libdir="$SSL_BASE"
            else
                echo "Error: Cannot find SSL library files under $SSL_BASE" 1>&2
                exit 1
            fi
        fi
    fi
    SSL_LDFLAGS='-L$(SSL_LIBDIR)'
    SSL_LIBS="-lssl -lcrypto"

    #
    #   SSL installation type
    #
    case $SSL_BINDIR in
        */apps ) my_type="source tree only" ;;
             * ) my_type="installed package" ;;
    esac
    case $SSL_BASE in
        SYSTEM ) my_note="(system-wide)" ;;
        *      ) my_note="(stand-alone)" ;;
    esac
    echo "$my_prefix SSL library type: $my_type $my_note"

    #
    #   SSL version
    #
    SSL_VERSION="`$SSL_PROGRAM version`"
    echo "$my_prefix SSL library version: $SSL_VERSION"

    #
    #   support for RSAref library
    #
    if [ ".$RSA_BASE" = . ]; then
        RSA_BASE=`egrep '^RSA_BASE=' $file | tail -1 | awk -F= '{print $2}'`
    fi
    if [ ".$RSA_BASE" != . ]; then
        if [ ! -f "$my_real_ssl_libdir/libRSAglue.a" ]; then
            echo "Error: Cannot find SSLeay's RSAglue library under $my_real_ssl_libdir" 1>&2
            exit 1
        else
            SSL_LIBS="$SSL_LIBS -lRSAglue"
        fi
        case $RSA_BASE in
            SYSTEM ) ;;
            /* ) ;;
             * ) RSA_BASE="`cd ../$RSA_BASE; pwd`" ;;
        esac
        echo "$my_prefix SSL library plugin mode: RSAref (explicitly configured)"
    else
        if [ -f "$my_real_ssl_libdir/libRSAglue.a" ]; then
            if [ ".`$SSL_PROGRAM version -f | grep -- -DRSAref`" != . ]; then
                SSL_LIBS="$SSL_LIBS -lRSAglue"
                RSA_BASE='SYSTEM'
            fi
        fi
        if [ ".$RSA_BASE" != . ]; then
            echo "$my_prefix SSL library plugin mode: RSAref (implicitly configured)"
        else
            echo "$my_prefix SSL library plugin mode: none"
        fi
    fi
    if [ ".$RSA_BASE" != . ]; then
        if [ ".$RSA_BASE" = .SYSTEM ]; then
            my_found=no
            for p in . /lib /usr/lib /usr/local/lib; do
                if [ -f "$p/librsaref.a" -o -f "$p/librsaref.so" ]; then
                    SSL_LDFLAGS="$SSL_LDFLAGS -L$p"
                    SSL_LIBS="$SSL_LIBS -lrsaref"
                    echo "$my_prefix SSL library plugin path: $p/librsaref.a"
                    my_found=yes
                    break
                fi
            done
            if [ .$my_found = .no ]; then
                echo "Error: Cannot find RSAref library in any of the following dirs:" 1>&2
                echo "Error: . /lib /usr/lib /usr/local/lib" 1>&2
                exit 1
            fi
        else
            my_found=no
            if [ -f "$RSA_BASE/librsaref.a" ]; then
                SSL_LDFLAGS="$SSL_LDFLAGS -L$RSA_BASE/lib"
                SSL_LIBS="$SSL_LIBS -lrsaref"
                echo "$my_prefix SSL library plugin path: $RSA_BASE/librsaref.a"
                my_found=yes
            else
                if [ -f "$RSA_BASE/rsaref.a" ]; then
                    SSL_LIBS="$SSL_LIBS $RSA_BASE/rsaref.a"
                    echo "$my_prefix SSL library plugin path: $RSA_BASE/rsaref.a"
                    my_found=yes
                else
                    wild="`echo $RSA_BASE/*/rsaref.a`"
                    if [ -f "$wild" ]; then
                        SSL_LIBS="$SSL_LIBS $wild"
                        echo "$my_prefix SSL library plugin path: $wild"
                        my_found=yes
                    fi
                fi
            fi
            if [ .$my_found = .no ]; then
                echo "Error: Cannot find RSAref library under $RSA_BASE" 1>&2
                exit 1
            fi
        fi
    fi

    #
    #   adjust the Apache build environment
    #
    echo "SSL_BASE=$SSL_BASE" >>$my_outfile
    echo "SSL_BINDIR=$SSL_BINDIR" >>$my_outfile
    echo "SSL_INCDIR=$SSL_INCDIR" >>$my_outfile
    echo "SSL_LIBDIR=$SSL_LIBDIR" >>$my_outfile
    echo "SSL_PROGRAM=$SSL_PROGRAM" >>$my_outfile
    echo "SSL_VERSION=-DMOD_SSL_VERSION=\\\"$V_STR\\\"" >>$my_outfile
    CFLAGS="$CFLAGS -DMOD_SSL=$V_HEX"
    CFLAGS="$CFLAGS $SSL_CFLAGS"
    LDFLAGS="$LDFLAGS $SSL_LDFLAGS"
    LIBS="$LIBS $SSL_LIBS"

ConfigEnd

