#!/bin/sh

echo "Starting auto configuration ..."
rm -f Makefile.in Makefile.amap
SSL_PATH=""
SSL_IPATH=""
CRYPTO_PATH=""
NSL_PATH=""
SOCKET_PATH=""
TEST=`which test`
echo

echo "Checking for openssl ..."
for i in  /lib  /usr/lib /usr/local/lib /ssl /openssl /opt/openssl \
/opt/ssl /usr/ssl /usr/openssl /usr/local/ssl /usr/local/openssl \
/openssl/lib /ssl/lib /opt/ssl/lib /opt/openssl/lib /usr/ssl/lib \
/usr/openssl/lib /usr/local/ssl/lib /usr/local/openssl/lib
do
    if [ -z "$SSL_PATH" ]; then
        if [ -e "$i/libssl.so" ]; then
            SSL_PATH="$i"
        fi
    fi
    if [ -z "$SSL_PATH" ]; then
       TMP_LIB=`echo $i/lib*ssl.so* | grep libssl.so 2> /dev/null`
        if [ -n "$TMP_LIB" ]; then
          SSL_PATH="$i"
        fi
    fi
    if [ -z "$CRYPTO_PATH" ]; then
        if [ -e "$i/libcrypto.so" ]; then
            CRYPTO_PATH="$i"
        fi
    fi
    if [ -z "$CRYPTO_PATH" ]; then
       TMP_LIB=`echo $i/libcrypto.so* | grep libcrypto.so 2> /dev/null`
        if [ -n "$TMP_LIB" ]; then
          CRYPTO_PATH="$i"
        fi
    fi
done

for i in /usr/include /usr/local/include /openssl/include /ssl/include \
/opt/ssl/include /opt/openssl/include /usr/openssl/include \
/usr/ssl/include /usr/local/openssl/include /usr/local/ssl/include
do
    if [ -z "$SSL_IPATH" ]; then
        if [ -e "$i/openssl/ssl.h" ]; then
            SSL_IPATH="$i"
        fi
    fi
done

if [ -z "$SSL_PATH" ]; then
    SSL_IPATH=""
    CRYPTO_PATH=""
fi
if [ -z "$SSL_IPATH" ]; then
    SSL_PATH=""
    CRYPTO_PATH=""
fi

if [ -n "$SSL_PATH" ]; then
    echo "                     ... found"
fi
if [ -z "$SSL_PATH" ]; then
    echo "                     ... NOT found, please install it first"
fi

SYS=`uname`
if [ "$SYS" = "Solaris" ]; then
    echo "Checking for Solaris libraries ..."
    for i in  /lib /usr/lib /usr/local/lib
    do
        if [ -z "$NSL_PATH" ]; then
            if [ -e "$i/libnsl.so" ]; then
                NSL_PATH="$i"
            fi
        fi
        if [ -z "$SOCKET_PATH" ]; then
            if [ -e "$i/libsocket.so" ]; then
                SOCKET_PATH="$i"
            fi
        fi
    done

    if [ -z "$NSL_PATH" ]; then
        echo "NSL library not found, which is needed on Solaris."
    fi
    if [ -z "$SOCKET_PATH" ]; then
        echo "Socket library not found, which is needed on Solaris."
    fi
fi

echo
XDEFINES=""
XLIBS=""
XLIBPATHS=""
XIPATHS=""
for i in $SSL_PATH $CRYPTO_PATH $NSL_PATH $SOCKET_PATH; do
    XLIBPATHS="$XLIBPATHS -L$i"
done
if [ -n "$SSL_IPATH" ]; then
    XIPATHS="-I$SSL_IPATH"
fi
if [ -n "$SSL_PATH" ]; then
    XLIBS="$XLIBS -lssl"
fi
if [ -n "$CRYPTO_PATH" ]; then
    XLIBS="$XLIBS -lcrypto"
fi
if [ -n "$NSL_PATH" ]; then
    XLIBS="$XLIBS -lnsl"
fi
if [ -n "$SOCKET_PATH" ]; then
    XLIBS="$XLIBS -lsocket"
fi

echo "Writing Makefile.in ..."
echo "XDEFINES=$XDEFINES" >> Makefile.in
echo "XLIBS=$XLIBS" >> Makefile.in
echo "XLIBPATHS=$XLIBPATHS" >> Makefile.in
echo "XIPATHS=$XIPATHS" >> Makefile.in
echo >> Makefile.in

echo "Generating Makefile.amap ..."
cat Makefile.in > Makefile.amap
cat Makefile.am >> Makefile.amap

echo
#echo "now type \"make\""
