#!/bin/sh
#
# Configures to build the Qt library
#
# Copyright 1999-2002 Trolltech AS.  All rights reserved.
#


#-------------------------------------------------------------------------------
# script initialization
#-------------------------------------------------------------------------------

relconf=`basename $0`
# the directory the script was launched from is the "source tree"
relpath=`dirname $0`
relpath=`(cd $relpath; /bin/pwd)`
# the current directory is the "build tree" or "object tree"
outpath=`/bin/pwd`

# initialize global variables
OPT_CMDLINE=`echo $@ | sed "s,-v,,g"` # cache the commandline for config.status
QMAKE_SWITCHES=
QMAKE_VARS=
QMAKE_CONFIG=

# initialize platform detection
X11_PLATFORM=no
MAC_PLATFORM=no
[ -d /System/Library/Frameworks/Carbon.framework ] && MAC_PLATFORM=yes
QWS_PLATFORM=no
[ "$MAC_PLATFORM" = "no" ] && [ -d mkspecs/qws ] && QWS_PLATFORM=maybe


#-------------------------------------------------------------------------------
# licensing
#-------------------------------------------------------------------------------

# Free edition - may only be used under the terms of the QPL or GPL.
if [ -f $relpath/LICENSE.QPL -o -f $relpath/LICENSE.GPL ]; then
    [ "$QWS_PLATFORM" = "maybe" ] && QWS_PLATFORM=yes
    Edition="free"
    Licensee="Non-Commercial"
    Products="qt-free"
# Evaluation version.
elif [ -f $relpath/LICENSE.EVAL ]; then
    [ "$QWS_PLATFORM" = "maybe" ] && QWS_PLATFORM=yes
    Edition="eval"
    Licensee="Evaluation"
    Products="qt-eval"
# Internal Trolltech edition.
elif [ -f $relpath/LICENSE.TROLL ]; then
    Edition="troll"
    Licensee="Trolltech"
    Products="qt-internal"
# Commercial edition - needs a license.
else
    if [ -f $relpath/.LICENSE ]; then
	echo
	[ "$QWS_PLATFORM" = "maybe" ] && QWS_PLATFORM=yes
	Desc=""
	if [ -f $HOME/.qt-license ]; then
	    . $HOME/.qt-license >/dev/null 2>&1
	    if [ -z "$LicenseKey" ]; then
		echo
		echo "You are using an old license file."
		echo
		echo "Please install the license file supplied by Trolltech,"
		echo "or install the Qt Free Edition if you intend to develop"
		echo "free software."
		exit 1
	    fi
	else
	    if [ -z "$LicenseKey" ]; then
		echo
		echo "Please enter your license key"
		read LicenseKey
	    fi
	fi
	if echo $LicenseKey | grep "....-....-...." >/dev/null 2>&1; then
	    # get the first character of the key
	    keyTest=`echo $LicenseKey | cut -b1`
	    if [ "$QWS_PLATFORM" = "yes" ]; then
		# we have to test for Qt/Embedded
	        case $keyTest in
		A|B|C|D|E|F|G|H|a|b|c|d|e|f|g|h)
		    # Feature_Embedded
		    echo ;;
		*)
		    echo
		    echo "You are not licensed for the Qt/Embedded platform."
		    echo
		    echo "Please contact sales@trolltech.com to upgrade your license"
		    echo "to include the Qt/Embedded platform, or install the"
		    echo "Qt Free Edition if you intend to develop free software."
		    exit 1 ;;
		esac
	    elif [ "$MAC_PLATFORM" = "yes" ]; then
	        case $keyTest in
		*) ###
		    # Feature_Mac
		    echo ;;
		*)
		    echo
		    echo "You are not licensed for the Mac OS X platform."
		    echo
		    echo "Please contact sales@trolltech.com to upgrade your license"
		    echo "to include the Mac OS X platform."
		    exit 1 ;;
                esac
            else
	        case $keyTest in
		6|7|8|9|E|F|G|H|e|f|g|h)
		    # Feature_Unix
		    echo ;;
		*)
		    echo
		    echo "You are not licensed for the X11 platform."
		    echo
		    echo "Please contact sales@trolltech.com to upgrade your license"
		    echo "to include the X11 platform, or install the Qt Free Edition"
		    echo "if you intend to develop free software."
		    exit 1 ;;
                esac
	    fi
	    case $keyTest in
	    4|5|8|9|C|D|G|H|c|d|g|h)
		# Feature_Enterprise
		Desc="Enterprise"
		Edition="ent"
		;;
	    *)
		Products=qt-professional
		Desc="Professional"
		Edition="pro"
		;;
	    esac
	    case $keyTest in
	    3|5|7|9|B|D|F|H|b|d|f|h)
		# Feature_US
		cp -f $relpath/.LICENSE-US $relpath/LICENSE
		;;
	    *)
		cp -f $relpath/.LICENSE $relpath/LICENSE
		;;
	    esac
	else
	    echo
	    echo "Invalid license key. Please check the license key."
	    exit 1
	fi
    else
	echo "The LICENSE, LICENSE.QPL, or LICENSE.GPL file shipped with"
	echo "this software has disappeared."
	echo
	echo "Sorry, you are not licensed to use this software."
	echo "Try re-installing."
	echo
	exit 0
    fi
fi

# check licensed modules
MODULES="styles tools kernel widgets dialogs iconview workspace"
[ "$Products" != "qt-professional" ] && MODULES="$MODULES network canvas table xml opengl sql"
AVAIL_MODULES=$MODULES
QMAKE_VARS="$QMAKE_VARS \"QT_PRODUCT=$Products\""
QMAKE_VARS="$QMAKE_VARS \"styles += windows motif aqua platinum sgi cde motifplus\""


#-------------------------------------------------------------------------------
# parse script arguments
#-------------------------------------------------------------------------------

# initalize internal variables
CFG_LAZY_DEPS_ALLOWED=no
CFG_GPLUSPLUS_EXCEPTIONS=unspecified
CFG_INCREMENTAL=auto
CFG_QCONFIG=full
if [ "$QWS_PLATFORM" = "yes" ]; then
    EMBEDDED=auto
else
    EMBEDDED=no
fi
CFG_DEBUG=no
CFG_SHARED=yes
CFG_GIF=no
CFG_THREAD=auto
CFG_SM=auto
CFG_XINERAMA=auto
CFG_BIG_CODECS=yes
CFG_ZLIB=yes
CFG_PNG=yes
CFG_LIBPNG=qt
CFG_JPEG=auto
CFG_LIBJPEG=qt
CFG_MNG=auto
CFG_LIBMNG=qt
CFG_XRENDER=auto
CFG_FREETYPE=auto
CFG_QWS_FREETYPE=yes
CFG_SQL_AVAILABLE=
CFG_SQL_AUTODETECTED=
CFG_TABLET=auto
CFG_XKB=auto
CFG_NIS=no
CFG_STL=auto
CFG_NAS=no
CFG_REMOTE=no
CFG_QWS_QVFB=prompted
CFG_QWS_DEPTHS=prompted
D_FLAGS=
I_FLAGS=
L_FLAGS=
R_FLAGS=
l_FLAGS=
XPLATFORM=
PLATFORM=$QMAKESPEC
QMAKE_PROJECTS=
QMAKE_IGNORE_PROJECTS=
OPT_CONCURRENT=0
OPT_SHADOW=maybe
OPT_VERBOSE=no
OPT_HELP=

# initalize variables used for installation
QT_INSTALL_PREFIX=/usr/local/qt
QT_INSTALL_DOCS=
QT_INSTALL_HEADERS=
QT_INSTALL_LIBS=
QT_INSTALL_BINS=
QT_INSTALL_PLUGINS=
QT_INSTALL_DATA=

