#!/bin/sh
#
# The Apache Software License, Version 1.1
#
# Copyright (c) 1999-2001 The Apache Software Foundation.  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. The end-user documentation included with the redistribution,
#    if any, must include the following acknowledgment:
#       "This product includes software developed by the
#        Apache Software Foundation (http://www.apache.org/)."
#    Alternately, this acknowledgment may appear in the software itself,
#    if and wherever such third-party acknowledgments normally appear.
#
# 4. The names "Xerces" and "Apache Software Foundation" must
#    not be used to endorse or promote products derived from this
#    software without prior written permission. For written
#    permission, please contact apache\@apache.org.
#
# 5. Products derived from this software may not be called "Apache",
#    nor may "Apache" appear in their name, without prior written
#    permission of the Apache Software Foundation.
#
# THIS SOFTWARE IS PROVIDED ``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 THE APACHE SOFTWARE FOUNDATION OR
# ITS 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.
# ====================================================================
#
# This software consists of voluntary contributions made by many
# individuals on behalf of the Apache Software Foundation, and was
# originally based on software copyright (c) 1999, International
# Business Machines, Inc., http://www.ibm.com .  For more information
# on the Apache Software Foundation, please see
# <http://www.apache.org/>.
#

#
# $Id: runConfigure,v 1.15 2002/01/18 19:48:55 tng Exp $
#

#
# runConfigure : This script will run the "configure" script for the appropriate platform
# Only supported platforms are recognized

usage()
{
    echo "runConfigure: Helper script to run \"configure\" for one of the supported platforms"
    echo "Usage: runConfigure \"options\""
    echo "       where options may be any of the following:"
    echo "       -p <platform> (accepts 'aix', 'unixware', 'linux', 'freebsd', 'solaris',
'hp-10', 'hp-11', 'os400', 'irix', 'ptx', 'tru64', 'macosx')"
    echo "       -c <C compiler name> (e.g. gcc, xlc or icc)"
    echo "       -x <C++ compiler name> (e.g. g++, xlC, or icc)"
    echo "       -d (specifies that you want to build debug version)"
    echo "       -r <thread option> can be 'pthread' or 'dce' (only used on aix, HP-11 and solaris) or 'sproc' (only on IRIX) or 'none'"
    echo "       -l <extra linker options>"
    echo "       -z <extra compiler options>"
    echo "       -h (get help on the above commands)"
}

ERROR_EXIT_CODE=1

if test ${1}o = "o"; then
   usage
   exit ${ERROR_EXIT_CODE}
fi

if test ${XERCESCROOT}o = "o"; then
   echo ERROR : You have not set your XERCESCROOT environment variable
   echo Though this environment variable has nothing to do with creating makefiles,
   echo this is just a general warning to prevent you from pitfalls in future. Please
   echo set an environment variable called XERCESCROOT to indicate where you installed
   echo the XERCES-C files, and run this command again to proceed. See the documentation
   echo for an example if you are still confused.
   exit ${ERROR_EXIT_CODE}
fi

if test $1 = "-h"; then
   usage
   exit ${ERROR_EXIT_CODE}
fi

# Check the command line parameters
if test -x /usr/bin/getopt; then
getoptErr=`getopt p:c:x:dm:n:t:r:l:z:h $*`
else
getoptErr=`getopts p:c:x:dm:n:t:r:l:z:h `$*``
fi
if [ $? != 0 ]
   then
   usage
   exit ${ERROR_EXIT_CODE}
fi

# Now get the command line parameters
if test -x /usr/bin/getopt; then
set -- `getopt p:c:x:dm:n:t:r:l:z:h $*`
else
set -- `getopts p:c:x:dm:n:t:r:l:z:h `$*``
fi

# Set up the default values for each parameter
debug=off                # by default debug is off
transcoder=native        # by default use native transcoder
msgloader=iconv          # by default use native transcoder
netaccessor=fileonly     # by default use fileonly
thread=pthread           # by default use POSIX threads
compileroptions=""
while [ $# -gt 0 ]
   do
   case $1 in
   -p)
        platform=$2; shift 2;;

   -c)
        ccompiler=$2; shift 2;;

   -x)
        cppcompiler=$2; shift 2;;

   -d)
        debug=on; shift;;

   -r)
        thread=$2; shift 2;;

   -z)
        compileroptions="$compileroptions $2"; shift 2;;

   -l)
        linkeroptions="$linkeroptions $2"; shift 2;;

   -h)
        usage
        exit ${ERROR_EXIT_CODE};;

   --)
        shift; break;;

   *)
       echo "unknown option $1"
       usage
       exit ${ERROR_EXIT_CODE};;
   esac
