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

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

relpath=`dirname $0`
relpath=`(cd $relpath; pwd)`
HELP=

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

touch .test.qt.
if [ '!' -f ${QTDIR}/.test.qt. ];
then
    rm .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 .test.qt.

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

# Check licensed modules
MODULES=`$MAKE -f src/Makefile.in listallmodules`


# Free edition - may only be used under the terms of the QPL.
if [ -f LICENSE.QPL -a -f LICENSE.GPL ]
then
    Edition=free
    Licensee=you
    while true
    do
	echo
	echo "This is the Qt 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."
	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 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 LICENSE.GPL
	    elif [ "$acceptance" = Q -o "$acceptance" = q ]
	    then
		more LICENSE.QPL
	    fi
	fi
    done
else
    if [ -f LICENSE ]
    then
	echo
	if [ -f configs/linux-generic-g++-shared ]
	then
	    EMB="/Embedded"
	else
	    EMB=""
	fi
	E="Qt$EMB Professional Edition"
	Edition=pro

	if [ -f $HOME/.qt-license ]
	then
	    . $HOME/.qt-license
	    if echo $Products | grep >/dev/null 'qt-enterprise'
	    then
		E="Qt$EMB Enterprise Edition"
		Edition=ent
	    else
		MODULES="$Products "`$MAKE -f src/Makefile.in listpromodules`
	    fi
	    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
	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 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

# Remove the build options file
[ -f .buildopts ] && rm .buildopts

fi # help

# Defaults

KDE=no
VERBOSE=
DEBUG=no
SHARED=yes
GIF=no
THREAD=auto
SM=auto
ZLIB=yes
LIBPNG=yes
JPEG=no
MNG=no
NAS_SOUND=no
I_FLAGS=
L_FLAGS=
R_FLAGS=
l_FLAGS=
XPLATFORM=
FROMMAKE=
QT_CXX=""


# Parse the arguments, setting things to "yes" or "no".

while [ -n "$1" ]; do
   case $1 in
   -frommake)
       FROMMAKE=yes
	;;
   -platform)
       shift; PLATFORM=$1
	;;
   -xplatform)
       shift; XPLATFORM=$1
	;;
   -release)
       DEBUG=no
	;;
   -debug)
       DEBUG=yes
	;;
   -shared)
       SHARED=yes
	;;
   -static)
       SHARED=no
	;;
   -no-gif)
       GIF=no
	;;
   -gif)
       GIF=yes
	;;
   -no-sm)
       SM=no
	;;
   -sm)
       SM=yes
	;;
   -no-thread)
       THREAD=no
	;;
   -thread)
       THREAD=yes
	;;
   -qt-zlib)
       ZLIB=yes
	;;
   -system-zlib)
       ZLIB=no
	;;
   -qt-libpng)
       LIBPNG=yes
	;;
   -system-libpng)
       LIBPNG=no
	;;
   -no-mng)
       MNG=no
	;;
   -system-libmng)
       MNG=yes
	;;
   -no-jpeg)
       JPEG=no
	;;
   -system-jpeg)
       JPEG=yes
	;;
   -no-nas-sound)
       NAS_SOUND=no
	;;
   -system-nas-sound)
       NAS_SOUND=yes
	;;
   -kde)
       KDE=yes
	;;
   -no-*)
       MODULE=`echo $1 | sed -e "s/-no-//"`;
       if [ -d "src/$MODULE" ]
       then
	   MODULES=`echo $MODULES | sed -e "s/$MODULE//"`
       else
	   FEATURE=`echo $1 | sed -e "s/-/_/g" | tr '[a-z]' '[A-Z]'`;
	   QT_CXX="$QT_CXX -DQT"`echo $1 | sed -e "s/-/_/g" | tr '[a-z]' '[A-Z]'`
       fi
	
    #buildpath=`dirname $a | sed -e "s;$sepath;.;g"`
	;;
   -h | -help | --help)
       HELP=yes
	;;
   -v)
       VERBOSE=yes
        ;;
   -I?*)
	I_FLAGS="$I_FLAGS $1"
	;;
   -L?*)
	L_FLAGS="$L_FLAGS $1"
	;;
   -R?*)
	R_FLAGS="$R_FLAGS $1"
	;;
   -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 -R$1"
	;;
   *)
       echo $1: unknown argument
       HELP=yes;
       ERROR=yes
	;;
   esac
   shift