# parse the arguments, setting things to "yes" or "no"
while [ -n "$1" ]; do
    CURRENT_OPT="$1"
    case $1 in
    #Autoconf style options
    --enable-*|--disable-*)
	VAR=`echo $1 | sed "s,^--.*-\(.*\),\1,"`
	VAL=yes
	[ `echo $1 | sed "s,^--\(.*\)-.*,\1,"` = "disable" ] && VAL=no
	;;
    --*=*)
	VAR=`echo $1 | sed "s,^--\(.*\)=.*,\1,"`
	VAL=`echo $1 | sed "s,^--.*=\(.*\),\1,"`
	;;
    --*)
	VAR=`echo $1 | sed "s,^--\(.*\),\1,"`
	VAL=yes
	;;
    #Qt plugin options
    -no-*-*|-plugin-*-*|-qt-*-*)
	VAR=`echo $1 | sed "s,^-[^-]*-\(.*\),\1,"`
	VAL=`echo $1 | sed "s,^-\([^-]*\).*,\1,"`
	;;
    #Qt style no options
    -no-*)
	VAR=`echo $1 | sed "s,^-no-\(.*\),\1,"`
	VAL=no
	;;
    #Qt style yes options
    -incremental|-accel-*|-repeater|-qvfb|-vnc|-profile|-shared|-static|-sm|-thread|-xinerama|-tablet|-stl|-freetype|-big-codecs|-xrender|-xft|-xkb|-nis|-remote|-internal|-h|-help|-v|-verbose|-debug|-release)
	VAR=`echo $1 | sed "s,^-\(.*\),\1,"`
	VAL=yes
	;;
    #Qt style options that pass an argument
    -prefix|-docdir|-headerdir|-plugindir|-datadir|-libdir|-bindir|-embedded|-depths|-make|-nomake|-qconfig|-platform|-xplatform)
	VAR=`echo $1 | sed "s,^-\(.*\),\1,"`
	shift
	VAL=$1
	;;
    #Qt style complex options in one command
    -enable-*|-disable-*)
	VAR=`echo $1 | sed "s,^-\(.*\)-.*,\1,"`
	VAL=`echo $1 | sed "s,^-.*-\(.*\),\1,"`
	;;
    #Qt Builtin/System style options
    -qt-*|-system-*|-no-*)
	VAR=`echo $1 | sed "s,^-.*-\(.*\),\1,"`
	VAL=`echo $1 | sed "s,^-\(.*\)-.*,\1,"`
	;;
    #Options that cannot be generalized
    -j?*)
	VAR="concurrent"
	VAL="`echo $1 | sed 's,-j,,'`"
	;;
    -D?*|-D)
	VAR="define"
	if [ "$1" = "-D" ]; then
	    shift
	    VAL="$1"
	else
	    VAL=`echo $1 | sed 's,-D,,'`
        fi
	;;
    -I?*|-I)
	VAR="ipath"
	if [ "$1" = "-I" ]; then
	    shift
	    VAL="$1"
	else
	    VAL=`echo $1 | sed 's,-I,,'`
        fi
	;;
    -L?*|-L)
	VAR="lpath"
	if [ "$1" = "-L" ]; then
	    shift
	    VAL="$1"
	else
	    VAL=`echo $1 | sed 's,-L,,'`
        fi
	;;
    -R?*|-R)
	VAR="rpath"
	if [ "$1" = "-R" ]; then
	    shift
	    VAL="$1"
	else
	    VAL=`echo $1 | sed 's,-R,,'`
        fi
	;;
    -l?*)
	VAR="link"
	VAL=`echo $1 | sed 's,-l,,'`
	;;
    *)
	echo "${1}: unknown argument"
	OPT_HELP=yes
	ERROR=yes
	shift
	continue
    esac
    shift #advance

    UNKNOWN_OPT=no
    case $VAR in
    lazydeps)
	CFG_LAZY_DEPS_ALLOWED="$VAL"
	;;
    prefix)
	QT_INSTALL_PREFIX="$VAL"
	;;
    docdir)
	QT_INSTALL_DOCS="$VAL"
	;;
    headerdir)
	QT_INSTALL_HEADERS="$VAL"
	;;
    plugindir)
	QT_INSTALL_PLUGINS="$VAL"
	;;
    datadir)
	QT_INSTALL_DATA="$VAL"
	;;
    libdir)
	QT_INSTALL_LIBS="$VAL"
	;;
    bindir)
	QT_INSTALL_BINS="$VAL"
	;;
    embedded)
	if [ "$QWS_PLATFORM" != "no" ]; then
	    QWS_PLATFORM=yes
	    EMBEDDED="$VAL"
	    if [ $EMBEDDED = "qnx" ]; then
		PLATFORM=qws/qnx-rtp-g++
		XPLATFORM=qws/qnx-rtp-g++
		CFG_STL=no
		CFG_QWS_QVFB=no
	    else
		PLATFORM=qws/linux-generic-g++
		XPLATFORM=qws/linux-${EMBEDDED}-g++;
	    fi
	else
	    echo "No license exists to enable Embedded. Disabling."
	fi
	;;
    depths)
	CFG_QWS_DEPTHS="$VAL"
	;;
    incremental)
	CFG_INCREMENTAL="$VAL"
	;;
    accel-voodoo3)
	CFG_QWS_VOODOO3="$VAL"
	;;
    accel-mach64)
	CFG_QWS_MACH64="$VAL"
	;;
    repeater)
	CFG_QWS_REPEATER="$VAL"
	;;
    accel-matrox)
	CFG_QWS_MATROX="$VAL"
	;;
    qvfb)
	CFG_QWS_QVFB="$VAL"
	;;
    vnc)
	CFG_QWS_VNC="$VAL"
	;;
    qconfig)
	CFG_QCONFIG="$VAL"
	;;
    nomake)
	QMAKE_IGNORE_PROJECTS="$QMAKE_IGNORE_PROJECTS $VAL"
	;;
    make)
	if [ -f $relpath/$VAL ]; then
	    QMAKE_PROJECTS="$QMAKE_PROJECTS $relpath/$VAL"
	else
	    if [ -d $relpath/$VAL ]; then
		QMAKE_PROJECTS="$QMAKE_PROJECTS `find $relpath/$VAL -name '*.pro' -print`"
	    else
		QMAKE_PROJECTS="$QMAKE_PROJECTS `find $relpath/. -name '*.pro' -print`"
	    fi
	fi
	;;
    profile)
	if [ "$VAL" = "yes" ]; then
	    QMAKE_VARS="$QMAKE_VARS QMAKE_CFLAGS+=-pg QMAKE_CXXFLAGS+=-pg"
	    QMAKE_VARS="$QMAKE_VARS QMAKE_LFLAGS+=-pg"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    g++-exceptions|exceptions)
	if [ "$VAL" = "no" ]; then
	    CFG_GPLUSPLUS_EXCEPTIONS=no
	    QMAKE_VARS="$QMAKE_VARS \"QMAKE_CFLAGS+=-fno-exceptions\""
	    QMAKE_VARS="$QMAKE_VARS \"QMAKE_CXXFLAGS+=-fno-exceptions\""
	    QMAKE_VARS="$QMAKE_VARS \"QMAKE_LFLAGS+=-fno-exceptions\""
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    platform)
	PLATFORM="$VAL"
	;;
    xplatform)
	XPLATFORM="$VAL"
	;;
    release)
	if [ "$VAL" = "yes" ]; then
	    CFG_DEBUG=no
	elif [ "$VAL" = "no" ]; then
	    CFG_DEBUG=yes
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    debug)
	CFG_DEBUG="$VAL"
	;;
    static)
	if [ "$VAL" = "yes" ]; then
	    CFG_SHARED=no
	elif [ "$VAL" = "no" ]; then
	    CFG_SHARED=yes
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    shared)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_SHARED="$VAL"
	else
	    UNKNOWN_OPT=yes
        fi
	;;
    gif)
	[ "$VAL" = "qt" ] && VAL=yes
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_GIF="$VAL"
	else
	    UNKNOWN_OPT=yes
        fi
	;;
    sm)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_SM="$VAL"
	else
	    UNKNOWN_OPT=yes
        fi

	;;
    thread)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_THREAD="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    xinerama)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_XINERAMA="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    tablet)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_TABLET="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    stl)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_STL="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    freetype)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_QWS_FREETYPE="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    zlib)
	[ "$VAL" = "qt" ] && VAL=yes
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ] || [ "$VAL" = "system" ]; then
	    CFG_ZLIB="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	[ "$VAL" = "no" ] && CFG_LIBPNG=no
	;;
    libpng)
	[ "$VAL" = "yes" ] && VAL=qt
	if [ "$VAL" = "no " ]; then #left for commandline compatibility, not documented
	    echo "-no-png is left for compatibility, in the future please use -no-imgfmt-png"
	    CFG_PNG=no
	fi
	if [ "$VAL" = "qt" ] || [ "$VAL" = "no" ] || [ "$VAL" = "system" ]; then
	    CFG_LIBPNG="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    libmng)
	[ "$VAL" = "yes" ] && VAL=qt
	if [ "$VAL" = "no " ]; then #left for commandline compatibility, not documented
	    echo "-no-mng is left for compatibility, in the future please use -no-imgfmt-mng"
	    CFG_MNG=no
	fi
	if [ "$VAL" = "qt" ] || [ "$VAL" = "no" ] || [ "$VAL" = "system" ]; then
	    CFG_LIBMNG="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    jpeg|libjpeg)
	[ "$VAL" = "yes" ] && VAL=qt
	if [ "$VAR" = "jpeg" ]; then
	    echo "-${VAL}-jpeg is left for compatibility, in the future please use -${VAL}-libjpeg"
	fi
	if [ "$VAL" = "qt" ] || [ "$VAL" = "no" ] || [ "$VAL" = "system" ]; then
	    CFG_LIBJPEG="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	if [ "$VAL" = "no " ]; then #left for commandline compatibility, not documented
	    echo "-no-jpeg is left for compatibility, in the future please use -no-imgfmt-jpeg"
	    CFG_JPEG=no
	fi
	;;
    nas-sound)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_NAS="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    big-codecs)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_BIG_CODECS="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    xrender)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_XRENDER="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    xft)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_FREETYPE="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    xkb)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_XKB="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    nis)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_NIS="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    remote)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    CFG_REMOTE="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    enable) #left for commandline compatibility, not documented
	MODULE="$VAL"
	if [ -n $MODULE -a -d "$relpath/src/$MODULE" ]; then
	    if echo "${MODULES}" | grep $MODULE >/dev/null 2>&1; then
		MODULES="$MODULES $MODULE"
	    fi
	fi
	;;
    disable)
	MODULE="$VAL"
	if [ -d "$relpath/src/$MODULE" ]; then
	    MODULES=`echo $MODULES | sed -e "s/$MODULE//"`
	else
	    echo "Unknown module disabled: $MODULE"
	fi
	;;
    internal)
	[ "$VAL" = "yes" ] && QMAKE_CONFIG="$QMAKE_CONFIG internal"
	;;
    h|help)
	if [ "$VAL" = "yes" ]; then
	    OPT_HELP="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	;;
    imgfmt-*|style-*|sql-*)
	[ "$VAL" = "yes" ] && VAL=qt
	if [ "$VAL" != "no" ] && [ "$VAL" != "qt" ] && [ "$VAL" != "plugin" ]; then
	    UNKNOWN_OPT=yes
	fi
	OPT="$VAL"
	VAL=`echo $VAR | sed "s,^[^-]*-\([^-]*\).*,\1,"`
	VAR=`echo $VAR | sed "s,^\([^-]*\).*,\1,"`
	if [ "$VAR" = "imgfmt" ]; then
	    [ "$OPT" = "qt" ] && OPT=yes
	    if [ "$OPT" = "no" ] || [ "$OPT" = "yes" ] || [ "$OPT" = "plugin" ]; then
	        case "$VAL" in
	        jpeg) CFG_JPEG=$OPT ;;
	        mng) CFG_MNG=$OPT ;;
	        png) CFG_PNG=$OPT ;;
	        *) ERROR=yes ;;
	        esac
            else
	        ERROR=yes
	    fi
	elif [ "$OPT" = "plugin" ] || [ "$OPT" = "qt" ]; then
	   if [ "$OPT" = "plugin" ]; then
	      [ "$VAR" = "style" ] && QMAKE_VARS="$QMAKE_VARS \"${VAR}s -= $VAL\""
	      VAR="${VAR}-${OPT}"
	   elif [ "$VAR" = "sql" ]; then
	      VAR="${VAR}-driver"
	   fi
	   QMAKE_VARS="$QMAKE_VARS \"${VAR}s += $VAL\""
	elif [ "$OPT" = "no" ]; then
	   PLUG_VAR="${VAR}-plugin"
	   IN_VAR="${VAR}"
	   [ "$IN_VAR" = "sql" ] && IN_VAR="${IN_VAR}-driver"
	   QMAKE_VARS="$QMAKE_VARS \"${IN_VAR}s -= $VAL\" \"${PLUG_VAR}s -= $VAL\""
        fi
        if [ "$ERROR" = "yes" ]; then
	   echo "$1: unknown argument"
	   OPT_HELP=yes;
	fi
	;;
    v|verbose)
	if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
	    OPT_VERBOSE="$VAL"
	else
	    UNKNOWN_OPT=yes
	fi
	# takes two verboses to turn on qmake debugs
	[ "$OPT_VERBOSE" = "yes" ] && QMAKE_SWITCHES="$QMAKE_SWITCHES -d"
	;;
    concurrent)
	OPT_CONCURRENT="$VAL"
	;;
    define)
	D_FLAGS="$D_FLAGS $VAL"
	;;
    ipath)
	I_FLAGS="$I_FLAGS -I${VAL}"
	;;
    lpath)
	L_FLAGS="$L_FLAGS -L${VAL}"
	;;
    rpath)
	R_FLAGS="$R_FLAGS \$\${QMAKE_RPATH}${VAL}"
	;;
    link)
	l_FLAGS="$l_FLAGS -l${VAL}"
	;;
    *)
	UNKNOWN_OPT=yes
	;;
    esac
    if [ "$UNKNOWN_OPT" = "yes" ]; then
	echo "${CURRENT_OPT}: invalid command-line switch"
	OPT_HELP=yes
	ERROR=yes
    fi
