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

# To remove the need for the first test, do this:
# QTDIR=`pwd` export QTDIR

relconf=`basename $0`
relpath=`dirname $0`
relpath=`(cd $relpath; pwd)`
outpath=$relpath

#internal variables
LAZY_DEPS_ALLOWED=no
PROFILECFLAGS=
CONFIG_CMDLINE=`echo $@ | sed "s,-v,,g"` #cache the commandline for config.status
QMAKE_SWITCHES=""
QMAKE_VARS=""
QMAKE_CONFIG=""
QWS_QVFB=prompted
QWS_DEPTHS=prompted
HELP=
GPLUSPLUS_EXCEPTIONS=unspecified
#platform
X11_PLATFORM=no
MAC_PLATFORM=no
QWS_PLATFORM=no

if [ -d mkspecs/qws ]
then
    QWS_CANDIDATE="yes"
else
    QWS_CANDIDATE="no"
fi

if [ "$relpath" != `pwd` ]
then
    # setup the magic variable
    outpath=`pwd`

    echo "Preparing build tree..."
    mkdir -p $outpath/bin #make build dir

    # 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."
	exit 1
    fi

    # qmake must be symlinked
    for a in `find $relpath/qmake`
    do
       my_a=`echo $a | sed "s,^${relpath}/,${outpath}/,"`
       [ -f $my_a ] && continue
       if [ -d $a ]
       then
	    mkdir -p $my_a
       else
	   a_dir=`dirname $my_a`
	   [ -d $a_dir ] || mkdir -p $a_dir
	   case `basename $a` in
	   *.o|*.d|GNUmakefile*|qmake) ;;
	   *) rm -f $my_a; ln -s $a $my_a ;;
	   esac
       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)
    if [ ! -d $outpath/tools ]
    then
	mkdir $outpath/tools
    fi
    if [ ! -d $outpath/tools/linguist ]
    then
	mkdir $outpath/tools/linguist
    fi
    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)
    if [ ! -d $outpath/tools/designer ]
    then
	mkdir $outpath/tools/designer
    fi
    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

    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 "'`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.
fi