done

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 [ -f configs/linux-generic-g++-shared ]
    then
	# This is a Qt/Embedded distribution
	SM=no
	MODULES=`echo $MODULES | sed -e 's/\<opengl\>//'`
	PLATFORMS="linux-generic-g++ linux-mips-g++ linux-x86-g++ linux-arm-g++"
	case "$UNAME_MACHINE" in
	 *86) ARCH=x86-
	;; *) ARCH=generic-
	esac
    else
	##### Generate?  Look in configs/?
	PLATFORMS="aix-g++ aix-xlc bsdi-g++ dgux-g++ freebsd-g++ gnu-g++ hpux-acc
	    hpux-cc hpux-g++ hpux-n64 hpux-o64 irix-64 irix-dcc irix-g++ irix-n32
	    irix-o32 linux-g++ netbsd-g++ openbsd-g++ osf1-cxx osf1-g++ qnx-g++
	    sco-g++ solaris-cc-gcc solaris-cc solaris-64 solaris-g++ sunos-g++
	    ultrix-g++ unixware-g++ unixware7-cc unixware7-g++"
    fi

    PLATFORM_NOTES=""

    case "$UNAME_SYSTEM:$UNAME_RELEASE" in
     AIX*)
        #PLATFORM=aix-g++
	PLATFORM=aix-xlc
	PLATFORM_NOTES="
	    - Also available: 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++
	;;
     IRIX64:*)
	PLATFORM=irix-64
	;;
     IRIX:*)
	#PLATFORM=irix-dcc
	#PLATFORM=irix-g++
	PLATFORM=irix-n32
	#PLATFORM=irix-o32
	PLATFORM_NOTES="
	    - Also available for IRIX: irix-dcc irix-g++ irix-o32
	"
	;;
     HP-UX:*)
	#PLATFORM=hpux-acc
	#PLATFORM=hpux-cc
	#PLATFORM=hpux-n64
	#PLATFORM=hpux-o64
	PLATFORM=hpux-g++
	PLATFORM_NOTES="
	    - Also available for HP-UX: hpux-acc hpux-cc hpux-n64 hpux-o64
	"
	;;
     OSF1:*)
	#PLATFORM=osf1-cxx
	PLATFORM=osf1-g++
	PLATFORM_NOTES="
	    - Also available for OSF1: osf1-cxx
	"
	;;
     Linux:*)
	PLATFORM=linux-${ARCH}g++
	;;
     SunOS:5*)
	#PLATFORM=solaris-g++
	PLATFORM=solaris-cc
	PLATFORM_NOTES="
	    - Also available for SunOS:5: solaris-64 solaris-g++
	    - To mix GNU C and SunPRO C++, add -platform solaris-cc-gcc
	"
	;;
     SunOS:4*)
	PLATFORM=sunos-g++
	PLATFORM_NOTES="
	    - Mixing GNU C and SunPRO C++ is not supported
	"
	;;
     UNIX_SV:4.2*)
	PLATFORM=unixware-g++
	;;
     UnixWare:5*)
	PLATFORM=unixware7-cc
	#PLATFORM=unixware7-g++
	PLATFORM_NOTES="
	    - Also available for UnixWare:5: unixware7-g++
	"
	;;
     *:3.2)
	PLATFORM=sco-g++
	;;
     *)
	if [ "$HELP" != "yes" ]; then
	    echo
	    for p in $PLATFORMS
	    do
		echo "    $0 $* -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

# Check if the platform + static + debug exists

if [ -z "$XPLATFORM" ]
then
    XPLATFORM="$PLATFORM"
fi

TARGET=$PLATFORM
XTARGET=$XPLATFORM
if [ "x$SHARED" = "xyes" ]; then
    TARGET="${TARGET}-shared"
    XTARGET="${XTARGET}-shared"
    if [ '!' -f $relpath/configs/$TARGET ]; then
        echo
        echo
        echo '    No shared library support for platform/compiler ' $TARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
    if [ '!' -f $relpath/configs/$XTARGET ]; then
        echo
        echo
        echo '    No shared library support for platform/compiler ' $XTARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
else
    TARGET="${TARGET}-static"
    XTARGET="${XTARGET}-static"
    if [ '!' -f $relpath/configs/$TARGET ]; then
        echo
        echo
        echo '     No static library support for platform/compiler ' $TARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
    if [ '!' -f $relpath/configs/$XTARGET ]; then
        echo
        echo
        echo '     No static library support for platform/compiler ' $XTARGET
        echo '    Please see the PLATFORMS file for more information.'
        echo
        echo
        exit 2
    fi
fi