done


#-------------------------------------------------------------------------------
# build tree initialization
#-------------------------------------------------------------------------------

# skip this if the user just needs help...
if [ "$OPT_HELP" != "yes" ]; then

# create the include directory (will hold real qconfig.h and qmodules.h)
mkdir -p $outpath/include

# create temporary qconfig.h for compiling qmake
# the original file will be restored after qmake is built
if [ -f $outpath/include/qconfig.h ]; then
    mv -f $outpath/include/qconfig.h qconfig.h-hold
fi
echo "// All features enabled while configuring" >$outpath/include/qconfig.h

# create temporary qmodules.h for compiling qmake
# the original file will be restored after qmake is built
if [ -f $outpath/include/qmodules.h ]; then
    mv -f $outpath/include/qmodules.h qmodules.h-hold
fi
echo "// All modules enabled while configuring" >$outpath/include/qmodules.h

# make sure qconfig.h and qmodules.h are restored if Ctrl+C
# - using 130 as an exit value because on bash the return value of a
#   simple command is 128+n if the command is terminated by signal n
#   and Ctrl+C results in SIGINT which on Linux is 2
if [ "$MAC_PLATFORM" = "no" ]; then
trap "\
[ -r qconfig.h-hold ] && mv -f $outpath/qconfig.h-hold $outpath/include/qconfig.h ;\
[ -r qmodules.h-hold ] && mv -f $outpath/qmodules.h-hold $outpath/include/qmodules.h ;\
exit 130" HUP INT TERM
fi

# is this a shadow build?
if [ "$OPT_SHADOW" = "maybe" ]; then
    OPT_SHADOW=no
    if [ "$relpath" != "$outpath" ] && [ '!' -e "$outpath/configure" ]; then
	if [ -L "$outpath" ]; then
	    [ "$relpath" -ef "$outpath" ] || OPT_SHADOW=yes
	else
	    OPT_SHADOW=yes
        fi
    fi
fi
if [ "$OPT_VERBOSE" = "yes" -a "$OPT_SHADOW" = "yes" ]; then
    echo "Performing shadow build..."
fi

# we may write in the build tree, let's take advantage of this
# to test that QTDIR is correctly set to be the build tree
touch .test.qt.
if [ '!' -f ${QTDIR}/.test.qt. ]; then
    rm -f .test.qt.
    echo
    echo
    echo '   The environment variable $QTDIR is not set correctly. It is currently'
    echo '   set to "'$QTDIR'", but it should be set to this directory,'
    echo '   which is "'`/bin/pwd`'".'
    echo
    echo '   Please read the INSTALL file for instructions on how to set $QTDIR'
    echo '   correctly. If you have set $QTDIR in your .profile or .login, you '
    echo '   will need to log out and log in again to make the setting effective.'
    echo
    echo
    exit 1
fi
rm -f .test.qt.

# if the source tree is different from the build tree,
# symlink or copy part of the sources
if [ "$OPT_SHADOW" = "yes" ]; then
    echo "Preparing build tree..."
    mkdir -p $outpath/bin

    # need a top level makefile, prefer gnu internally
    if [ -f $relpath/GNUmakefile ]; then
	rm -f $outpath/Makefile
	ln -s $relpath/GNUmakefile $outpath/Makefile
    elif [ -f $relpath/Makefile ]; then
	rm -f $outpath/Makefile
	ln -s $relpath/Makefile $outpath/Makefile
    else
	echo "No top level Makefile." >&2
	exit 1
    fi

    # symlink the qmake directory
    for a in `find $relpath/qmake`; do
	my_a=`echo $a | sed "s,^${relpath}/,${outpath}/,"`
	if [ '!' -f $my_a ]; then
	    if [ -d $a ]; then
		# directories are created...
		mkdir -p $my_a
	    else
		a_dir=`dirname $my_a`
		[ -d $a_dir ] || mkdir -p $a_dir
		# ... and files are symlinked
		case `basename $a` in
		*.o|*.d|GNUmakefile*|qmake)
		    ;;
		*)
		    rm -f $my_a
		    ln -s $a $my_a
		    ;;
		esac
	    fi
	fi
    done

    # symlink the mkspecs directory
    rm -f $outpath/mkspecs
    ln -s $relpath/mkspecs $outpath/mkspecs

    # symlink the linguist/doc directory (to make the assistant happy)
    mkdir -p $outpath/tools/linguist
    rm -f $outpath/tools/linguist/doc
    ln -s $relpath/tools/linguist/doc $outpath/tools/linguist/doc

    # symlink the designer/templates directory (to make the designer find
    # the templates)
    mkdir -p $outpath/tools/designer
    rm -f $outpath/tools/designer/templates
    ln -s $relpath/tools/designer/templates $outpath/tools/designer/templates

    # symlink the doc directory
    rm -f $outpath/doc
    ln -s $relpath/doc $outpath/doc

    # binaries from qt
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_MOC=$outpath/bin/moc\""
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_UIC=$outpath/bin/uic\""
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_QMAKE=$outpath/bin/qmake\""
    # hacky src line
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_MOC_SRC=$relpath/src/moc\""
else
    # override the Windows Makefile
    if [ -f "$relpath/GNUmakefile" ]; then
	rm -f $outpath/Makefile
	cp $relpath/GNUmakefile $outpath/Makefile
    fi
fi

# find a make command
if ( gmake /dev/null ) >/dev/null 2>&1; then
    MAKE=gmake
else
    if ( make /dev/null ) >/dev/null 2>&1; then
	MAKE=make
    else
	echo "You don't seem to have 'make' or 'gmake' in your PATH." >&2
	echo "Cannot proceed." >&2
	exit 1
    fi
fi

# remove existing configuration cache
[ -f $outpath/.qmake.cache ] && rm -f $outpath/.qmake.cache

fi ### help


#-------------------------------------------------------------------------------
# auto-detect all that hasn't been specified in the arguments
#-------------------------------------------------------------------------------

if [ "$EMBEDDED" = "auto" ]; then
    UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
    case "$UNAME_MACHINE" in
	x86pc) ARCH=qnx ;;
	*86) ARCH=x86 ;;
	*) ARCH=generic ;;
    esac
    EMBEDDED=$ARCH
	if [ $EMBEDDED = qnx ]; then
		PLATFORM=qws/qnx-rtp-g++
		XPLATFORM=qws/qnx-rtp-g++
		CFG_STL=no
		CFG_QWS_QVFB=no
	else
		PLATFORM=qws/linux-generic-g++
		XPLATFORM=qws/linux-${EMBEDDED}-g++
	fi
fi

if [ "$CFG_JPEG" = "auto" ]; then
    CFG_JPEG=plugin
    [ "$EMBEDDED" = "no" ] || CFG_JPEG=no
fi

if [ "$CFG_MNG" = "auto" ]; then
    CFG_MNG=plugin
    [ "$EMBEDDED" = "no" ] || CFG_MNG=no
fi


if [ -z "$PLATFORM" ]; then
    UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
    UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
    UNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown
    UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown

    ARCH=""
    if echo $QMAKE_CONFIG | grep embedded >/dev/null 2>&1; then
	# this is a Qt/Embedded distribution
	CFG_SM=no
	MODULES=`echo $MODULES | sed -e 's/opengl//'`
	PLATFORMS=`find $relpath/mkspecs/qws | sed "s,$relpath/mkspecs/qws/,,"`
	case "$UNAME_MACHINE" in
	*86) ARCH=x86- ;;
	*) ARCH=generic- ;;
	esac
    else
	PLATFORMS=`find $relpath/mkspecs/ -type f | grep -v qws | sed "s,$relpath/mkspecs/qws/,,"`
    fi

    PLATFORM_NOTES=""

    case "$UNAME_SYSTEM:$UNAME_RELEASE" in
     Darwin:*)
	PLATFORM=macx-g++
	# PLATFORM=macx-pbuilder
	PLATFORM_NOTES="
	    - Also available for Mac OS X: macx-pbuilder
	"
	;;
     AIX*)
	#PLATFORM=aix-g++
	PLATFORM=aix-xlc
	#PLATFORM=aix-64
	PLATFORM_NOTES="
	    - Also available for AIX: aix-g++
	"
	;;
     QNX:*)
	PLATFORM=qnx-g++
	;;
     GNU:*)
	PLATFORM=hurd-g++
	;;
     BSD/386:*)
	PLATFORM=bsdi-g++
	;;
     dgux:*)
	PLATFORM=dgux-g++
	;;
     ULTRIX:*)
	PLATFORM=ultrix-g++
	;;
     FreeBSD:*)
	PLATFORM=freebsd-g++
	;;
     OpenBSD:*)
	PLATFORM=openbsd-g++
	;;
     NetBSD:*)
	PLATFORM=netbsd-g++
	;;
     IRIX*)
	#PLATFORM=irix-g++
	PLATFORM=irix-n32
	#PLATFORM=irix-64
	#PLATFORM=irix-o32
	PLATFORM_NOTES="
	    - Also available for IRIX: irix-g++ irix-64 irix-o32
	"
	;;
     HP-UX:*)
	#PLATFORM=hpux-g++
	PLATFORM=hpux-acc
	#PLATFORM=hpux-n64
	#PLATFORM=hpux-cc
	#PLATFORM=hpux-o64
	PLATFORM_NOTES="
	    - Also available for HP-UX: hpux-g++ hpux-n64 hpux-cc hpux-o64
	"
	;;
     OSF1:*)
	#PLATFORM=tru64-g++
	PLATFORM=tru64-cxx
	PLATFORM_NOTES="
	    - Also available for Tru64: tru64-g++
	"
	;;
     Linux:*)
	PLATFORM=linux-${ARCH}g++
	PLATFORM_NOTES="
	    - Also available for Linux: linux-kcc linux-icc linux-cxx
	"
	;;
     SunOS:5*)
	#PLATFORM=solaris-g++
	PLATFORM=solaris-cc
	PLATFORM_NOTES="
	    - Also available for Solaris: solaris-g++ solaris-64
	"
	;;
     SunOS:4*)
	PLATFORM=sunos-g++
	;;
     OpenUNIX:*)
        PLATFORM=openunix-cc
        ;;
     UNIX_SV:4.2*)
	PLATFORM=unixware-g++
	;;
     UnixWare:5*)
	#PLATFORM=unixware7-g++
	PLATFORM=unixware7-cc
	PLATFORM_NOTES="
	    - Also available for UnixWare 7: unixware7-g++
	"
	;;
     *:3.2)
	PLATFORM=sco-g++
	;;
     *)
	if [ "$OPT_HELP" != "yes" ]; then
	    echo >&2
	    for p in $PLATFORMS; do
		echo "    $relconf $* -platform $p" >&2
	    done
	    echo >&2
	    echo "The build script does not currently recognize all platforms" >&2
	    echo "supported by Qt." >&2
	    echo "Rerun this script with a -platform option listed to" >&2
	    echo "set the operating system / compiler combination you use." >&2
	    echo >&2
	    exit 2
	fi
    esac
fi

