#!/bin/sh
#
# Configures to build the TinyQ
#


#-------------------------------------------------------------------------------
# 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
QWS_PLATFORM=no


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

Edition="free"
Licensee="Non-Commercial"
Products="qt-free"

# check licensed modules
MODULES="kernel tools xml"
AVAIL_MODULES=$MODULES
QMAKE_VARS="$QMAKE_VARS \"QT_PRODUCT=$Products\""


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

# initalize internal variables
CFG_LAZY_DEPS_ALLOWED=no
CFG_GPLUSPLUS_EXCEPTIONS=unspecified
CFG_INCREMENTAL=auto
CFG_QCONFIG=tinyq
EMBEDDED=no
CFG_DEBUG=no
CFG_SHARED=yes
CFG_GIF=no
CFG_THREAD=no
CFG_SM=no
CFG_XINERAMA=no
CFG_BIG_CODECS=no
CFG_ZLIB=no
CFG_PNG=no
CFG_LIBPNG=qt
CFG_JPEG=no
CFG_LIBJPEG=qt
CFG_MNG=no
CFG_LIBMNG=qt
CFG_XRENDER=no
CFG_FREETYPE=no
CFG_QWS_FREETYPE=no
CFG_SQL_AVAILABLE=no
CFG_SQL_AUTODETECTED=no
CFG_TABLET=no
CFG_XKB=no
CFG_NIS=no
CFG_STL=no
CFG_NAS=no
CFG_REMOTE=no
CFG_QWS_QVFB=no
CFG_QWS_DEPTHS=no
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/tinyq
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
    case $1 in
    -no-lazydeps)
	CFG_LAZY_DEPS_ALLOWED=no
	;;
    -prefix)
	shift
	QT_INSTALL_PREFIX=$1
	;;
    -docdir)
	shift
	QT_INSTALL_DOCS=$1
	;;
    -headerdir)
	shift
	QT_INSTALL_HEADERS=$1
	;;
    -datadir)
        shift
	QT_INSTALL_DATA=$1
	;;
    -libdir)
	shift
	QT_INSTALL_LIBS=$1
	;;
    -bindir)
	shift
	QT_INSTALL_BINS=$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)
	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\""
	;;
    -platform)
	shift
	PLATFORM=$1
	;;
    -xplatform)
	shift
	XPLATFORM=$1
	;;
    -release)
	CFG_DEBUG=no
	;;
    -debug)
	CFG_DEBUG=yes
	;;
    -shared)
	CFG_SHARED=yes
	;;
    -static)
	CFG_SHARED=no
	;;
    -no-thread)
	CFG_THREAD=no
	;;
    -thread)
	CFG_THREAD=yes
	;;
    -stl)
	CFG_STL=yes
	;;
    -no-stl)
	CFG_STL=no
	;;
    -no-nis)
	CFG_NIS=no
	;;
    -nis)
	CFG_NIS=yes
	;;
    -internal)
	QMAKE_CONFIG="$QMAKE_CONFIG internal"
	;;
    -h | -help | --help)
	OPT_HELP=yes
	;;
    -v)
	# takes two verboses to turn on qmake debugs
	[ "$OPT_VERBOSE" = "yes" ] && QMAKE_SWITCHES="$QMAKE_SWITCHES -d"
	OPT_VERBOSE=yes
	;;
    -j?*)
	OPT_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"
	OPT_HELP=yes;
	ERROR=yes
	;;
    esac
    shift
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 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 [ -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=""

    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

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

#-------------------------------------------------------------------------------
# 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] [-datadir dir]
	[-debug] [-release] [-stl] [-no-stl]
	[-no-thread] [-thread]
	[-Istring] [-lstring] [-Lstring] [-Rstring]

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)
    -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 (libtinyq.so).
    -static ............ Create and use a static Qt library (libtinyq.a).

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


 $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.
EOF

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


# -----------------------------------------------------------------------------
# LICENSING, INTERACTIVE PART
# -----------------------------------------------------------------------------

echo
echo "This is TinyQ, based on the Qt X11 free GPL Edition."
echo
echo "You are licensed to use this software under the terms the GNU General Public"
echo "License (GPL)."
echo
echo "Installing this software implies that you accept the terms of this license."
echo "Read the LICENSE.GPL file for detailed information."
echo

# -----------------------------------------------------------------------------
# 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
fi

# build qmake
if true ###[ '!' -f "$outpath/bin/qmake" ]
then
    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
	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}"
        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@,\$(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
#-------------------------------------------------------------------------------

# 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


#-------------------------------------------------------------------------------
# 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 "$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
QMAKE_VARS="$QMAKE_VARS \"QMAKE_LIBDIR_QT=$outpath/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 [ "$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"

[ '!' -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 '[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 [ "$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 "STL support ......... $CFG_STL"
echo "Thread support ...... $CFG_THREAD"

# 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


syntax highlighted by Code2HTML, v. 0.9.1