done

if test $platform = "aix"; then
      if test ! -n "$ccompiler" -o ! -n "$cppcompiler"; then
              ccompiler=xlc
              cppcompiler=xlC
      fi
fi

echo "Generating makefiles with the following options ..."
echo "Platform: $platform"
echo "C Compiler: $ccompiler"
echo "C++ Compiler: $cppcompiler"
echo "Extra compile options: $compileroptions"
echo "Extra link options: $linkeroptions"
echo "Thread option: $thread"

#
# Now check if the options are correct or not, bail out if incorrect
#
case $platform in
   aix | unixware | linux | freebsd | solaris | hp-10 | hp-11 | os400 | irix | ptx | tru64 | macosx)
       # platform has been recognized
       ;;
   *)
      echo "I do not recognize the platform '$platform'. Please type '${0} -h' for help."
      exit ${ERROR_EXIT_CODE};;
esac

#
# Enable debugging or not...
#
if test $debug = "off"; then
    echo "Debug is OFF"
    if test $platform = "os400"; then
    debugflag="-O";
    elif test $platform = "irix"; then
       debugflag="-w -O2";
    elif test $platform = "aix"; then
       debugflag="-w -O2";
    else
    debugflag="-w -O";
    fi
else
    echo "Debug is ON"
    debugflag="-g";
fi

#
# Check for the threading option
#
if test $thread = "none"; then
    THREADS=none
    threadingLibs=""
    threadingDefines="-DAPP_NO_THREADS -DXML_USE_NO_THREADS"
else
    THREADS=pthread
    threadingDefines="-DXML_USE_PTHREADS"
    threadingLibs="-lpthread"
    if test $platform = "hp-11"; then
        if test $thread; then
        case $thread in
           pthread)
               ;;
           dce)
               THREADS=dce ;
               threadingLibs="-lcma" ;
               threadingDefines="-D_PTHREADS_DRAFT4 -D_THREAD_SAFE -DXML_USE_DCE" ;;
           *)
               echo "I do not recognize the thread option '$thread'. Please type '${0} -h' for help." ;
               exit ${ERROR_EXIT_CODE};;
        esac
        fi
    elif test $platform = "freebsd"; then
	threadingLibs="-pthread -lc_r"
	threadingDefines="-D_THREAD_SAFE -DXML_USE_PTHREADS"
    elif test $platform = "aix"; then
        aix_version=`./config.guess`;
        echo Found host system to be $aix_version
        case $aix_version in
           *4.3*)
              ## Linkflags control the use of libpthreads on AIX
              threadingLibs="-lpthreads_compat";;
           *)
              threadingLibs="-lpthreads";;
        esac
        if test $thread; then
        case $thread in
           pthread)
               ;;
           dce)
               THREADS=dce ;
               threadingDefines="-DXML_USE_DCE"
               case $aix_version in
                  *4.3*)
                     ## Linkflags control the use of libpthreads on AIX
                     threadingLibs="-ldcelibc_r -ldcepthreads -lpthreads_compat";;
                  *)
                     threadingLibs="-lC_r -lC -ldcelibc_r -ldcepthreads";;
               esac
               ;;
           *)
               echo "I do not recognize the thread option '$thread'. Please type '${0} -h' for help." ;
               exit ${ERROR_EXIT_CODE};;
        esac
        fi
    elif test $platform = "ptx"; then
        threadingLibs=" "
        if test -z $XMLINSTALL; then
            XMLINSTALL=$ICUROOT         ## use either the -C or the -P option now.
        fi
    elif test $platform = "hp-10"; then
        THREADS=dce
        threadingLibs="-lcma"
        threadingDefines="-DXML_USE_DCE"
    elif test $platform = "unixware"; then
        threadingLibs="" ## Linkflags control the use of threads on UnixWare
    elif test $platform = "solaris"; then
        if test $thread; then
        case $thread in
           pthread)
               ;;
           dce)
               THREADS=dce ;
               threadingLibs="-ldce";
               threadingDefines="-DXML_USE_DCE";;
           *)
               echo "I do not recognize the thread option '$thread'. Please type '${0} -h' for help."
               exit ${ERROR_EXIT_CODE};;
        esac
        fi
    elif test $platform = "irix"; then
        if test $thread; then
        case $thread in
        pthread)
            ;;

        sproc)
            threadingLibs=" ";
            threadingDefines="-DXML_USE_SPROC" ;;
        *)
           echo "I do not recognize the thread option '$thread'. Please type '${0} -h' for help."
            exit ${ERROR_EXIT_CODE};;
        esac
        fi
    fi