if [ "$CFG_LAZY_DEPS_ALLOWED" = "yes" ]; then
    ${MAKE} --version | grep GNU >/dev/null 2>&1 && QMAKE_CONFIG="$QMAKE_CONFIG GNUmake"
fi

if [ "x$OPT_CONCURRENT" = "x0" ] && echo $MAKEFLAGS | grep '.* *-j[0-9]* *.*' >/dev/null 2>&1; then
    OPT_CONCURRENT=`echo $MAKEFLAGS | sed "s,.* *-j\([0-9]*\) *.*,\1,"`
fi

[ -z "$XPLATFORM" ] && XPLATFORM="$PLATFORM"
if [ -d "$PLATFORM" ]; then
  QMAKESPEC="$PLATFORM"
else
  QMAKESPEC="$relpath/mkspecs/${PLATFORM}"
fi
if [ -d "$XPLATFORM" ]; then
  XQMAKESPEC="$XPLATFORM"
else
  XQMAKESPEC="$relpath/mkspecs/${XPLATFORM}"
fi
if [ "$QMAKESPEC" = "$XQMAKESPEC" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG nocrosscompiler"
fi
if [ '!' -d "$QMAKESPEC" ]; then
    echo >&2
    echo >&2
    echo "   The specified system/compiler is not supported: $QMAKESPEC" >&2
    echo "   Please see the PLATFORMS file for a complete list." >&2
    echo >&2
    echo >&2
    exit 2
fi
if [ '!' -d "$XQMAKESPEC" ]; then
    echo >&2
    echo >&2
    echo "   The specified system/compiler is not supported: $XQMAKESPEC" >&2
    echo "   Please see the PLATFORMS file for a complete list." >&2
    echo >&2
    echo >&2
    exit 2
fi

# setup qplatformdefs.h
if [ '!' -f "${XQMAKESPEC}/qplatformdefs.h" ]; then
    echo >&2
    echo >&2
    echo "   The specified system/compiler port is not complete:" >&2
    echo >&2
    echo "       $XQMAKESPEC/qplatformdefs.h" >&2
    echo >&2
    echo "   Please contact qt-bugs@trolltech.com." >&2
    echo >&2
    echo >&2
    exit 2
fi

# now look at the configs and figure out what platform we are config'd for
[ ! -z "`grep QMAKE_LIBS_X11 $XQMAKESPEC/qmake.conf | awk '{print $3;}'`" ] && X11_PLATFORM=yes
echo "$XQMAKESPEC" | grep mkspecs/qws >/dev/null 2>&1 && QWS_PLATFORM=yes

# find default thread option for target
if grep >/dev/null '^QMAKE_LIBS_THREAD' $XQMAKESPEC/qmake.conf; then
    AUTOTHREAD=yes
    AUTOTHREAD=no  ####### leave off for now
else
    AUTOTHREAD=never
fi
# common qmake.conf file but dependency on OS version
if echo $PLATFORM | grep "hpux" >/dev/null 2>&1; then
    if echo `uname -r` | grep -E "^B\.(09|10)" >/dev/null 2>&1; then
	AUTOTHREAD=never
    fi
fi
if [ $AUTOTHREAD = never ]; then
    if [ "$CFG_THREAD" = "yes" ]; then
	echo
	echo "ERROR: Qt is not configured to support threading on this platform"
	echo "       See the THREAD settings in $XQMAKESPEC/qmake.conf"
	echo
	exit 2
    fi
    AUTOTHREAD=no
fi
[ "$CFG_THREAD" = "auto" ] && CFG_THREAD=$AUTOTHREAD


#-------------------------------------------------------------------------------
# tests that don't need qmake (must be run before displaying help)
#-------------------------------------------------------------------------------

# unix
unixtests=$relpath/config.tests/unix

# auto-detect SQL-modules support
if echo $MODULES | grep sql >/dev/null; then
    CFG_SQL_AVAILABLE=`find $relpath/plugins/src/sqldrivers/* -prune -type d -print | awk -F/ '{print $NF}'`
    CFG_SQL_AVAILABLE=`echo $CFG_SQL_AVAILABLE` ### is this really needed???
    MYSQL_REQ="-lmysqlclient mysql.h"
    OCI_REQ="-lclntsh -lwtc8 oci.h"
    PSQL_REQ="-lpq postgres.h libpq/libpq-fs.h catalog/pg_type.h libpq-fe.h"
    ODBC_REQ="-lodbc sql.h sqlext.h"
    TDS_REQ="-lsybdb sybfront.h sybdb.h"
    for _SQLDR in $CFG_SQL_AVAILABLE; do
	case $_SQLDR in
	mysql)
	    $unixtests/checkavail "MySQL" $OPT_VERBOSE $MYSQL_REQ $L_FLAGS $I_FLAGS && CFG_SQL_AUTODETECTED="$CFG_SQL_AUTODETECTED mysql"
	;;
	psql)
	    $unixtests/checkavail "PostgreSQL" $OPT_VERBOSE $PSQL_REQ $L_FLAGS $I_FLAGS && CFG_SQL_AUTODETECTED="$CFG_SQL_AUTODETECTED psql"
	    ;;
	odbc)
	    $unixtests/checkavail "ODBC" $OPT_VERBOSE $ODBC_REQ $L_FLAGS $I_FLAGS && CFG_SQL_AUTODETECTED="$CFG_SQL_AUTODETECTED odbc"
	    ;;
	oci)
	    $unixtests/checkavail "OCI" $OPT_VERBOSE $OCI_REQ $L_FLAGS $I_FLAGS && CFG_SQL_AUTODETECTED="$CFG_SQL_AUTODETECTED oci"
	    ;;
	tds)
	    $unixtests/checkavail "TDS" $OPT_VERBOSE $TDS_REQ $L_FLAGS $I_FLAGS && CFG_SQL_AUTODETECTED="$CFG_SQL_AUTODETECTED tds"
	    ;;
	*)
	    if [ "$OPT_VERBOSE" = "yes" ]; then
		echo "unknown SQL driver: $_SQLDR"
	    fi
	    ;;
	esac
    done
fi

# auto-detect NIS support
if [ "$CFG_NIS" != "no" ]; then
    CFG_NIS=yes
    $unixtests/nis.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_NIS=no
else
    CFG_NIS=no
fi

# x11
if [ "$X11_PLATFORM" = "yes" ]; then
    x11tests=$relpath/config.tests/x11

    # auto-detect OpenGL support
    if echo $MODULES | grep opengl >/dev/null; then
	$x11tests/opengl.test $XQMAKESPEC $CFG_THREAD $OPT_VERBOSE $L_FLAGS $I_FLAGS && MODULES=`echo $MODULES | sed -e 's/opengl//'`
    fi
    # auto-detect XRender support
    if [ "$CFG_XRENDER" != "no" ]; then
	CFG_XRENDER=yes
	$x11tests/xrender.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_XRENDER=no && CFG_FREETYPE=no
    else
	CFG_FREETYPE=no
    fi
    # auto-detect XftFreeType support
    if [ "$CFG_XRENDER" = "yes" ] && [ "$CFG_FREETYPE" != "no" ]; then
	CFG_FREETYPE=yes
	$x11tests/xfreetype.test $XQMAKESPEC $OPT_VERBOSE $x11tests $L_FLAGS $I_FLAGS && CFG_FREETYPE=no
    fi
    # add freetype2 include path
    if [ "$CFG_FREETYPE" = "yes" ] && [ -f $x11tests/xfreetype.inc ]; then
	QMAKE_VARS="$QMAKE_VARS \"INCLUDEPATH+=`cat $x11tests/xfreetype.inc`\""
    fi
    rm -f $x11tests/xfreetype.inc
    # add XftFreetype specific config options
    if [ "$CFG_FREETYPE" = "yes" ] && [ -f $x11tests/xfreetype.cfg ]; then
	QMAKE_CONFIG="$QMAKE_CONFIG `cat $x11tests/xfreetype.cfg`"
    fi
    rm -f $x11tests/xfreetype.cfg
    # auto-detect Session Management support
    if [ "$CFG_SM" = "auto" ]; then
	CFG_SM=yes
	$x11tests/sm.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_SM=no
    fi
    # auto-detect Xinerama support
    if [ "$CFG_XINERAMA" != "no" ]; then
	CFG_XINERAMA=yes
	$x11tests/xinerama.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_XINERAMA=no
    fi
    # auto-detect tablet support (currenlty only in IRIX)
    if [ "$CFG_TABLET" = "auto" ]; then
	case $PLATFORM in
	irix-*)
	    CFG_TABLET=yes
	    $x11tests/xinput.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_TABLET=no
	    ;;
	*)
	    CFG_TABLET=no
	    ### add error message for the "yes" case on non-IRIX systems
	    ;;
	esac
    fi
    # auto-detect XKB support
    if [ "$CFG_XKB" != "no" ]; then
	CFG_XKB=yes
	$x11tests/xkb.test $XQMAKESPEC $OPT_VERBOSE $L_FLAGS $I_FLAGS && CFG_XKB=no
    fi
fi


#-------------------------------------------------------------------------------
# help - interactive parts of the script _after_ this section please
#-------------------------------------------------------------------------------

# next, emit a usage message if something failed.
if [ "$OPT_HELP" = "yes" ]; then
    [ "x$ERROR" = "xyes" ] && echo
    if [ "$CFG_THREAD" = "yes" ]; then
	THY="*"
	THN=" "
    else
	THY=" "
	THN="*"
    fi
    if [ "$CFG_NIS" = "no" ]; then
	NSY=" "
	NSN="*"
    else
	NSY="*"
	NSN=" "
    fi
    if [ "$CFG_STL" = "auto" ]; then
	SHY="+"
	SHN=" "
    else
	SHY=" "
	SHN="*"
    fi

    cat <<EOF
Usage:  $relconf [-prefix dir]
	[-docdir dir] [-headerdir dir] [-libdir dir]
	[-bindir dir] [-plugindir dir ] [-datadir dir] [-debug] [-release]
	[-qt-gif] [-no-gif] [-sm] [-no-sm] [-stl] [-no-stl]
	[-qt-zlib] [-system-zlib] [-qt-libpng] [-system-libpng]
	[-no-jpeg] [-system-jpeg] [-no-thread] [-thread]
	[-Istring] [-lstring] [-Lstring] [-Rstring] [-disable-<module>]
	[-with-<module setting>] [-without-<module setting>]

