#!/bin/bash
#
# Copyright (c) Members of the EGEE Collaboration. 2006-2010.
# See http://public.eu-egee.org/partners/ for details on
# the copyright holders.
# For license conditions see the license file or
# http://www.apache.org/licenses/LICENSE-2.0
#
# Authors:
#      Akos Frohner <Akos.Frohner@cern.ch>
#      Jean-Philippe Baud <Jean-Philippe.Baud@cern.ch>
#

basedir=`dirname $0`

USAGE="configure --with-version=...  --with-release=... --with-globus-prefix=...  --with-globus-thr-flavor=...  --with-globus-nothr-flavor=...  --with-gsoap-location=...  --with-gsoap-version=...  --with-voms-location=... --with-cpp-libs --with-libdir=... --with-emi"

echo "$@" >${basedir}/config.status

# some sensible defaults in RedHat style
if [ $(uname -m) = 'x86_64' ]; then
    GLOBUS_FLAVOUR=gcc64dbg
    GLOBUS_FLAVOUR_PTHR=gcc64dbgpthr
    LIBDIR=lib64
else
    GLOBUS_FLAVOUR=gcc32dbg
    GLOBUS_FLAVOUR_PTHR=gcc32dbgpthr
    LIBDIR=lib
fi
GSOAP_VERSION=2.7.6b
source ${basedir}/VERSION

while test $# -ne 0; do
    case "$1" in
        -h|--help)
            echo $USAGE
            exit
            ;;
        --with-version*)
            if [ '--with-version' = "$1" ]; then
                VERSION="$1"
                shift
            else
                VERSION=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;  
        --with-release*)
            if [ '--with-release' = "$1" ]; then
                RELEASE="$1"
                shift
            else
                RELEASE=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;  
        --with-globus-prefix*)
            if [ '--with-globus-prefix' = "$1" ]; then
                GLOBUS_LOCATION="$1"
                shift
            else
                GLOBUS_LOCATION=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;  
        --with-globus-thr-flavor*)
            if [ '--with-globus-thr-flavor' = "$1" ]; then
                GLOBUS_FLAVOUR_PTHR="$1"
                shift
            else
                GLOBUS_FLAVOUR_PTHR=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;  
        --with-globus-nothr-flavor*)
            if [ '--with-globus-nothr-flavor' = "$1" ]; then
                GLOBUS_FLAVOUR="$1"
                shift
            else
                GLOBUS_FLAVOUR=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;; 
        --with-gsoap-location*)
            if [ '--with-gsoap-location' = "$1" ]; then
                GSOAP_LOCATION="$1"
                shift
            else
                GSOAP_LOCATION=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;
        --with-gsoap-version*)
            if [ '--with-gsoap-version' = "$1" ]; then
                GSOAP_VERSION="$1"
                shift
            else
                GSOAP_VERSION=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;  
        --with-voms-location*)
            if [ '--with-voms-location' = "$1" ]; then
                VOMS_LOCATION="$1"
                shift
            else
                VOMS_LOCATION=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;
        --with-cpp-libs)
            shift
            WITH_CPP_LIBS='yes'
            ;;
        --with-libdir*)
            if [ '--with-libdir' = "$1" ]; then
                LIBDIR="$1"
                shift
            else
                LIBDIR=`echo $1 | cut -d= -f 2`
            fi
            shift
            ;;
        --with-emi)
            shift
            WITH_EMI='yes'
            ;;
        --*)
            echo "Unknown option '$1'"
            shift
            break
            ;;
        *)
            echo "Unknown parameter '$1'"
            exit 2
            ;;
    esac
done

GSOAP_MAJOR_MINOR_VERSION=$(echo ${GSOAP_VERSION} | cut -d . -f1,2)

cat >Makefile <<EOF
SRCDIR=${basedir}/src
VERSION=${VERSION}

GSOAP_LOCATION=${GSOAP_LOCATION}
GSOAP_VERSION=_gsoap_${GSOAP_MAJOR_MINOR_VERSION}
GLOBUS_LOCATION=${GLOBUS_LOCATION}
GLOBUS_FLAVOUR=${GLOBUS_FLAVOUR}
GLOBUS_FLAVOUR_PTHR=${GLOBUS_FLAVOUR_PTHR}
USE_VOMS=yes
VOMS_LOCATION=${VOMS_LOCATION}
WITH_CPP_LIBS=${WITH_CPP_LIBS}
LIBDIR=${LIBDIR}
WITH_EMI=${WITH_EMI}

include \$(SRCDIR)/Makefile
EOF