if [ "x$DEBUG" = "xyes" ]; then
    TARGET="${TARGET}-debug"
    XTARGET="${XTARGET}-debug"
fi

CONFIG="$relpath/configs/${TARGET}"
XCONFIG="$relpath/configs/${XTARGET}"

if [ -z "$TARGET" -o '!' -f "$CONFIG" ];
then
    echo
    echo
    echo '   The specified platform/compiler not supported: ' $TARGET
    echo '   Please see the PLATFORMS file for a complete list.'
    echo
    echo
    exit 2
fi
if [ -z "$XTARGET" -o '!' -f "$XCONFIG" ];
then
    echo
    echo
    echo '   The specified platform/compiler not supported: ' $XTARGET
    echo '   Please see the PLATFORMS file for a complete list.'
    echo
    echo
    exit 2
fi

if [ ! -f configs/linux-generic-g++-shared ]
then
    # Auto-detect opengl support
    LIBS=`sed -n -e '/^SYSCONF_LIBS_OPENGL[	]*=/ { s/[^=]*=[	 ]*//; s/-l/ /g; p; }' $XCONFIG`
    for L in $LIBS
    do
	F=
	for LIB in `sed -n -e '/^SYSCONF_LFLAGS_OPENGL[	]*=/ { s/[^=]*=[	 ]*//; s/-L/ /g; p; }' $XCONFIG` /usr/lib /lib
	do
	    if [ "`echo $LIB/lib$L.*`" != "$LIB"/lib$L.\* ]
	    then
		F=yes
		break
	    fi
	done
	if [ -z "$F" ]
	then
	    MODULES=`echo $MODULES | sed -e 's/\<opengl\>//'`
	    break
	fi
    done
fi


# Find default thread option for target
if grep >/dev/null '^SYSCONF_LIBS_THREAD' $XCONFIG
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 $XCONFIG"
	echo
	exit 2
    fi
    AUTOTHREAD=no
fi
if [ $THREAD = auto ]
then
    THREAD=$AUTOTHREAD
fi

# Auto-detect session management support
if [ $SM = auto ]
then
    SM=no
    for INC in `sed -n -e '/^SYSCONF_CXXFLAGS_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-I/ /g; p; }' $XCONFIG`
    do
	if [ -f $INC/X11/SM/SM.h ]
	then
	    SM=yes
	    break
	fi
    done
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$SM" = "xno" ]
    then
	SMY=" "
	SMN="*"
    else
	SMY="*"
	SMN=" "
    fi


    MODULES_HELP=`$MAKE -f src/Makefile.in listaddonentmodules`
    if [ -f $HOME/.qt-license ]
    then
    . $HOME/.qt-license
	    if echo $Products | grep >/dev/null 'qt-enterprise'
	    then
		MODULES_HELP="opengl table network canvas"
	    else
		MODULES_HELP=""
	    fi
    else
	MODULES_HELP="opengl table network canvas"
    fi

    cat <<EOF
Usage: $0 [-debug] [-release] [-gif] [-no-gif] [-sm] [-no-sm] \\
        [-qt-zlib] [-system-zlib] [-qt-libpng] [-system-libpng] \\
        [-no-jpeg] [-system-jpeg] [-no-thread] [-thread] [-kde] \\
        [-Istring] [-lstring] [-Lstring] [-Rstring] [-no-<module>] \\

The defaults (*) are usually acceptable.  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.
    -gif ............... Compile in GIF reading support. See src/kernel/qgif.h

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

 $THN  -no-thread ......... Do not compile with Threading Support
 $THY  -thread ............ Compile with Threading Support

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

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

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

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

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

    -kde ............... Builds the Qt Designer with KDE2 support, so that
			 KDE2 widgets can be used directly in
			 the Qt Designer. \$KDEDIR must be
			 set to point to a KDE2 installation.
                         See http://www.kde.org

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

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


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

    -no-<module> ....... Disables a module, where module can
                         can be one of: $MODULES_HELP
EOF

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



# Build up the variables for output

QT_LIBS=""
QT_MODOBJ=""