Installation options:

 These are optional, but you may specify install directories.

    -prefix dir ........ This will install everything relative dir
			  (default $QT_INSTALL_PREFIX)

 You may use these to separate different parts of the install:

    -bindir dir ........ Executables will be installed to dir
			  (default PREFIX/bin)
    -libdir dir ........ Libraries will be installed to dir
			  (default PREFIX/lib)
    -docdir dir ........ Documentation will be installed to dir
			  (default PREFIX/doc)
    -headerdir dir ..... Headers will be installed to dir
			  (default PREFIX/include)
    -plugindir dir ..... Plugins will be installed to dir 
                          (default PREFIX/plugins)
    -datadir dir ....... Data used by Qt programs will be installed to dir
                          (default PREFIX)

 The defaults (*) are usually acceptable.  If marked with a plus (+) a test
 for that feature has not been done yet, but will be evaluated later, the
 plus simply denotes the default value. Here is a short explanation of each
 option:

 *  -release ........... Compile and link Qt with debugging turned off.
    -debug ............. Compile and link Qt with debugging turned on.

 *  -shared ............ Create and use a shared Qt library (libqt.so).
    -static ............ Create and use a static Qt library (libqt.a).

 *  -no-gif ............ Do not compile in GIF reading support.
    -qt-gif ............ Compile in GIF reading support.
			 See src/kernel/qgif.h

 *  -qt-zlib ........... Use the zlib bundled with Qt.
    -system-zlib ....... Use zlib from the operating system
			 See http://www.info-zip.org/pub/infozip/zlib

    -profile ........... Enable profiling with gprof (adds -pg options)

    -no-g++-exceptions . Disable exceptions on platforms using the GNU C++
			 compiler by using the -fno-exceptions flag.

    -platform target ... The operating system and compiler you are building
			 on ($PLATFORM).
    -xplatform target .. The target platform when cross-compiling.

			 See the PLATFORMS file for a list of supported
			 operating systems and compilers.


    -Dstring ........... Add an explicit define to the preprocessor.
    -Istring ........... Add an explicit include path.
    -Lstring ........... Add an explicit library path.
    -Rstring ........... Add an explicit dynamic library runtime search path.
    -lstring ........... Add an explicit library.


    -disable-<module> ......... Disables a module where module is one of:
				$AVAIL_MODULES

    -qt-sql-<driver> .......... Enable a SQL <driver> in the Qt Library, by
                                default none are turned on.
    -plugin-sql-<driver> ...... Enable SQL <driver> as a plugin to be linked
                                to at run time.
    -no-sql-<driver> .......... Disable SQL <driver> entirely.

                                Possible values for <driver>: [ $CFG_SQL_AVAILABLE ]
                                Auto-Detected on this system: [$CFG_SQL_AUTODETECTED ]

    -qt-style-<style> ......... Enable a GUI <style> in the Qt Library, by
                                default all available are on.
    -plugin-style-<style> ..... Enable GUI <style> as a plugin to be linked
                                to at run time.
    -no-style-<style> ......... Disable GUI <style> entirely.

 $THN  -no-thread ......... Do not compile threading support.
 $THY  -thread ............ Compile threading support.

 $SHN  -no-stl ............ Do not compile STL support.
 $SHY  -stl ............... Compile STL support.

    -verbose .............. Print verbose information about each step of the
    -v .................... configure process.

    -help ................. Print help and usage information.
    -h .................... Print help and usage information.

Image Formats:

    -plugin-imgfmt-<format> Enable format (png, jpeg, or mng) to
                            be linked to at runtime. Uses specified
			    lib<format>.
    -qt-imgfmt-<format> ... Enable format (png, jpeg, or mng) to
                            be linked into Qt. Uses specified
			    lib<format>.
    -no-imgfmt-<format> ... Fully disable format (png, jpeg, or mng)
                            from Qt.

Third Party Image Loading Libraries:

 *  -qt-libpng ......... Use the libpng bundled with Qt.
    -system-libpng ..... Use libpng from the operating system.
			 See http://www.libpng.org/pub/png

 *  -qt-libjpeg ........ Use the libjpeg bundled with Qt.
    -system-libjpeg .... Use jpeglib from the operating system.
			 See http://www.ijg.org

 *  -qt-libmng ......... Use the libmng bundled with Qt.
    -system-libmng ..... Use libmng from the operating system.
			 See http://www.libmng.com
EOF

if [ "$X11_PLATFORM" = "yes" ]; then
    if [ "$CFG_SM" = "no" ]; then
	SMY=" "
	SMN="*"
    else
	SMY="*"
	SMN=" "
    fi
    if [ "$CFG_XINERAMA" = "no" ]; then
	XRY=" "
	XRN="*"
    else
	XRY="*"
	XRN=" "
    fi
    if [ "$CFG_FREETYPE" = "no" ]; then
	XFTY=" "
	XFTN="*"
    else
	XFTY="*"
	XFTN=" "
    fi
    if [ "$CFG_XRENDER" = "no" ]; then
	XRY=" "
	XRN="*"
    else
	XRY="*"
	XRN=" "
    fi
    if [ "$CFG_TABLET" = "no" ]; then
	XIY=" "
	XIN="*"
    else
	XIY="*"
	XIN=" "
    fi
    if [ "$CFG_XKB" = "no" ]; then
        XKY=" "
        XKN="*"
    else
        XKY="*"
        XKN=" "
    fi
    cat << EOF

Qt/X11 only:

 *  -no-nas-sound ...... Do not compile in NAS sound support.
    -system-nas-sound .. Use NAS libaudio from the operating system.
			 See http://radscan.com/nas.html

 $SMN  -no-sm ............. Do not support X Session Management.
 $SMY  -sm ................ Support X Session Management, links in -lSM -lICE.

 $XRN  -no-xinerama ....... Do not compile Xinerama (multihead) support.
 $XRY  -xinerama .......... Compile Xinerama support.
			 Requires X11/extensions/Xinerama.h and libXinerama.

 $XRN  -no-xrender ........ Do not compile XRender support.
 $XRY  -xrender ........... Compile XRender support.
			 Requires X11/extensions/Xrender.h and libXrender.

 $XFTN  -no-xft ............ Do not compile XftFreeType (anti-aliased font) support.
 $XFTY  -xft ............... Compile XftFreeType support.
			 Requires X11/Xft/XftFreetype.h and libXft.

 $XIN  -no-tablet ......... Do not compile Tablet support.
 $XIY  -tablet ............ Compile Tablet support.
			 Requires IRIX with wacom.h and libXi or
			 XFree86 with X11/extensions/XInput.h and libXi.

 $XKN  -no-xkb ............ Do not compile XKB (X KeyBoard extension) support.
 $XKY  -xkb ............... Compile XKB support.

EOF
fi

if [ "$QWS_PLATFORM" = "yes" ]; then
    cat << EOF

Qt/Embedded only:

    -embedded arch...... This will enable the embedded build, you must have a
			 proper license for this switch to work.

    -no-freetype ....... Qt/Embedded only: do not compile in FreeType2 support.
 *  -freetype .......... Qt/Embedded only: add FreeType2 font support.

    -no-zlib ........... No compression library -- implies -no-libpng

    -qconfig local ..... Use src/tools/qconfig-local.h rather than the
			 default ($CFG_QCONFIG).

    -depths list ....... Comma-separated list of supported bit-per-pixel
			 depths, from:  v, 4, 8, 16, 24, and 32.
			 ('v' is VGA16, '8g' is 8bpp greyscale.)

    -accel-voodoo3 ..... Enable Voodoo3 acceleration.
    -accel-mach64 ...... Enable Mach64 acceleration.
    -accel-matrox ...... Enable Matrox MGA acceleration.
    -qvfb .............. Enable X11-based Qt Virtual Frame Buffer.
    -no-qvfb ........... Disable X11-based Qt Virtual Frame Buffer.
    -vnc ............... Enable VNC server (requires network module).

EOF
fi

   [ "x$ERROR" = "xyes" ] && exit 1
   exit 0
fi


#-------------------------------------------------------------------------------
# licensing, interactive part
#-------------------------------------------------------------------------------

# check for license confirmation
if [ "$Edition" = "free" ]; then
    while true; do
	if [ "$QWS_PLATFORM" = "yes" ]; then
	    echo
	    echo "This is the Qt/Embedded Free Edition."
	    echo
	    echo "You are licensed to use this software under the terms of"
	    echo "the GNU General Public License (GPL)."
	    echo
	    affix="the"
	elif [ "$X11_PLATFORM" = "yes" ]; then
	    echo
	    echo "This is the Qt/X11 Free Edition."
	    echo
	    echo "You are licensed to use this software under the terms of either"
	    echo "the Q Public License (QPL) or the GNU General Public License (GPL)."
	    echo
	    echo "Type 'Q' to view the Q Public License."
	    affix="either"
	else
	    echo >&2
	    echo "Internal error." >&2
	    echo >&2
	    exit 2
	fi
	echo "Type 'G' to view the GNU General Public License."
	echo "Type 'yes' to accept this license offer."
	echo "Type 'no' to decline this license offer."
	echo
	echo "Do you accept the terms of $affix license?"
	read acceptance
	if [ "$acceptance" = "yes" ]; then
	    break
	elif [ "$acceptance" = "no" ]; then
	    echo
	    echo "You are not licensed to use this software."
	    echo
	    exit 0
	elif [ "$acceptance" = "G" -o "$acceptance" = "g" ]; then
	    more $relpath/LICENSE.GPL
	elif [ "$acceptance" = "Q" -o "$acceptance" = "q" ]; then
	    if [ "$QWS_PLATFORM" != "yes" ]; then
		more $relpath/LICENSE.QPL
	    fi
	fi
    done
elif [ "$Edition" = "eval" ]; then
    while true; do
	if [ "$QWS_PLATFORM" = "yes" ]; then
	    echo
	    echo "This is the Qt/Embedded Evaluation version."
	    echo
	elif [ "$X11_PLATFORM" = "yes" ]; then
	    echo
	    echo "This is the Qt/X11 Evaluation version."
	    echo
	else
	    echo >&2
	    echo "Internal error." >&2
	    echo >&2
	    exit 2
	fi
	echo "You are licensed to use this software under the terms of"
	echo "the Qt EVALUATION VERSION LICENSE AGREEMENT"
	echo
	echo "Type '?' to view the Evaluation License."
	echo "Type 'yes' to accept this license offer."
	echo "Type 'no' to decline this license offer."
	echo
	echo "Do you accept the terms of the license?"
	read acceptance
	if [ "$acceptance" = "yes" ]; then
	    break
	elif [ "$acceptance" = "no" ]; then
	    echo
	    echo "You are not licensed to use this software."
	    echo
	    exit 0
	elif [ "$acceptance" = "?" ]; then
	    more $relpath/LICENSE.EVAL
	fi
    done
elif [ "$Edition" = "troll" ]; then
    if [ "$QWS_PLATFORM" = "yes" ]; then
	Platform="Qt/Embedded"
    elif [ "$X11_PLATFORM" = "yes" ]; then
	Platform="Qt/X11"
    elif [ "$MAC_PLATFORM" = "yes" ]; then
	Platform="Qt/Mac"
    else
	echo >&2
	echo "Internal error." >&2
	echo >&2
	exit 2
    fi
    echo
    echo "This is the Trolltech in-house version of $Platform."
    echo