# 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
    if [ "$QWS_CANDIDATE" = "yes" ]
    then
	EMB="/Embedded"
	QWS_PLATFORM=yes
    else
	EMB=""
    fi
    Edition=free
    Licensee=you
    while true
    do
	echo
	echo "This is the Qt$EMB Free Edition."
	echo
	if [ -n "$EMB" ]
	then
	    echo "You are licensed to use this software under the terms of"
	    echo "the GNU General Public License (GPL)."
	    echo
	    QWS_EITHER="the"
	else
	    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."
	    QWS_EITHER="either"
	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 $QWS_EITHER license?"
	read acceptance
	if [ "$acceptance" = yes ]
	then
	    break
	else
	    if [ "$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 [ -z "$EMB" ]
		    then
			more $relpath/LICENSE.QPL
		    fi
	    fi
	fi
    done
    Licensee="Non-Commercial"
    Products="qt-free"
# Internal Trolltech edition.
elif [ -f $relpath/LICENSE.TROLL ]
then
    Edition=troll
    echo
    echo "This is the Trolltech in-house version of Qt."
    echo
    Licensee="Trolltech"
    Products="qt-internal"
    EMB="/Embedded"       ### fixme again
# Commercial edition - needs a license.
else
    if [ -f $relpath/LICENSE ]
    then
	if [ "$QWS_CANDIDATE" = "yes" ]
	then
	    EMB="/Embedded"
	    QWS_PLATFORM=yes
	else
	    EMB=""
	fi
	echo
	if [ "$QWS_PLATFORM" = "yes" ]
	then
	    EMB="/Embedded"
	else
	    EMB=""
	fi
	Desc=""
	if [ -f $HOME/.qt-license ]
	then
	    . $HOME/.qt-license >/dev/null 2>&1
	    if echo $Products | grep >/dev/null 'qt-enterprise'
	    then
		Desc=Enterprise
		Edition=ent
	    else
		Products=qt-professional
		Desc=Professional
		Edition=pro
	    fi
	    ExpiryDate=`echo $ExpiryDate | sed -e "s,-,,g"`
	    NOW=`date +%Y%m%d`
	    if [ "$NOW" -gt "0$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
	else
	    echo
	    echo "\"$HOME/.qt-license\" not found"
	    echo
	    echo "You have not installed a valid 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
	E="Qt$EMB $Desc Edition"
	echo
	echo "This is the $E."
	echo
	while true
	do
	    echo
	    echo "Type '?' to view the $E 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 $E License?"
	    read acceptance
	    if [ "$acceptance" = yes ]
	    then
		break
	    else
		if [ "$acceptance" = no ]
		then
		    echo
		    echo "You are not licensed to use this software."
		    echo
		    exit 0
		fi
		more $relpath/LICENSE
	    fi
	done
    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

# create the include directory (will hold real qconfig.h and qmodules.h)
if [ ! -d $outpath/include ]; then
   mkdir $outpath/include
fi

qdir="\$(QTDIR)"
[ "x$relpath" != "x$outpath" ] && qdir="$outpath"

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

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

if [ "$1" = -h -o "$1" = -help -o "$1" = --help ]
then
    HELP=yes
else

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

# Remove the build options file
[ -f $outpath/.qmake.cache ] && rm -f $outpath/.qmake.cache

fi # help

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

# Defaults
DO_INCREMENT=auto
QCONFIG=full
if [ "$QWS_PLATFORM" = "yes" ]
then
    EMBEDDED=auto
else
    EMBEDDED=no
fi
VERBOSE=no
DEBUG=no
SHARED=yes
GIF=no
THREAD=auto
SM=auto
XINERAMA=auto
BIG_CODECS=yes
ZLIB=yes
LIBPNG=yes
JPEG=no
MNG=no
XRENDER=auto
XFTFREETYPE=auto
SQL_AUTODETECTED=""
SQL_AVAIL=""
TABLET=auto
XKB=auto
STL=auto
FT=yes
NAS_SOUND=no
REMOTE=no
D_FLAGS=
I_FLAGS=
L_FLAGS=
R_FLAGS=
l_FLAGS=
XPLATFORM=
PLATFORM=$QMAKESPEC
FROMMAKE=
QMAKE_PROJECTS=""
QMAKE_IGNORE_PROJECTS=""
CONCURRENT=0

# qt installs
QT_INSTALL_PREFIX=/usr/local/qt
QT_INSTALL_DOCS=
QT_INSTALL_HEADERS=
QT_INSTALL_LIBS=
QT_INSTALL_BINS=

# parse the arguments, setting things to "yes" or "no"
while [ -n "$1" ]
do
   case $1 in
   -no-lazydeps)
        LAZY_DEPS_ALLOWED=no
	;;
   -prefix)
	shift
	QT_INSTALL_PREFIX=$1
	;;
   -docdir)
	shift
	QT_INSTALL_DOCS=$1
	;;
   -headerdir)
	shift
	QT_INSTALL_HEADERS=$1
	;;
   -libdir)
	shift
	QT_INSTALL_LIBS=$1
	;;
   -bindir)
	shift
	QT_INSTALL_BINS=$1
	;;
   -frommake)
	FROMMAKE=yes
	;;
   -embedded)
	shift;
	if [ -z $EMB ]
	then
	    echo "No license exists to enable Embedded. Disabling."
	else
	    EMBEDDED=$1
	    PLATFORM=qws/linux-generic-g++
	    XPLATFORM=qws/linux-${EMBEDDED}-g++
	fi
	;;
   -depths)
	shift; QWS_DEPTHS=$1;
	;;
   -no-incremental)
	DO_INCREMENT="no"
	;;
   -accel-voodoo3)
	QWS_ACCEL_VOODOO3=y
	;;
   -accel-mach64)
	QWS_ACCEL_MACH64=y
	;;
   -repeater)
	QWS_REPEATER=y
	;;
   -accel-matrox)
	QWS_ACCEL_MATROX=y
	;;
   -qvfb)
	QWS_QVFB=y
	;;
   -no-qvfb)
	QWS_QVFB=n
	;;
   -vnc)
	QWS_VNC=y
	;;
   -qconfig)
	shift
	QCONFIG=$1
	;;
   -nomake)
	shift
	QMAKE_IGNORE_PROJECTS="$QMAKE_IGNORE_PROJECTS $1"
	;;
   -make)
	shift
	if [ -f $relpath/$1 ]
	then
	    QMAKE_PROJECTS="$QMAKE_PROJECTS $relpath/$1"
	else
	    if [ -d $relpath/$1 ]
	    then
		QMAKE_PROJECTS="$QMAKE_PROJECTS `find $relpath/$1 -name '*.pro' -print`"
	    else
		QMAKE_PROJECTS="$QMAKE_PROJECTS `find $relpath/. -name '*.pro' -print`"
	    fi
	fi
	;;
   -profile)
	QMAKE_VARS="$QMAKE_VARS QMAKE_CFLAGS+=-pg QMAKE_CXXFLAGS+=-pg"
	QMAKE_VARS="$QMAKE_VARS QMAKE_LFLAGS+=-pg"
	;;
   -no-g++-exceptions)
	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\""
	;;
   -platform)
	shift; PLATFORM=$1
	;;
   -xplatform)
	shift; XPLATFORM=$1
	;;
   -release)
	DEBUG=no
	;;
   -debug)
	DEBUG=yes
	;;
   -shared)
	SHARED=yes
	;;
   -static)
	SHARED=no
	;;
   -no-gif)
	GIF=no
	;;
   -qt-gif)
	GIF=yes
	;;
   -no-sm)
	SM=no
	;;
   -sm)
	SM=yes
	;;
   -no-thread)
	THREAD=no
	;;
   -thread)
	THREAD=yes
	;;
   -no-xinerama)
	XINERAMA=no
	;;
   -xinerama)
	XINERAMA=yes
	;;
   -no-tablet)
	TABLET=no
	;;
   -tablet)
	TABLET=yes
	;;
   -stl)
	STL=yes
	;;
   -no-stl)
	STL=no
	;;
   -freetype)
	FT=yes
	;;
   -no-freetype)
	FT=no
	;;
   -no-zlib)
	ZLIB=no
	LIBPNG=no
	;;
   -qt-zlib)
	ZLIB=yes
	;;
   -system-zlib)
	ZLIB=system
	;;
   -no-libpng)
	LIBPNG=no
	;;
   -qt-libpng)
	LIBPNG=yes
	;;
   -system-libpng)
	LIBPNG=system
	;;
   -no-mng)
	MNG=no
	;;
   -system-libmng)
	MNG=system
	;;
   -qt-libmng)
        MNG=yes
	;;
   -no-jpeg)
	JPEG=no
	;;
   -qt-jpeg)
	JPEG=yes
	;;
   -system-jpeg)
	JPEG=system
	;;
   -no-nas-sound)
	NAS_SOUND=no
	;;
   -system-nas-sound)
	NAS_SOUND=yes
	;;
   -no-big-codecs)
	BIG_CODECS=no
	;;
   -big-codecs)
	BIG_CODECS=yes
	;;
   -no-xrender)
	XRENDER=no
	;;
   -xrender)
	XRENDER=yes
	;;
   -no-xft)
	XFTFREETYPE=no
	;;
   -xft)
	XFTFREETYPE=yes
	;;
   -no-xkb)
	XKB=no
	;;
   -xkb)
	XKB=yes
	;;
   -remote)
        REMOTE=yes
	;;
   -enable-*)
	MODULE=`echo $1 | sed "s/-enable-//"`
	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=`echo $1 | sed -e "s/-disable-//"`
	if [ -d "$relpath/src/$MODULE" ]
	then
	    MODULES=`echo $MODULES | sed -e "s/$MODULE//"`
	else
	    echo "Unknown module disabled: $MODULE"
	fi
	;;
    -internal)
	QMAKE_CONFIG="$QMAKE_CONFIG internal"
	;;
   -h | -help | --help)
	HELP=yes
	;;
    -no-*-*|-plugin-*-*|-qt-*-*)
	OPT=`echo $1 | sed "s,^-\([^-]*\).*,\1,"`
	VAR=`echo $1 | sed "s,^-[^-]*-\([^-]*\).*,\1,"`
	VAL=`echo $1 | sed "s,^-[^-]*-[^-]*-\([^-]*\).*,\1,"`
	if [ "$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
	;;
   -v)
	#takes two verboses to turn on qmake debugs
	[ "x$VERBOSE" = "xyes" ] && QMAKE_SWITCHES="$QMAKE_SWITCHES -d"
	VERBOSE=yes
	;;
   -j?*)
	CONCURRENT="`echo $1 | sed 's,-j,,'`"
	;;
   -D?*)
	D_FLAGS="$D_FLAGS `echo $1 | sed 's,-D,,'`"
	;;
   -I?*)
	I_FLAGS="$I_FLAGS $1"
	;;
   -L?*)
	L_FLAGS="$L_FLAGS $1"
	;;
   -R?*)
	R_FLAGS="$R_FLAGS \$\${QMAKE_RPATH}`echo $1 | sed 's,-R,,'`"
	;;
   -l?*)
	l_FLAGS="$l_FLAGS $1"
	;;
   -I)
	shift
	I_FLAGS="$I_FLAGS -I$1"
	;;
   -L)
	shift
	L_FLAGS="$L_FLAGS -L$1"
	;;
   -R)
	shift
	R_FLAGS="$R_FLAGS \$\${QMAKE_RPATH}`echo $1 | sed 's,-R,,'`"
	;;
   *)
	echo $1: unknown argument
	HELP=yes;
	ERROR=yes
	;;
    esac
    shift