fi

export THREADS

#
# Set the C compiler and C++ compiler environment variables
#
case $cppcompiler in
   xlC* | xlc* | g++ | c++ | cc | CC | aCC | icc | ICC | cxx)
      ;;

   *)
      echo "I do not recognize the C++ compiler '$cppcompiler'. Continuing anyway ..."
      ;;
esac

CC="$ccompiler"
export CC

CXX="$cppcompiler"
export CXX

# Special test for libiconv necessity under FreeBSD
#
transcodingLibs=""       # by default don't suppose the libiconv is necessary
transcodingDefines=""
if test $platform = "freebsd"; then
    if test -n "${XERCESCROOT}"; then
       # try lookup the configuration results
       . "${XERCESCROOT}/version.incl"
       if test -f "${XERCESCROOT}/src/config.log" ; then
           if grep XML_USE_LIBICONV "${XERCESCROOT}/src/config.log" \
              > /dev/null 2>&1 ; then
               transcodingLibs=" -L/usr/local/lib -liconv "
               transcodingDefines=" -DXML_USE_LIBICONV -I/usr/local/include "
           fi
       elif test -f "${XERCESCROOT}/obj/IconvFBSDTransService.o" ; then
           if nm "${XERCESCROOT}/obj/IconvFBSDTransService.o" | \
              grep XML_USE_LIBICONV > /dev/null 2>&1 ; then
               transcodingLibs=" -L/usr/local/lib -liconv "
               transcodingDefines=" -DXML_USE_LIBICONV -I/usr/local/include "
           fi
       elif test -f "${XERCESCROOT}/lib/libxerces-c${VER}.so" ; then
           if nm "${XERCESCROOT}/lib/libxerces-c${VER}.so" | \
              grep XML_USE_LIBICONV > /dev/null 2>&1 ; then
               transcodingLibs=" -L/usr/local/lib -liconv "
               transcodingDefines=" -DXML_USE_LIBICONV -I/usr/local/include "
           fi
       fi
    fi
fi

#
# Set the extra C and C++ compiler flags
#
# include the user defined CXXFLAGS/CFLAGS first in case they have
# set an platform spefic flags
#
CXXFLAGS="$CXXFLAGS $compileroptions $debugflag $threadingDefines $transcodingDefines"; export CXXFLAGS
CFLAGS="$CFLAGS $compileroptions $debugflag $threadingDefines $transcodingDefines"; export CFLAGS

# gcc crashes if optimisation is turned on in a Tru64 environment
if [ $platform = "tru64" -a $CXX = "g++" ]; then
    CXXFLAGS=`echo $CXXFLAGS | sed -e 's/-O[0-9]*//g'`
    CFLAGS=`echo $CFLAGS | sed -e 's/-O[0-9]*//g'`
    export CXXFLAGS CFLAGS
fi

LDFLAGS="$LDFLAGS $linkeroptions"; export LDFLAGS
EXTRA_LIBS="$threadingLibs $transcodingLibs"; export EXTRA_LIBS

echo
rm -f config.cache
rm -f config.log
rm -f config.status
if test $platform = "os400"; then
./configure --host AS400-OS400
elif test $platform = "ptx"; then
./configure --prefix=$XMLINSTALL
else
./configure
fi
# exit if configure failed
if test $? != 0; then
  exit 1
fi

echo
echo In future, you may also directly type the following commands to create the Makefiles.
echo
echo export THREADS=\"$THREADS\"
echo export CC=$CC
echo export CXX=$CXX
echo export CXXFLAGS=$CXXFLAGS
echo export CFLAGS=$CFLAGS
echo export EXTRA_LIBS=$EXTRA_LIBS
echo export LDFLAGS=$LDFLAGS
echo configure

echo
echo If the result of the above commands look OK to you, go to the directory
echo ${XERCESCROOT}/tests and type \"gmake\" to make the test cases.

exit 0;