else
    if [ -f $HOME/.qt-license ]; then
	ExpiryDate=`echo $ExpiryDate | sed -e "s,-,,g" | tr -d "\n\r"`
	[ -z "$ExpiryDate" ] && ExpiryDate="0"
	Today=`date +%Y%m%d`
	if [ "$Today" -gt "$ExpiryDate" ]; then
	    echo
	    echo "WARNING  WARNING  WARNING  WARNING"
	    echo
	    echo "  Your support and upgrade period has expired."
	    echo
	    echo "  You may continue to use your last licensed release"
	    echo "  of Qt under the terms of your existing license"
	    echo "  agreement. But you are not entitled to technical"
	    echo "  support, nor are you entitled to use any more recent"
	    echo "  Qt releases."
	    echo
	    echo "  Please contact sales@trolltech.com to renew your"
	    echo "  support and upgrades for this license."
	    echo
	    echo "WARNING  WARNING  WARNING  WARNING"
	    echo
	    sleep 3
	fi
    fi
    if [ "$QWS_PLATFORM" = "yes" ]; then
	Platform="Qt/Embedded"
    elif [ "$X11_PLATFORM" = "yes" ]; then
	Platform="Qt/X11"
    elif [ "$MAC_PLATFORM" = "yes" ]; then
	Platform="Qt/Mac"
    else
	echo >&2
	echo "Internal error." >&2
	echo >&2
	exit 2
    fi
    echo
    echo "This is the $Platform $Desc Edition."
    echo
    while true; do
	echo
	echo "Type '?' to view the $Platform License."
	echo "Type 'yes' to accept this license offer."
	echo "Type 'no' to decline this license offer."
	echo
	echo "Do you accept the terms of the $Platform License?"
	read acceptance
	if [ "$acceptance" = "yes" ]; then
	    break
	elif [ "$acceptance" = "no" ]; then
	    echo
	    echo "You are not licensed to use this software."
	    echo
	    exit 0
	else [ "$acceptance" = "?" ]
	    more $relpath/LICENSE
	fi
    done
fi


#-------------------------------------------------------------------------------
# build qmake
#-------------------------------------------------------------------------------

# symlink includes
if [ -x "$relpath/bin/syncqt" ]; then
    QTDIR=$relpath $relpath/bin/syncqt -inc $relpath/include
    # when doing shadow builds, make sure we symlink includes there too
    [ "$OPT_SHADOW" = "yes" ] && QTDIR=$relpath $relpath/bin/syncqt -inc $outpath/include
elif [ "$OPT_SHADOW" = "yes" ]
then
    # syncqt doesn't exist, but we are doing a shadow build, so we need to use
    # the includes from $relpath/include
    I_FLAGS="$I_FLAGS -I${relpath}/include"
fi

# build qmake
if true; then ###[ '!' -f "$outpath/bin/qmake" ]
    echo "Creating qmake. Please wait..."
    rm -f mkspecs/default
    ln -s `basename $QMAKESPEC` mkspecs/default
    # fix makefiles
    for mkfile in GNUmakefile Makefile; do
	EXTRA_LFLAGS=
	EXTRA_CFLAGS=
	in_mkfile="${mkfile}.in"
	[ "$mkfile" = "Makefile" ] && in_mkfile="${mkfile}.unix"
	in_mkfile="$relpath/qmake/$in_mkfile"
	mkfile="$outpath/qmake/$mkfile"

	rm -f $mkfile
	echo "########################################################################" >$mkfile
	echo "## This file was autogenerated by configure, all changes will be lost ##" >>$mkfile
	echo "########################################################################" >>$mkfile
	grep "QMAKE_CC[^_A-Z0-9]" $QMAKESPEC/qmake.conf | sed "s,QMAKE_CC,CC," >>$mkfile
	grep "QMAKE_CXX[^_A-Z0-9]" $QMAKESPEC/qmake.conf | sed "s,QMAKE_CXX,CXX," >>$mkfile
	if [ "$CFG_DEBUG" = "yes" ]; then
          grep "QMAKE_CFLAGS_DEBUG[^_A-Z0-9]" $QMAKESPEC/qmake.conf >> $mkfile
	  EXTRA_CFLAGS="$EXTRA_CFLAGS \$(QMAKE_CFLAGS_DEBUG)"
        fi
	if [ "$MAC_PLATFORM" = "yes" ]; then
	  echo "CARBON_LFLAGS +=-framework Carbon" >> $mkfile
	  EXTRA_LFLAGS="$EXTRA_LFLAGS \$(CARBON_LFLAGS)"
	  EXTRA_CFLAGS="$EXTRA_CFLAGS \$(CARBON_CFLAGS)"
	  fi
	  if [ $EMBEDDED = "qnx" ]; then
	  echo "QNX LFLAGS += -fhonor-std -fno-builtins -pipe"
	  echo "QNX CFLAGS += -lcpp"
	  EXTRA_CFLAGS="$EXTRA_CFLAGS -fhonor-std -fno-builtins -pipe"
	  EXTRA_LFLAGS="$EXTRA_CFLAGS -lcpp"
	  fi
	QMAKE_BIN_DIR="$QT_INSTALL_BINS"
	[ -z "$QMAKE_BIN_DIR" ] && QMAKE_BIN_DIR="${QT_INSTALL_PREFIX}/bin"
	QMAKE_DATA_DIR="$QT_INSTALL_DATA"
	[ -z "$QMAKE_DATA_DIR" ] && QMAKE_DATA_DIR="${QT_INSTALL_PREFIX}"
	QMAKE_OUT_QTDIR=".."
	[ "$OPT_SHADOW" = "yes" ] && QMAKE_OUT_QTDIR="${outpath}"
        echo >>$mkfile
	sed -e "s,@REL_QTDIR@,$relpath,g" -e "s,@OUT_QTDIR@,$QMAKE_OUT_QTDIR,g" \
	    -e "s,@QMAKE_CFLAGS@,$EXTRA_CFLAGS," -e "s,@QMAKE_LFLAGS@,$EXTRA_LFLAGS," \
	    -e "s,@QT_INSTALL_BINS@,\$(INSTALL_ROOT)$QMAKE_BIN_DIR,g" \
	    -e "s,@QT_INSTALL_DATA@,\$(INSTALL_ROOT)$QMAKE_DATA_DIR,g" \
	    -e "s,@QMAKESPEC@,$QMAKESPEC,g" $in_mkfile >>$mkfile
    done

    (cd $outpath/qmake; $MAKE || (echo "QMake failed to build. Aborting." && exit 2)) || exit 2
fi


#-------------------------------------------------------------------------------
# tests that need qmake
#-------------------------------------------------------------------------------

# work around broken X11 headers when using GCC 2.95 or later
if [ "$X11_PLATFORM" = "yes" ]; then
    NOTYPE=no
    $x11tests/notype.test $XQMAKESPEC $OPT_VERBOSE $relpath $outpath && NOTYPE=yes
    [ $NOTYPE = "yes" ] && QMAKE_VARS="$QMAKE_VARS  \"QMAKE_CXXFLAGS+=-fpermissive\""
fi

# find if the compiler supports enough of STL for our purposes
if [ $PLATFORM = "hpux-acc" -a "$CFG_STL" != "no" ]; then
   echo
   echo "As of aCC A.3.0.30 you may enable STL support in Qt by adding -AA"
   echo "to the QMAKE_CXXFLAGS and QMAKE_LFLAGS variables in"
   echo "$XQMAKESPEC/qmake.conf"
   echo "and re-running configure. Make sure to use the -AA compiler switch"
   echo "in all your software projects using Qt as it turns on the new C++"
   echo "Standard Library which is not binary compatible with the old one."
   echo
fi

if [ "$CFG_STL" = "auto" ]; then
    CFG_STL=yes
    $unixtests/stl.test $XQMAKESPEC $OPT_VERBOSE $relpath $outpath && CFG_STL=no
fi


#-------------------------------------------------------------------------------
# ask for all that hasn't been auto-detected or specified in the arguments
#-------------------------------------------------------------------------------

### fix this: user input should be in a loop and tested for correctness
if [ "$CFG_QWS_DEPTHS" = "prompted" -a "$EMBEDDED" != "no" ]; then
    echo
    echo "Choose pixel-depths to support:"
    echo
    echo "   v. VGA-16 - also enables 8bpp"
    echo "   4. 4bpp grayscale - also enables 8bpp"
    echo "   8. 8bpp"
    echo "  8g. 8bpp grayscale - also enables 8bpp"
    echo "  16. 16bpp"
    echo "  24. 24bpp - also enables 32bpp"
    echo "  32. 32bpp"
    echo
    echo "Each depth adds around 100Kb on 80386."
    echo
    echo "Your choices (default 8,16):"
    read CFG_QWS_DEPTHS
    if [ -z "$CFG_QWS_DEPTHS" ]; then
	CFG_QWS_DEPTHS=8,16
    fi