[ "x$GIF" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_BUILTIN_GIF_READER=1"
[ "x$GIF" = "xno" ] && QT_CXX="${QT_CXX} -DQT_BUILTIN_GIF_READER=0"
[ "x$JPEG" = "xno" ] && QT_CXX="${QT_CXX} -DQT_NO_IMAGEIO_JPEG"
[ "x$MNG" = "xno" ] && QT_CXX="${QT_CXX} -DQT_NO_IMAGEIO_MNG"
[ "x$NAS_SOUND" = "xyes" ] && QT_CXX="${QT_CXX} -DQT_NAS_SUPPORT"
[ "x$SM" = "xno" ] && QT_CXX="${QT_CXX} -DQT_NO_SM_SUPPORT"
if [ "x$THREAD" = "xyes" ]
then
    cat >src-mt.mk <<EOF
SHELL=/bin/sh

src-mt:
	cd src; \$(MAKE) \
	    'QT_THREAD_SUFFIX=-mt' \
	    'QT_LFLAGS_MT=\$\$(SYSCONF_LFLAGS_THREAD) \$\$(SYSCONF_LIBS_THREAD)' \
	    'QT_CXX_MT=\$\$(SYSCONF_CXXFLAGS_THREAD) -DQT_THREAD_SUPPORT' \
	    'QT_C_MT=\$\$(SYSCONF_CFLAGS_THREAD) -DQT_THREAD_SUPPORT'
	cd examples/guithreads; \$(MAKE) \
	    'QT_THREAD_SUFFIX=-mt' \
	    'QT_LFLAGS_MT=\$\$(SYSCONF_LFLAGS_THREAD) \$\$(SYSCONF_LIBS_THREAD)' \
	    'QT_CXX_MT=\$\$(SYSCONF_CXXFLAGS_THREAD) -DQT_THREAD_SUPPORT' \
	    'QT_C_MT=\$\$(SYSCONF_CFLAGS_THREAD) -DQT_THREAD_SUPPORT'
	cd examples/semaphores; \$(MAKE) \
	    'QT_THREAD_SUFFIX=-mt' \
	    'QT_LFLAGS_MT=\$\$(SYSCONF_LFLAGS_THREAD) \$\$(SYSCONF_LIBS_THREAD)' \
	    'QT_CXX_MT=\$\$(SYSCONF_CXXFLAGS_THREAD) -DQT_THREAD_SUPPORT' \
	    'QT_C_MT=\$\$(SYSCONF_CFLAGS_THREAD) -DQT_THREAD_SUPPORT'
	cd src; \$(MAKE) clean
EOF
else
    cat >src-mt.mk <<EOF
all:
	@echo "(not building threaded Qt)"
EOF
fi
if [ "x$ZLIB" = "xyes" ]
then
    I_FLAGS="${I_FLAGS} -I\$(QTDIR)/src/3rdparty/zlib"
    if [ -n "$EMB" ]
    then
	QT_SUBLIBS="${QT_SUBLIBS} tmp/libz.a"
    fi
fi
if [ "x$LIBPNG" = "xyes" ]
then
    I_FLAGS="${I_FLAGS} -I\$(QTDIR)/src/3rdparty/libpng"
    if [ -n "$EMB" ]
    then
	QT_SUBLIBS="${QT_SUBLIBS} tmp/libpng.a"
    fi
fi

[ "x$SM" = "xyes" ] && QT_LIBS="${QT_LIBS} -lSM -lICE"
[ "x$LIBPNG" = "xyes" ] && QT_MODOBJ=$QT_MODOBJ' $(PNG_OBJECTS)'
[ "x$ZLIB" = "xyes" ] && QT_MODOBJ=$QT_MODOBJ' $(ZLIB_OBJECTS)'
chmod +w src/tools/qmodules.h
cat >src/tools/qmodules.h << EOF
// These modules are licensed to $Licensee
EOF
for MODULE in $MODULES
do
    if [ -d src/$MODULE ]
    then
	M=`echo $MODULE | tr '[a-z]' '[A-Z]'`
	echo "#define QT_MODULE_$M" >>src/tools/qmodules.h
        QT_MODOBJ=$QT_MODOBJ' \
	    $(OBJECTS_'$MODULE')'
	if [ $MODULE = opengl ]
	then
	    I_FLAGS=$I_FLAGS' $(SYSCONF_CXXFLAGS_OPENGL)'
	    QT_LIBS=$QT_LIBS' $(SYSCONF_LFLAGS_OPENGL) $(SYSCONF_LIBS_OPENGL)'
	fi
    fi
done
chmod -w src/tools/qmodules.h
[ "x$LIBPNG" = "xno" ] && QT_LIBS="${QT_LIBS} -lpng"
[ "x$ZLIB" = "xno" ] && QT_LIBS="${QT_LIBS} -lz"
[ "x$JPEG" = "xyes" ] && QT_LIBS="${QT_LIBS} -ljpeg"
[ "x$MNG" = "xyes" ] && QT_LIBS="${QT_LIBS} -lmng -ljpeg" # assume JNG support
[ "x$NAS_SOUND" = "xyes" ] && QT_LIBS="${QT_LIBS} -laudio -lXt" # Xt junk in audio library
QT_LIBS="$L_FLAGS $R_FLAGS $QT_LIBS $l_FLAGS"


# Create the .buildopts file

cat > .buildopts <<EOF
QT_CXXFLAGS_OPT =$QT_CXX $I_FLAGS
QT_CFLAGS_OPT   =$QT_C $I_FLAGS
QT_LIBS_OPT	=$QT_LIBS
QT_MODULE_OBJ      =$QT_MODOBJ $QT_SUBLIBS

EOF


QTDIR='$(QTDIR)'


# Give feedback...

echo
if [ "$XTARGET" = "$TARGET" ]
then
    echo "Build type:    $TARGET"
else
    echo "Building on:   $TARGET"
    echo "Building for:  $XTARGET"
fi

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

if [ -n "$VERBOSE" ]
then
    echo "Compile flags.... $QT_CXX $I_FLAGS"
    echo "Link flags....... $QT_LIBS"
fi

echo "Thread support....... $THREAD"
echo "GIF support.......... $GIF"
echo "MNG support.......... $MNG"
echo "JPEG support......... $JPEG"
echo "NAS sound support.... $NAS_SOUND"
echo "Session management... $SM"
echo

echo Creating makefiles...

sepath=`echo $relpath | sed -e 's/\\./\\\\./g'`

if [ $KDE = yes ]
then
    if [ '!' -f $KDEDIR/bin/dcopserver ]
    then
	KDE=no
	echo ""
	echo "You wanted to compile the Qt Designer with"
	echo "KDE2 support, but \$KDEDIR does not point"
	echo "to a valid KDE2 installation. KDE support"
	echo "has been turned off."
	echo ""
    fi
fi

for a in `find $relpath/. -name Makefile.in -print | sort` ; do
    dir=`dirname $a`
    buildpath=`echo "$dir" | sed -e "s;$sepath;.;g"`
    test -d $buildpath || mkdir $buildpath
    N=$buildpath/Makefile
    M=$a
    if [ -f $dir/Makefile-$Edition.in ]
    then
	M=$dir/Makefile-$Edition.in
    fi
    if [ $KDE = yes ]
    then
	if [ -f $dir/Makefile-kde.in ]
	then
	    M=$dir/Makefile-kde.in
	fi
    fi
    if [ $buildpath = ././src/moc ]
    then
	# Don't cross-compile this
	CONF=$CONFIG
    else
	CONF=$XCONFIG
    fi

    REQ=`sed -n -e '/^REQUIRES *=/ { s/[^=]*= *//; p; }' $M`
    for r in $REQ
    do
	if echo $MODULES | grep -v "\\<$r\\>" >/dev/null
	then
	    M=
	    break
	fi
    done
    if [ -z "$M" ]
    then
	echo "all clean:" >$N
	echo "	@echo 'Some of the required modules ($REQ) are not available.'" >>$N
	echo "	@echo 'Skipped.'" >>$N
	if [ "$VERBOSE" = "yes" ]; then
	   echo '  Created dummy' $N
	fi
    else
	cat > $N <<EOF
#############################################################################
# Automatically generated from $M
# Build options from $1
#############################################################################

EOF

	if [ $relpath != `pwd` ] ; then
	    echo VPATH=$relpath/$buildpath >> $N
	    cat .buildopts $CONF >> $N
	    sed -e's/	\$(MOC) [^ ]* -o .*/	$(MOC) $< -o $@/g' \
		-e's/\(	\$(CXX) -c \$(CXXFLAGS) \$(INCPATH) -o\) [^ ]* [^ ]*/\1 $@ $</g' $M >> $N
	else
	    cat .buildopts $CONF $M >> $N
	fi
	if [ "$VERBOSE" = "yes" ]; then
	   echo '  Created' $N
	fi
    fi
done

if echo $LD_LIBRARY_PATH | grep >/dev/null $QTDIR/lib
then
    if grep >/dev/null SYSCONF_RPATH $XCONFIG
    then
	echo # Using -R/-rpath, so no need to warn
    else
	if [ -n "$FROMMAKE" ]
	then
	    echo
	    echo "WARNING: \$LD_LIBRARY_PATH does not contain \$QTDIR/lib"
	    echo "         many platforms require this."
	    echo
	fi
    fi
fi

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