done

if [ "$EMBEDDED" = "auto" ]
then
    UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown
    case "$UNAME_MACHINE" in
	*86) ARCH=x86 ;;
	*) ARCH=generic ;;
    esac
    EMBEDDED=$ARCH
    PLATFORM=qws/linux-generic-g++
    XPLATFORM=qws/linux-${EMBEDDED}-g++
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
	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 | xargs grep -v qws | sed "s,$relpath/mkspecs/qws/,,"`
    fi

    PLATFORM_NOTES=""

    case "$UNAME_SYSTEM:$UNAME_RELEASE" in
     Darwin:*)
	PLATFORM=macx-g++
	;;
     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-cxx
	"
	;;
     SunOS:5*)
	#PLATFORM=solaris-g++
	PLATFORM=solaris-cc
	PLATFORM_NOTES="
	    - Also available for Solaris: solaris-g++ solaris-64
	"
	;;
     SunOS:4*)
	PLATFORM=sunos-g++
	;;
     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 [ "$HELP" != "yes" ]
	then
	    echo
	    for p in $PLATFORMS
	    do
		echo "    $relconf $* -platform $p"
	    done
	    echo
	    echo "The build script does not currently recognize all platforms"
	    echo "supported by Qt."
	    echo "Rerun this script with a -platform option listed to"
	    echo "set the operating system / compiler combination you use."
	    echo
	    exit 2
	fi
    esac
fi

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

if [ "x$CONCURRENT" = "x0" ] && echo $MAKEFLAGS | grep '.* *-j[0-9]* *.*' >/dev/null 2>&1; then
    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
    echo
    echo '   The specified platform/compiler is not supported: ' $QMAKESPEC
    echo '   Please see the PLATFORMS file for a complete list.'
    echo
    echo
    exit 2
fi
if [ '!' -d "$XQMAKESPEC" ]
then
    echo
    echo
    echo '   The specified platform/xcompiler is not supported: ' $XQMAKESPEC
    echo '   Please see the PLATFORMS file for a complete list.'
    echo
    echo
    exit 2
fi

# Setup qplatformdefs.h
if [ '!' -f "${XQMAKESPEC}/qplatformdefs.h" ]
then
    echo
    echo
    echo '   The specified platform/compiler port is not complete: '
    echo
    echo '       ' $XQMAKESPEC/qplatformdefs.h
    echo
    echo '   Please contact qt-bugs@trolltech.com.'
    echo
    echo
    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
[ -d /System/Library/Frameworks/Carbon.framework ] && MAC_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
if [ $AUTOTHREAD = never ]
then
    if [ $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
[ $THREAD = auto ] && THREAD=$AUTOTHREAD

##### Tests

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

# Auto-detect SQL-modules support
if echo $MODULES | grep sql >/dev/null
then
    SQL_AVAIL=`find $relpath/plugins/src/sqldrivers/* -prune -type d -print | awk -F/ '{print $NF}'`
    SQL_AVAIL=`echo $SQL_AVAIL`
    MYSQL_REQ="-llibmysqlclient.* mysql.h"
    OCI_REQ="-llibclntsh.* -llibwtc8.* oci.h"
    PSQL_REQ="-llibpq.* postgres.h libpq/libpq-fs.h catalog/pg_type.h libpq-fe.h"
    ODBC_REQ="-llibodbc.* sql.h sqlext.h"
    TDS_REQ="-llibsybdb.* sybfront.h sybdb.h"
    for _SQLDR in $SQL_AVAIL
    do
	case $_SQLDR in
	mysql)
	    $unixtests/checkavail "MySQL" $VERBOSE $MYSQL_REQ $L_FLAGS $I_FLAGS && SQL_AUTODETECTED="$SQL_AUTODETECTED mysql"
	;;
	psql)
	    $unixtests/checkavail "PostgreSQL" $VERBOSE $PSQL_REQ $L_FLAGS $I_FLAGS && SQL_AUTODETECTED="$SQL_AUTODETECTED psql"
	    ;;
	odbc)
	    $unixtests/checkavail "ODBC" $VERBOSE $ODBC_REQ $L_FLAGS $I_FLAGS && SQL_AUTODETECTED="$SQL_AUTODETECTED odbc"
	    ;;
	oci)
	    $unixtests/checkavail "OCI" $VERBOSE $OCI_REQ $L_FLAGS $I_FLAGS && SQL_AUTODETECTED="$SQL_AUTODETECTED oci"
	    ;;
	tds)
	    $unixtests/checkavail "TDS" $VERBOSE $TDS_REQ $L_FLAGS $I_FLAGS && SQL_AUTODETECTED="$SQL_AUTODETECTED tds"
	    ;;
	*)
	    if [ "x$VERBOSE" = "xyes" ]
	    then
		echo "unknown SQL driver: $_SQLDR"
	    fi
	    ;;
	esac
    done
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 $THREAD $VERBOSE && MODULES=`echo $MODULES | sed -e 's/opengl//'`
    fi
    # Auto-detect XRender support
    if [ $XRENDER != no ]
    then
	XRENDER=yes
	$x11tests/xrender.test $XQMAKESPEC $VERBOSE && XRENDER=no && XFTFREETYPE=no
    else
	XFTFREETYPE=no
    fi
    # Auto-detect XftFreeType support
    if [ $XRENDER = yes ] && [ $XFTFREETYPE != no ]
    then
	XFTFREETYPE=yes
	$x11tests/xfreetype.test $XQMAKESPEC $VERBOSE $x11tests && XFTFREETYPE=no
    fi
    # Add freetype2 include path
    if [ $XFTFREETYPE = 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 [ $XFTFREETYPE = 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 [ $SM = auto ]
    then
	SM=yes
	$x11tests/sm.test $XQMAKESPEC $VERBOSE && SM=no
    fi
    # Auto-detect Xinerama support
    if [ $XINERAMA != no ]
    then
	XINERAMA=yes
	$x11tests/xinerama.test $XQMAKESPEC $VERBOSE && XINERAMA=no
    fi
    # Auto-detect Tablet support (currenlty only in IRIX)
    case $PLATFORM in
    irix-*)
	if [ $TABLET = auto ]
	then
	    TABLET=yes
	    $x11tests/xinput.test $XQMAKESPEC $VERBOSE && TABLET=no
	fi
	;;
    *)
	if [ $TABLET = auto ]
	then
	    TABLET=no
	fi
	;;
    esac
    # Auto-detect XKB support
    if [ $XKB != no ]
    then
	XKB=yes
	$x11tests/xkb.test $XQMAKESPEC $VERBOSE && XKB=no
    fi
    # Work around broken X11 headers when using GCC 2.95 or later
    NOTYPE=no
    $x11tests/notype.test $XQMAKESPEC $VERBOSE $relpath $outpath && NOTYPE=yes
    if [ "x$NOTYPE" = "xyes" ]
    then
	QMAKE_VARS="$QMAKE_VARS  \"QMAKE_CXXFLAGS+=-fpermissive\""
    fi
fi

# Next, emit a usage message if something failed.
if [ "$HELP" = "yes" ]
then
    [ "x$ERROR" = "xyes" ] && echo
    if [ "x$THREAD" = "xyes" ]
    then
	THY="*"
	THN=" "
    else
	THY=" "
	THN="*"
    fi
    if [ "x$STL" = "xauto" ]
    then
	SHY="+"
	SHN=" "
    else
	SHY=" "
	SHN="*"
    fi

    cat <<EOF
Usage:  $relconf [-prefix dir] \\
	[-docdir dir] [-headerdir dir] [-libdir dir] [-bindir dir] \\
	[-debug] [-release] [-qt-gif] [-no-gif] [-sm] [-no-sm] [-stl] [-no-st] \\
	[-qt-zlib] [-system-zlib] [-qt-libpng] [-system-libpng] \\
	[-no-jpeg] [-system-jpeg] [-no-thread] [-thread] \\
	[-Istring] [-lstring] [-Lstring] [-Rstring] [-enable-<module>] \\
	[-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)

 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

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

 *  -no-jpeg ........... Do not compile in JPEG I/O support.
    -qt-jpeg ........... Use the libjpeg bundled with Qt.
    -system-jpeg ....... Use jpeglib from the operating system.
			 See http://www.ijg.org

 *  -no-mng ............ Do not compile in MNG I/O support.
    -qt-libmng ......... Use the libmng bundled with Qt.
    -system-libmng ..... Use libmng from the operating system.
			 See http://www.libmng.com

 *  -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

    -make directory .... This will generate makefiles for all project files
			 in directory. You can specify this option multiple
			 times so as to only make specific directories.
    -nomake regexp ..... This will prevent matches of regexp from being built
			 you may use this to exclude certain projects in
			 a directory included in -make.

    -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 platform 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.


    -enable-<module> .......... Enables a module where module is one of:
				$AVAIL_MODULES
    -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>: [ $SQL_AVAIL ]
                                Auto-Detected on this system: [$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 with threading support.
 $THY  -thread ............ Compile with threading support.

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

EOF

if [ "$X11_PLATFORM" = "yes" ]; then
    if [ "x$SM" = "xno" ]
    then
	SMY=" "
	SMN="*"
    else
	SMY="*"
	SMN=" "
    fi
    if [ "x$XINERAMA" = "xno" ]
    then
	XRY=" "
	XRN="*"
    else
	XRY="*"
	XRN=" "
    fi
    if [ "x$XFTFREETYPE" = "xno" ]
    then
	XFTY=" "
	XFTN="*"
    else
	XFTY="*"
	XFTN=" "
    fi
    if [ "x$XRENDER" = "xno" ]
    then
	XRY=" "
	XRN="*"
    else
	XRY="*"
	XRN=" "
    fi
    if [ "x$TABLET" = "xno" ]
    then
	XIY=" "
	XIN="*"
    else
	XIY="*"
	XIN=" "
    fi
    if [ "x$XKB" = "xno" ]
    then
        XKY=" "
        XKN="*"
    else
        XKY="*"
        XKN=" "
    fi
    cat << EOF

Qt/X11 only:

 $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 (currently only on IRIX).

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

EOF
fi

if [ "$QWS_CANDIDATE" = "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-libpng ......... Turn off PNG support
    -no-zlib ........... No compression library -- implies -no-libpng

    -qconfig local ..... Use src/tools/qconfig-local.h rather than the
			 default ($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

#do the qmake..

if [ -x "$relpath/bin/syncqt" ]
then
    QTDIR=$relpath $relpath/bin/syncqt -inc $relpath/include

    # when doing shadow builds, make sure we have includes there too
    if [ "$relpath" != `pwd` ]
    then
	QTDIR=$relpath $relpath/bin/syncqt -inc $outpath/include
    fi
fi

if true #[ '!' -f "$outpath/bin/qmake" ]
then
    echo "Creating qmake. Please wait..."
    rm -f mkspecs/default
    ln -s $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 [ "$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_CFLAGS+=-I/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/" >>$mkfile
	  echo "CARBON_LFLAGS +=-framework Carbon" >> $mkfile
	  EXTRA_LFLAGS="$EXTRA_LFLAGS \$(CARBON_LFLAGS)"
	  EXTRA_CFLAGS="$EXTRA_CFLAGS \$(CARBON_CFLAGS)"
        fi
	if [ -z "$QT_INSTALL_BINS" ]; then
	   BIN_DIR="${QT_INSTALL_PREFIX}/bin"
	else
	   BIN_DIR="$QT_INSTALL_BINS"
        fi
        echo >>$mkfile
	sed -e "s,@REL_QTDIR@,$relpath,g" -e "s,@OUT_QTDIR@,$outpath,g" \
	    -e "s,@QMAKE_CFLAGS@,$EXTRA_CFLAGS," -e "s,@QMAKE_LFLAGS@,$EXTRA_LFLAGS," \
	    -e "s,@QT_INSTALL_BINS@,$BIN_DIR,g" -e "s,@QT_INSTALL_PREFIX@,$QT_INSTALL_PREFIX,g" \
	    -e "s,@QMAKESPEC@,$QMAKESPEC,g" $in_mkfile >>$mkfile
    done

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

#stl must be way down here after qmake has been made, because the test itself uses qmake
if [ $STL = auto ]
then
    STL=yes
    $unixtests/stl.test $XQMAKESPEC $VERBOSE $relpath $outpath && STL=no
fi

if [ "$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 QWS_DEPTHS
    if [ -z "$QWS_DEPTHS" ]
    then
	QWS_DEPTHS=8,16
    fi
fi

if [ -n "$QWS_DEPTHS" -a "$EMBEDDED" != "no" ]
then
    DG=;DV=;D4=;D8=;D16=;D24=;D32=
    for D in `echo "$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

if [ "$QWS_QVFB" = prompted -a "$EMBEDDED" != no ]
then
    echo
    echo "Enable Qt Virtual Framebuffer support for development on X11 (default yes)"
    read QWS_QVFB
    if [ "$QWS_QVFB" != n -a "$QWS_QVFB" != no ]
    then
	QWS_QVFB=y
    else
	QWS_QVFB=n
    fi
fi


if [ -z "$QWS_ACCEL_VOODOO3" -a "$EMBEDDED" != no ]
then
    D_FLAGS="$D_FLAGS QT_NO_QWS_VOODOO3"
fi
if [ -z "$QWS_ACCEL_MACH64" -a "$EMBEDDED" != no ]
then
    D_FLAGS="$D_FLAGS QT_NO_QWS_MACH64"
fi
if [ -z "$QWS_REPEATER" -a "$EMBEDDED" != no ]
then
    D_FLAGS="$D_FLAGS QT_NO_QWS_REPEATER"
fi
if [ -z "$QWS_ACCEL_MATROX" -a "$EMBEDDED" != no ]
then
    D_FLAGS="$D_FLAGS QT_NO_QWS_MATROX"
fi
if [ "$QWS_QVFB" = "n" -a "$EMBEDDED" != no ]
then
    D_FLAGS="$D_FLAGS QT_NO_QWS_VFB"
fi
if [ -z "$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

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

case "$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 [ "$QCONFIG" = $c ]
	then
	    break;
	fi
    done
    ;;
  *)
    # Not known to be sufficient for anything.
    if [ ! -f $relpath/src/tools/qconfig-$QCONFIG.h ]
    then
	echo >&2 "No such configuration: $QCONFIG"
	HELP=yes
    fi
esac
case "$QCONFIG" in
  full)
    echo "// Everything" >$outpath/include/qconfig.h.new
    ;;
  *)
    QCONFIGFILE=qconfig-$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
QCONF_CHANGES=`diff $outpath/include/qconfig.h $outpath/include/qconfig.h.new 2>&1 | wc -l | sed "s, ,,g"`
if [ -f $outpath/include/qconfig.h ] && [ "x$QCONF_CHANGES" = "x0" ]
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



# 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 [ "x$DEBUG" = "xyes" ]
then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}debug"
    QMAKE_CONFIG="$QMAKE_CONFIG debug"
fi
if [ "x$DEBUG" = "xno" ]
then
    QMAKE_OUTDIR="${QMAKE_OUTDIR}release"
    QMAKE_CONFIG="$QMAKE_CONFIG release"
fi
if [ "x$THREAD" = "xyes" ]
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
QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBDIR_QT=$qdir/lib\""
QMAKE_VARS="$QMAKE_VARS \"OBJECTS_DIR=.obj/$QMAKE_OUTDIR\" \"MOC_DIR=.moc/$QMAKE_OUTDIR\""

if [ "x$SHARED" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG staticlib"
elif [ "x$SHARED" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG dll"
fi
if [ "x$JPEG" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-jpeg"
elif [ "x$JPEG" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG jpeg"
elif [ "x$JPEG" = "xsystem" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG system-jpeg"
fi
if [ "x$MNG" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-mng"
elif [ "x$MNG" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG mng"
elif [ "x$MNG" = "xsystem" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG system-mng"
fi
if [ "x$GIF" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-gif"
elif [ "x$GIF" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG gif"
fi
if [ "x$ZLIB" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-zlib"
elif [ "x$ZLIB" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG zlib"
elif [ "x$ZLIB" = "xsystem" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG system-zlib"
fi
if [ "x$LIBPNG" = "xno" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG no-png"
elif [ "x$LIBPNG" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG png"
elif [ "x$LIBPNG" = "xsystem" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG system-png"
fi
[ "x$NAS_SOUND" = "xyes" ] && QMAKE_CONFIG="$QMAKE_CONFIG nas"
[ "x$BIG_CODECS" = "xyes" ] && QMAKE_CONFIG="$QMAKE_CONFIG bigcodecs"

if [ "x$REMOTE" = "xyes" ]; then
    QMAKE_CONFIG="$QMAKE_CONFIG remote"
    QMAKE_VARS="$QMAKE_VARS \"DEFINES+=QT_REMOTE_CONTROL\""
fi

[ "x$FT" = "xyes" -a "x$EMBEDDED" != "xno" ] && QMAKE_CONFIG="$QMAKE_CONFIG ft"
if [ "$X11_PLATFORM" = "yes" ]
then
    [ "x$SM" = "xyes" ] && 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 [ "x$XINERAMA" = "xyes" ]
    then
	QMAKE_CONFIG="$QMAKE_CONFIG xinerama"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXinerama\""
    fi
    if [ "x$XRENDER" = "xyes" ]
    then
	QMAKE_CONFIG="$QMAKE_CONFIG xrender"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXrender\""
    fi
    if [ "x$XFTFREETYPE" = "xyes" ]
    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 [ "x$TABLET" = "xyes" ]
    then
	QMAKE_CONFIG="$QMAKE_CONFIG tablet"
	QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBS_X11+=-lXi\""
    fi
    if [ "x$XKB" = "xyes" ]
    then
        QMAKE_CONFIG="$QMAKE_CONFIG xkb"
    fi
fi

[ -z "$QMAKE_PROJECTS" ] && QMAKE_PROJECTS=`find $relpath/. -name '*.pro' -print | sed 's-/\./-/-'`
[ '!' -z "$D_FLAGS" ] && QMAKE_VARS="$QMAKE_VARS \"DEFINES+=$D_FLAGS\""
if [ '!' -z "$l_FLAGS" ] || [ '!' -z "$L_FLAGS" ]
then
    QMAKE_VARS="$QMAKE_VARS \"LIBS+=$L_FLAGS $l_FLAGS\""
fi
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_LIBS" ] && QMAKE_VARS="$QMAKE_VARS \"QT_INSTALL_LIBPATH=${QT_INSTALL_LIBS}\""
if [ '!' -z "$QT_INSTALL_BINS" ]; then
    QMAKE_VARS="$QMAKE_VARS \"QT_INSTALL_BINPATH=${QT_INSTALL_BINS}\""
else
    QT_INSTALL_BINS=${QT_INSTALL_PREFIX}/bin
fi

sed "s,\(INSTALL_BIN_DIR=\).*$,\1${QT_INSTALL_BINS}," Makefile >Makefile.tmp
mv -f Makefile.tmp Makefile


#do modules.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 '[a-z]' '[A-Z]'`
	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 [ "$STL" = "no" ]
then
cat >>$outpath/include/qmodules.h.new << EOF
#ifndef QT_NO_STL
#define QT_NO_STL
#endif
EOF
fi
#because things depend on qmodules.h, I conditionally copy
#so I don't rebuild them when I configure but change nothing...
QMOD_CHANGES=`diff $outpath/include/qmodules.h $outpath/include/qmodules.h.new 2>&1 | wc -l | sed "s, ,,g"`
if [ -f $outpath/include/qmodules.h ] && [ "x$QMOD_CHANGES" = "x0" ]
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

##
# Create the .qmake.cache file
CACHEFILE=$outpath/.qmake.cache
[ -f $CACHEFILE ] && rm -f $CACHEFILE
cat >>$CACHEFILE <<EOF
CONFIG += $QMAKE_CONFIG dylib
QMAKESPEC = $XPLATFORM
QT_SOURCE_TREE = $relpath
QT_BUILD_TREE = $outpath
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=""
[ "$DO_INCREMENT" = "auto" ] && which p4 >/dev/null 2>&1 && [ "$Edition" = "troll" ] && DO_INCREMENT="yes"
if [ "$DO_INCREMENT" = "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...

# Check if we are using gcc/g++
COMPILER=`echo $PLATFORM | cut -f 2- -d-`
if [ "$COMPILER" = "g++" ]
then
    if [ "$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 [ "x$VERBOSE" = "xyes" ]
then
    echo "QMake vars....... $QMAKE_VARS"
    echo "QMake switches... $QMAKE_SWITCHES"
fi

[ "x$CONCURRENT" != "x0" ] && echo "Concurrent........... $CONCURRENT"
[ "x$DO_INCREMENT" = "xyes" ] && [ ! -z "$INCREMENTAL" ] && echo "Incremental.......... $INCREMENTAL"
echo "Configuration........ $QMAKE_CONFIG"
[ "x$REMOTE" = "xyes" ] && echo "Remote support....... $REMOTE"
echo "STL support.......... $STL"
echo "Thread support....... $THREAD"
echo "GIF support.......... $GIF"
echo "MNG support.......... $MNG"
echo "JPEG support......... $JPEG"
echo "NAS sound support.... $NAS_SOUND"
[ "$QWS_PLATFORM" = "yes" ] && echo "Embedded support..... $EMBEDDED"
if [ "$X11_PLATFORM" = "yes" ]
then
    echo "Session management... $SM"
    echo "Xinerama support..... $XINERAMA"
    echo "Tablet support ...... $TABLET"
    echo "XRender support ..... $XRENDER"
    echo "XftFreeType support . $XFTFREETYPE"
    echo "XKB Support ......... $XKB"
fi
echo

QMAKE_ALL_ARGS="\"CONFIG+=$QMAKE_CONFIG\" $QMAKE_VARS $QMAKE_SWITCHES"
echo Creating makefiles...
sepath=`echo $relpath | sed -e 's/\\./\\\\./g'`
PROCS=1
PIPE=""
EXEC=""

# Work around a segfaulting uniq(1)
rm -f .sorted.projects
for p in `echo $QMAKE_PROJECTS`
do
    echo $p >> .sorted.projects
done
for p in `echo $QMAKE_IGNORE_PROJECTS`
do
    grep -v $p .sorted.projects > .sorted.projects.new
    mv -f .sorted.projects.new .sorted.projects
done
sort .sorted.projects > .sorted.projects.new
mv -f .sorted.projects.new .sorted.projects
uniq .sorted.projects > .sorted.projects.new
mv -f .sorted.projects.new .sorted.projects
for a in `cat .sorted.projects`
do
    case $a in
    *-kde.pro|*qtmain.pro) continue ;;
    *-pro.pro)
	if [ "x$Edition" != "xpro" ]
	then
	    continue
	fi
	;;
    *moc*) SPEC=$QMAKESPEC; echo "USING $SPEC FOR MOC" ;;
    *) 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_EXTRA_ARGS="\"QMAKE_ABSOLUTE_SOURCE_PATH=$relpath/$dir\""
    PIPE="$outpath/bin/qmake -spec $SPEC $QMAKE_SWITCHES $QMAKE_EXTRA_ARGS -o $N $a"
    if [ "x$CONCURRENT" = "x0" ] || [ "x$CONCURRENT" = "x1" ]
    then
	EXEC=$PIPE
    else
	EXEC="$EXEC ($PIPE & ) ;"
	if [ "x$PROCS" != "x$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 [ "x$VERBOSE" = "xyes" ]
    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 incase the concurrant 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 put 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 $CONFIG_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