fi
if [ -n "$CFG_QWS_DEPTHS" -a "$EMBEDDED" != "no" ]; then
    DG=;DV=;D4=;D8=;D16=;D24=;D32=
    for D in `echo "$CFG_QWS_DEPTHS" | sed -e 's/,/ /g'`; do
	case "$D" in
	    v) DV=y; D8=y
	;;  4) D4=y; D8=y
	;;  8) D8=y
	;; 8g) D8=y; DG=y
	;; 16) D16=y
	;; 24) D24=y; D32=y
	;; 32) D32=y
	esac
    done
    if [ -z "$DG" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_8GRAYSCALE"
    fi
    if [ -z "$DV" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_VGA_16"
    fi
    if [ -z "$D4" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_4"
    fi
    if [ -z "$D8" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_8"
    fi
    if [ -z "$D16" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_16"
    fi
    if [ -z "$D24" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_24"
    fi
    if [ -z "$D32" ]; then
	D_FLAGS="$D_FLAGS QT_NO_QWS_DEPTH_32"
    fi
fi

### fix this: user input should be in a loop and tested for correctness
if [ "$CFG_QWS_QVFB" = "prompted" -a "$EMBEDDED" != "no" ]; then
    echo
    echo "Enable Qt Virtual Framebuffer support for development on X11 (default yes)"
    read CFG_QWS_QVFB
    if [ "$CFG_QWS_QVFB" != "n" -a "$CFG_QWS_QVFB" != "no" ]; then
	CFG_QWS_QVFB=yes
    else
	CFG_QWS_QVFB=no
    fi
fi

if [ -z "$CFG_QWS_VOODOO3" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_VOODOO3"
fi
if [ -z "$CFG_QWS_MACH64" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_MACH64"
fi
if [ -z "$CFG_QWS_REPEATER" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_REPEATER"
fi
if [ -z "$CFG_QWS_MATROX" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_MATROX"
fi
if [ "$CFG_QWS_QVFB" = "no" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_VFB"
fi
if [ -z "$CFG_QWS_VNC" -a "$EMBEDDED" != "no" ]; then
    D_FLAGS="$D_FLAGS QT_NO_QWS_VNC"
fi
if [ "$EMBEDDED" != "no" ]; then
    MODULES=`echo $MODULES | sed -e 's/opengl//'`
fi


#-------------------------------------------------------------------------------
# part of configuration information goes into qconfig.h
#-------------------------------------------------------------------------------

[ -r qconfig.h-hold ] && mv -f qconfig.h-hold $outpath/include/qconfig.h

case "$CFG_QCONFIG" in
minimal|small|medium|large|full)
    # these are a sequence of increasing functionality
    for c in minimal small medium large full; do
	QMAKE_CONFIG="$QMAKE_CONFIG $c-config"
	if [ "$CFG_QCONFIG" = $c ]; then
	    break;
	fi
    done
    ;;
*)
    # not known to be sufficient for anything
    if [ ! -f $relpath/src/tools/qconfig-$CFG_QCONFIG.h ]; then
	echo >&2 "No such configuration: $CFG_QCONFIG"
	OPT_HELP=yes
    fi
esac
case "$CFG_QCONFIG" in
full)
    echo "// Everything" >$outpath/include/qconfig.h.new
    ;;
*)
    QCONFIGFILE=qconfig-$CFG_QCONFIG.h
    echo "// Copied from $QCONFIGFILE" >$outpath/include/qconfig.h.new
    cat $relpath/src/tools/$QCONFIGFILE >>$outpath/include/qconfig.h.new
    ;;
esac
cat >> $outpath/include/qconfig.h.new <<EOF

// From configure
#define QT_INSTALL_PREFIX "$QT_INSTALL_PREFIX"
#define QT_PRODUCT_LICENSEE "$Licensee"
#define QT_PRODUCT_LICENSE "$Products"
EOF
[ '!' -z "$LicenseKey" ] && echo "#define QT_PRODUCT_LICENSEKEY \"$LicenseKey\"" >>qconfig.h.new
[ '!' -z "$QT_INSTALL_PLUGINS" ] && echo "#define QT_INSTALL_PLUGINS ${QT_INSTALL_PLUGINS}" >>qconfig.h.new
[ '!' -z "$QT_INSTALL_DATA" ]    && echo "#define QT_INSTALL_DATA    ${QT_INSTALL_DATA}"    >>qconfig.h.new

# avoid unecessary rebuilds by copying only if qconfig.h has changed
if cmp -s $outpath/include/qconfig.h $outpath/include/qconfig.h.new; then
    rm -f $outpath/include/qconfig.h.new
else
    [ -f $outpath/include/qconfig.h ] && chmod +w $outpath/include/qconfig.h
    mv $outpath/include/qconfig.h.new $outpath/include/qconfig.h
    chmod -w $outpath/include/qconfig.h
fi


#-------------------------------------------------------------------------------
# ??? ### please add a title to this section
#-------------------------------------------------------------------------------

# build up the variables for output
QMAKE_OUTDIR=""
case $Products in
qt-enterprise)
    QMAKE_CONFIG="$QMAKE_CONFIG enterprise"
    ;;
qt-internal)
    # QMAKE_CONFIG="$QMAKE_CONFIG internal"
    ;;
*)
    ;;
esac

if [ "$CFG_DEBUG" = "yes" ]; then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}debug"
    QMAKE_CONFIG="$QMAKE_CONFIG debug"
elif [ "$CFG_DEBUG" = "no" ]; then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}release"
    QMAKE_CONFIG="$QMAKE_CONFIG release"
fi
if [ "$CFG_THREAD" = "yes" ]; then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}-mt"
    QMAKE_CONFIG="$QMAKE_CONFIG thread"
fi
if [ "x$EMBEDDED" != "xno" ]; then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}-emb-$EMBEDDED"
    QMAKE_CONFIG="$QMAKE_CONFIG embedded"
    rm -f src/.moc/$QMAKE_OUTDIR/allmoc.cpp # needs remaking if config changes
fi
### couldn't we use something like
### 	QMAKE_OUT_QTDIR=".."
### 	[ "$OPT_SHADOW" = "yes" ] && QMAKE_OUT_QTDIR="${outpath}"
### instead of the line below like it's done later on?
qdir="\$(QTDIR)"
[ "$OPT_SHADOW" = "yes" ] && qdir="$outpath"
QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBDIR_QT=$qdir/lib\""
QMAKE_VARS="$QMAKE_VARS \"OBJECTS_DIR=.obj/$QMAKE_OUTDIR\" \"MOC_DIR=.moc/$QMAKE_OUTDIR\""

if [ "$CFG_SHARED" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG staticlib"
elif [ "$CFG_SHARED" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG dll"
fi
[ "$CFG_LIBJPEG" = "system" ] && QMAKE_CONFIG="$QMAKE_CONFIG system-jpeg"
if [ "$CFG_JPEG" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-jpeg"
elif [ "$CFG_JPEG" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG jpeg"
fi
[ "$CFG_LIBMNG" = "system" ] && QMAKE_CONFIG="$QMAKE_CONFIG system-mng"
if [ "$CFG_MNG" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-mng"
elif [ "$CFG_MNG" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG mng"
fi
[ "$CFG_LIBPNG" = "system" ] && QMAKE_CONFIG="$QMAKE_CONFIG system-png"
if [ "$CFG_PNG" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-png"
elif [ "$CFG_PNG" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG png"
fi
if [ "$CFG_GIF" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-gif"
elif [ "$CFG_GIF" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG gif"
fi
if [ "x$MAC_PLATFORM" = "xyes" ]; then
    [ "$CFG_ZLIB" = "yes" ] && CFG_ZLIB="system"
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_QT+=-lz\""
fi
if [ "$CFG_ZLIB" = "no" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-zlib"
elif [ "$CFG_ZLIB" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG zlib"
elif [ "$CFG_ZLIB" = "system" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG system-zlib"
fi
[ "$CFG_NIS" = "yes" ] && QMAKE_CONFIG="$QMAKE_CONFIG nis"
[ "$CFG_NAS" = "yes" ] && QMAKE_CONFIG="$QMAKE_CONFIG nas"
[ "$CFG_BIG_CODECS" = "yes" ] && QMAKE_CONFIG="$QMAKE_CONFIG bigcodecs"

if [ "$CFG_REMOTE" = "yes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG remote"
fi

[ "$CFG_QWS_FREETYPE" = "yes" -a "x$EMBEDDED" != "xno" ] && QMAKE_CONFIG="$QMAKE_CONFIG ft"
if [ "$X11_PLATFORM" = "yes" ]; then
    [ "$CFG_SM" = "yes" ] && QMAKE_CONFIG="$QMAKE_CONFIG x11sm"

    # for some reason, the following libraries are not always build shared,
    # so *every* program/lib (including Qt) has to link against them
    if [ "$CFG_XINERAMA" = "yes" ]; then
	QMAKE_CONFIG="$QMAKE_CONFIG xinerama"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXinerama\""
    fi
    if [ "$CFG_XRENDER" = "yes" ]; then
	QMAKE_CONFIG="$QMAKE_CONFIG xrender"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXrender\""
    fi
    if [ "$CFG_FREETYPE" = "yes" ]; then
	QMAKE_CONFIG="$QMAKE_CONFIG xftfreetype"
	# libXft uses libfreetype to do all the work, BUT libfreetype isn't picked
	# up as a dependency if libXft isn't a shared library, so we have to add
	# libfreetype as well as libXt to the program/lib link command
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXft -lfreetype\""
    fi
    if [ "$CFG_TABLET" = "yes" ]; then
	QMAKE_CONFIG="$QMAKE_CONFIG tablet"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXi\""
    fi
    if [ "$CFG_XKB" = "yes" ]; then
        QMAKE_CONFIG="$QMAKE_CONFIG xkb"
    fi
fi

[ '!' -z "$D_FLAGS" ] && QMAKE_VARS="$QMAKE_VARS \"DEFINES+=$D_FLAGS\""
[ '!' -z "$L_FLAGS" ] && QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBDIR_FLAGS+=$L_FLAGS\""
[ '!' -z "$l_FLAGS" ] && QMAKE_VARS="$QMAKE_VARS \"LIBS+=$l_FLAGS\""

if [ '!' -z "$R_FLAGS" ]; then
    if grep QMAKE_RPATH $XQMAKESPEC/qmake.conf >/dev/null; then
	echo # Using -R/-rpath, so no need to warn
    else
	echo
	echo "ERROR: -R cannot be used on this platform as \$QMAKE_RPATH is"
	echo "       undefined."
	echo
	exit 1
    fi
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_LFLAGS+=$R_FLAGS\""
fi

if [ '!' -z "$I_FLAGS" ]; then
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_CFLAGS+=$I_FLAGS\""
    QMAKE_VARS="$QMAKE_VARS \"QMAKE_CXXFLAGS+=$I_FLAGS\""
fi

# install things
QMAKE_VARS="$QMAKE_VARS \"QT_PREFIX=${QT_INSTALL_PREFIX}\""
[ '!' -z "$QT_INSTALL_DOCS" ] && QMAKE_VARS="$QMAKE_VARS \"docs.path=${QT_INSTALL_DOCS}\""
[ '!' -z "$QT_INSTALL_HEADERS" ] && QMAKE_VARS="$QMAKE_VARS \"headers.path=${QT_INSTALL_HEADERS}\""
[ '!' -z "$QT_INSTALL_PLUGINS" ] && QMAKE_VARS="$QMAKE_VARS \"plugins.path=${QT_INSTALL_PLUGINS}\""
[ '!' -z "$QT_INSTALL_LIBS" ] && QMAKE_VARS="$QMAKE_VARS \"libs.path=${QT_INSTALL_LIBS}\""
[ '!' -z "$QT_INSTALL_BINS" ] && QMAKE_VARS="$QMAKE_VARS \"bins.path=${QT_INSTALL_BINS}\""
[ '!' -z "$QT_INSTALL_DATA" ] && QMAKE_VARS="$QMAKE_VARS \"data.path=${QT_INSTALL_DATA}\""

#-------------------------------------------------------------------------------
# part of configuration information goes into qmodules.h
#-------------------------------------------------------------------------------

[ -r qmodules.h-hold ] && mv -f qmodules.h-hold $outpath/include/qmodules.h
rm -f $outpath/include/qmodules.h.new
cat >$outpath/include/qmodules.h.new << EOF
// These modules are present in this configuration of Qt
EOF
for MODULE in $MODULES; do
    if [ -d "$relpath/src/$MODULE" ]; then
	M=`echo $MODULE | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
	echo "#define QT_MODULE_$M" >>$outpath/include/qmodules.h.new
	QMAKE_CONFIG="$QMAKE_CONFIG $MODULE"
    fi
done
cat >>$outpath/include/qmodules.h.new << EOF

// Compile time features
EOF
if [ "$CFG_STL" = "no" ]; then
cat >>$outpath/include/qmodules.h.new << EOF
#ifndef QT_NO_STL
#define QT_NO_STL
#endif
EOF
else
QMAKE_CONFIG="$QMAKE_CONFIG stl"
fi

# avoid unecessary rebuilds by copying only if qmodules.h has changed
if cmp -s $outpath/include/qmodules.h $outpath/include/qmodules.h.new; then
    rm -f $outpath/include/qmodules.h.new
else
    [ -f $outpath/include/qmodules.h ] && chmod +w $outpath/include/qmodules.h
    mv -f $outpath/include/qmodules.h.new $outpath/include/qmodules.h
    chmod -w $outpath/include/qmodules.h
fi


#-------------------------------------------------------------------------------
# save configuration into .qmake.cache
#-------------------------------------------------------------------------------

CACHEFILE=$outpath/.qmake.cache
[ -f $CACHEFILE ] && rm -f $CACHEFILE
cat >>$CACHEFILE <<EOF
CONFIG += $QMAKE_CONFIG dylib create_prl link_prl
QMAKESPEC = $XPLATFORM
QT_SOURCE_TREE = $relpath
QT_BUILD_TREE = $outpath
QMAKE_ABSOLUTE_SOURCE_ROOT = \$\$QT_SOURCE_TREE
EOF
# cmdline args
quoted=no
tmp=""
for i in $QMAKE_VARS; do
    if [ "$quoted" = "no" ]; then
	case "$i" in
	\"*\") echo $i | sed 's,^",,' | sed 's,"$,,' >>$CACHEFILE ;;
	\"*) quoted=yes ; tmp=$i ;;
	*) echo $i >>$CACHEFILE ;;
	esac
    else
	case "$i" in
	*\") quoted=no
	   echo $tmp $i | sed 's,^",,' | sed 's,"$,,' >>$CACHEFILE
	   tmp="" ;;
	*) tmp="$tmp $i" >>$CACHEFILE ;;
    esac
    fi
done
# incrementals
INCREMENTAL=""
[ "$CFG_INCREMENTAL" = "auto" ] && which p4 >/dev/null 2>&1 && [ "$Edition" = "troll" ] && CFG_INCREMENTAL="yes"
if [ "$CFG_INCREMENTAL" = "yes" ]; then
    openfiles=`find $relpath -perm u+w -mtime -3 | grep 'cpp$'`
    for f in $openfiles; do
	# don't need to worry about generated files
	[ -r `echo $f | sed "s,cpp$,ui,"` ] && continue
	basename $f | grep '^moc_' >/dev/null 2>&1 && continue
	# done
	INCREMENTAL="$INCREMENTAL `basename $f | sed 's,.cpp,.o,'`"
    done
    [ '!' -z "$INCREMENTAL" ] && echo "QMAKE_INCREMENTAL += $INCREMENTAL" >>$CACHEFILE
    [ -r "$outpath/.qmake.incremental" ] && echo "include($outpath/.qmake.incremental)" >>$CACHEFILE
fi


#-------------------------------------------------------------------------------
# give feedback on configuration
#-------------------------------------------------------------------------------

# check if we are using gcc/g++
COMPILER=`echo $PLATFORM | cut -f 2- -d-`
if [ "$COMPILER" = "g++" ]; then
    if [ "$CFG_GPLUSPLUS_EXCEPTIONS" != "no" ]; then
	cat <<EOF

	This target is using the GNU C++ compiler ($PLATFORM).

	Recent versions of this compiler automatically include code for
	exceptions, which increase both the size of the Qt library and the
	amount of memory taken by your applications.

	You may elect to re-run `basename $0` with the -no-g++-exceptions
	option to compile Qt without exceptions.  This is completely binary
	compatible, and existing applications should continue to work.

EOF
    fi
fi

echo
if [ "$XPLATFORM" = "$PLATFORM" ]; then
    echo "Build type:    $PLATFORM"
else
    echo "Building on:   $PLATFORM"
    echo "Building for:  $XPLATFORM"
fi

if [ -n "$PLATFORM_NOTES" ]; then
    echo "Platform notes:"
    echo "$PLATFORM_NOTES"
else
    echo
fi

if [ "$OPT_VERBOSE" = "yes" ]; then
    echo "QMake vars ...... $QMAKE_VARS"
    echo "QMake switches .. $QMAKE_SWITCHES"
fi

[ "x$OPT_CONCURRENT" != "x0" ] && echo "Concurrent .......... $OPT_CONCURRENT"
[ "$CFG_INCREMENTAL" = "yes" ] && [ ! -z "$INCREMENTAL" ] && echo "Incremental ......... $INCREMENTAL"
echo "Configuration ....... $QMAKE_CONFIG"
echo "Remote support ...... $CFG_REMOTE"
echo "STL support ......... $CFG_STL"
echo "Thread support ...... $CFG_THREAD"
echo "GIF support ......... $CFG_GIF"
if [ "$CFG_MNG" = "no" ]; then
    echo "MNG support ......... $CFG_MNG"
else
    echo "MNG support ......... $CFG_MNG ($CFG_LIBMNG)"
fi
if [ "$CFG_JPEG" = "no" ]; then
    echo "JPEG support ........ $CFG_JPEG"
else
    echo "JPEG support ........ $CFG_JPEG ($CFG_LIBJPEG)"
fi
if [ "$CFG_PNG" = "no" ]; then
    echo "PNG support ......... $CFG_PNG"
else
    echo "PNG support ......... $CFG_PNG ($CFG_LIBPNG)"
fi
if [ "$QWS_PLATFORM" != "yes" ]; then
    if echo $MODULES | grep opengl >/dev/null; then
	opengl=yes
    else
	opengl=no
    fi
    echo "OpenGL support ...... $opengl"
fi
if [ "$QWS_PLATFORM" = "yes" ]; then
    echo "Embedded support .... $EMBEDDED"
fi
if [ "$X11_PLATFORM" = "yes" ]; then
    echo "NAS sound support ... $CFG_NAS"
    echo "Session management .. $CFG_SM"
    echo "Xinerama support .... $CFG_XINERAMA"
    echo "Tablet support ...... $CFG_TABLET"
    echo "XRender support ..... $CFG_XRENDER"
    echo "XftFreeType support . $CFG_FREETYPE"
    echo "XKB Support ......... $CFG_XKB"
fi

# complain about not being able to use plugins if we are using a static build
if [ "$CFG_SHARED" = "no" ]; then
    echo
    echo "WARNING: Using static linking will disable the use of plugins."
    echo "Make sure you compile ALL needed modules into the library."
    echo
fi
echo

sepath=`echo $relpath | sed -e 's/\\./\\\\./g'`
PROCS=1
PIPE=""
EXEC=""


#-------------------------------------------------------------------------------
# build makefiles based on the configuration
#-------------------------------------------------------------------------------

echo "Finding project files. Please wait..."
[ -z "$QMAKE_PROJECTS" ] && QMAKE_PROJECTS=`find $relpath/. -name '*.pro' -print | sed 's-/\./-/-'`

# projects to process
rm -f .sorted.projects
# qt and moc
rm -f .sorted.projects.1
# the rest
rm -f .sorted.projects.2

for p in `echo $QMAKE_PROJECTS`; do
    if [ "$relpath/src/qt.pro" = "$p" ] || [ "$relpath/src/moc/moc.pro" = "$p" ]; then
	# put Qt and MOC at the front of the project list, so that all the
	# other projects that depend on them don't have to be re-qmaked if they
	# happen to sort before their dependency
	echo $p >> .sorted.projects.1
    else
	echo $p >> .sorted.projects.2
    fi
done
# don't sort Qt and MOC in with the other project files
# also work around a segfaulting uniq(1)
sort .sorted.projects.2 > .sorted.projects.2.new
mv -f .sorted.projects.2.new .sorted.projects.2
uniq .sorted.projects.2 > .sorted.projects.2.new
mv -f .sorted.projects.2.new .sorted.projects.2
[ -f .sorted.projects.2 ] && cat .sorted.projects.2 >> .sorted.projects.1
[ -f .sorted.projects.1 ] && cat .sorted.projects.1 >> .sorted.projects
rm -f .sorted.projects.2 .sorted.projects.1
for p in `echo $QMAKE_IGNORE_PROJECTS`; do
    grep -v $p .sorted.projects > .sorted.projects.new
    mv -f .sorted.projects.new .sorted.projects
done
PROJECT_COUNT=`cat .sorted.projects | wc -l | sed -e "s, ,,g"`
echo "  $PROJECT_COUNT projects found."
echo

echo "Creating makefiles. Please wait..."
# these need to be processed *now*
for a in `cat .sorted.projects`; do
    case $a in
    *-kde.pro|*qtmain.pro) continue ;;
    */qmake/qmake.pro) continue ;;
    *-pro.pro)
	if [ "x$Edition" != "xpro" ]; then
	    continue
	fi
	;;
    *moc*) SPEC=$QMAKESPEC ;;
    *) SPEC=$XQMAKESPEC ;;
    esac
    file=`basename $a`
    dir=`dirname $a | sed -e "s;$sepath;.;g"`
    test -d $dir || mkdir -p $dir
    N=$outpath/$dir/Makefile
    QMAKE_SPEC_ARGS= 
    [ "$SPEC" != "$XQMAKESPEC" ] && QMAKE_SPEC_ARGS="-spec $SPEC"
    PIPE="$outpath/bin/qmake $QMAKE_SWITCHES $QMAKE_SPEC_ARGS -o $N $a"
    if [ "x$OPT_CONCURRENT" = "x0" ] || [ "x$OPT_CONCURRENT" = "x1" ]; then
	EXEC=$PIPE
    else
	EXEC="$EXEC ($PIPE & ) ;"
	if [ "x$PROCS" != "x$OPT_CONCURRENT" ]; then
	    PROCS=`expr $PROCS + 1`
	else
	    PROCS=1
	    EXEC="$EXEC wait"
	fi
    fi

    if echo '\c' | grep '\c' >/dev/null; then
	echo -n "  for $a"
    else
	echo "  for $a\c"
    fi
    if [ "$OPT_VERBOSE" = "yes" ]; then
	echo " (`basename $SPEC`)"
	if echo '\c' | grep '\c' >/dev/null; then
	    echo -n "$EXEC"
	else
	    echo "$EXEC\c"
	fi
    fi
    echo

    [ -f "$N" ] && chmod +w $N
    if [ "x$PROCS" = "x1" ]; then
	/bin/sh -c "$EXEC"
	EXEC=""
    fi
done
# this is just in case the concurrent task is not a multiple of the number of tasks
[ '!' -z "$EXEC" ] && /bin/sh -c "$EXEC wait"

if echo "$LD_LIBRARY_PATH" | grep >/dev/null $outpath/lib; then
    echo
else
    if grep QMAKE_RPATH $XQMAKESPEC/qmake.conf >/dev/null; then
	echo # Using -R/-rpath, so no need to warn
    else
	echo
	echo "WARNING: \$LD_LIBRARY_PATH does not contain $outpath/lib"
	echo "         You will need to add it to correctly compile Qt."
	echo
    fi
fi
rm -f .sorted.projects


#-------------------------------------------------------------------------------
# finally save the executed command to another script
#-------------------------------------------------------------------------------

[ -f $outpath/config.status ] && rm -f $outpath/config.status
cat >$outpath/config.status << EOF
#!/bin/sh

echo yes | $relpath/$relconf $OPT_CMDLINE \$@
EOF
chmod +x $outpath/config.status

echo
echo Qt is now configured for building. Just run $MAKE.
echo To reconfigure, run $MAKE clean and configure.
echo
