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

PLATFORM=generic
DEVICE_ARCH=generic
SHARING=shared
DEBUG=
QDESKTOP=
X11=
QCONFIGARG=
ECONFIG="CONFIG+=embedded"
if [ `uname -s` != Darwin ]
then
	QMAKE_LFLAGS="QMAKE_LFLAGS=-Wl,-rpath,$QPEDIR/lib"
fi
SINGLE="CONFIG+=multiprocess"
SINGLESINGLE="CONFIG+=singleprocess DEFINES+=SINGLE_APP"
STATIC="CONFIG+=dynamic"
STATICSTATIC="CONFIG+=static DEFINES+=QTOPIA_FAKE_COMPONENT"
QUICKLAUNCH="CONFIG+=quicklaunch DEFINES+=QTOPIA_APP_INTERFACE"

touch .test.qtopia.
if [ '!' -f ${QPEDIR}/src/.test.qtopia. ];
then
    rm .test.qtopia.
    cd ..
    echo
    echo
    echo '   The environment variable $QPEDIR is not set correctly. It is currently'
    echo '   set to "'$QPEDIR'", but it should be set to above root qtopia directory,'
    echo '   which is "'`pwd`'".'
    echo
    echo '   Please read the INSTALL file for instructions on how to set $QPEDIR'
    echo '   correctly. If you have set $QPEDIR 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.qtopia.

#other checks
#TMAKE_VERSION=$(tmake -v 2>&1 | sed -n -e 's/^tmake: (Version [^\s]+)/$1/p');
TMAKE_VERSION=$(tmake -v 2>&1 | sed -n -e 's/^tmake: Version//p' | sed 's/ (run.*$//')
QMAKE_VERSION=$(qmake -v 2>&1 | sed -n -e 's/^Qmake version: //p' | sed 's/ (Qt.*$//')

#check tmake version
TMAKE_MAJ_VER=$(echo $TMAKE_VERSION | sed -e 's/\..*$//')
TMAKE_MIN_VER=$(echo $TMAKE_VERSION | sed -e 's/^.*\.//')

if [ -n "$TMAKE_MAJ_VER" ] ;
then
    if [ "$TMAKE_MAJ_VER" -lt 1 ] ; 
    then
	echo "tmake version needs to be at least 1.11"
	exit -1;
    fi
    if [ -n "$TMAKE_MIN_VEa"] ;
    then
	if [ "$TMAKE_MIN_VER" -lt 11 ] ; 
	then
	    echo "tmake version needs to be at least 1.11"
	    exit -1;
	fi
    fi 
fi

DEFAULTBUILD="dynamic"

VERSION_MAJ=$(sed -n -e 's/.*QPE_VERSION "\([0-9]*\)\.[0-9]*\.[0-9]*.*".*/\1/p' <libraries/qtopia/version.h)
VERSION_MIN=$(sed -n -e 's/.*QPE_VERSION "[0-9]*\.\([0-9]\)\.[0-9]*.*".*/\1/p' <libraries/qtopia/version.h)
VERSION_PAT=$(sed -n -e 's/.*QPE_VERSION "[0-9]*\.[0-9]*\.\([0-9]*\).*".*/\1/p' <libraries/qtopia/version.h)

TOMAKE=
EXTRALIBS=
lupdateargs=
INSTALL=

if grep -q 'VERSION_STR.*"3' $QTDIR/include/qglobal.h
then
    QT3=yes
else
    QT3=no
fi

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

while [ -n "$1" ]; do
   A=$1
   case $1 in
   -platform|-xplatform) # No difference since we don't need to build moc, etc.
        shift; PLATFORM=$1
	A="$A $1"
	DEVICE_ARCH=$PLATFORM
	;;
   -release)
       DEBUG=
	;;
   -debug)
       DEBUG=-debug
	;;
   -shared)
       SHARING=shared
	;;
   -x11)
       X11=x11
	;;
   -static)
       SHARING=static
       STATIC="$STATICSTATIC"
	;;
   -kde)
	CONFIG_STR="$CONFIG_STR kde"
	QT3=yes
	;;
   -rpath)
	shift;
	A="$A $1"
	QMAKE_LFLAGS="QMAKE_LFLAGS=-Wl,-rpath,$1"
	QT3=yes
	;;
    -install)
	#pass through to qdesktop.  does rpath.
	INSTALL=" -install"
	;;
   -noobsolete)
	lupdateargs="$lupdateargs -noobsolete"
	;;
   -qtopiadesktop)
	# requires qmake
	if [[ -z "$QMAKE_VERSION" ]]
	then	
	    echo "qmake is required to configure Qtopia Desktop."
	    exit -1;
	fi
	QDESKTOP="/qdesktop"
	ECONFIG="CONFIG+=qdesktop"
	DEFAULTBUILD=desktop
        QT3=yes
	V=$(awk '/define.QT_VERSION.*"/ { print $3 }' $QTDIR/include/qglobal.h)
	case "$V" in
	  \"3.*)
	    true;
	;; *)
	    echo >&2 "\$QTDIR does not appear to be Qt 3 ($QTDIR - $V)"
	    exit 1
	esac
	;;
   -qconfig)
	# optional way to specify the qconfig-qtopia.h is to pass -qconfig qtopia
	shift;
	A="$A $1"
	case "$1" in
	 qconfig*.h)
	    QCONFIGARG=DEFINES+=QCONFIG='\"'$1'\"' # Don't quote me on that.
	;; *)
	    QCONFIGARG=DEFINES+=QCONFIG='\"'qconfig-$1.h'\"'
	esac
	;;
   -make)
	shift; TOMAKE="$TOMAKE $1"
	A=""
	;;
   -lib)
	shift; EXTRALIBS="$EXTRALIBS $1"
	A="$A $1"
	;;
   -qt3)
       QT3=yes
       ;;
    -single)
	SINGLE="$SINGLESINGLE"
	;;
    -noquicklaunch)
	QUICKLAUNCH=
	;;
    *)
	HELP=yes;;
   esac
   RECONF="$RECONF $A"
   shift
done

TARGET=configs$QDESKTOP/$PLATFORM-$SHARING$X11$DEBUG

if [ '!' -f $TARGET ]
then
    if [ -f configs$QDESKTOP/linux-$PLATFORM-g++-$SHARING$X11$DEBUG ]
    then
	PLATFORM=linux-$PLATFORM-g++
	TARGET=configs$QDESKTOP/$PLATFORM-$SHARING$X11$DEBUG
    else
	echo
	echo '    The specified platform/compiler not supported: ' $TARGET
	echo
	exit 2
    fi
fi

# Next, emit a usage message if something failed.

if [ "$HELP" = "yes" ]; then
	cat <<EOF
	Usage: $0 [-debug] [-release] [-shared] [-static] [-platform ...]

	The defaults (*) are usually acceptable.  Here is a short explanation of
	each option:

	 *  -release ........... Compile and link Qtopia with optimization turned on.
	    -debug ............. Compile and link Qtopia with debugging turned on.

	 *  -shared ............ Create and use shared Qtopia libraries.
	    -static ............ Create and use static Qtopia libraries.

	    -platform target ... The platform you are building on ($PLATFORM)

	    -qtopiadesktop ..... Configure for Qtopia Desktop, not device Qtopia.

	    -noquicklaunch ..... Disable the quicklauncher functionality.
EOF
   exit 0
fi

if [ -n "$QDESKTOP" ]; then
	rm -f libraries/qtopia/custom.h
	rm -f libraries/qtopia/custom.cpp
	ln -s custom-qdesktop.h libraries/qtopia/custom.h
	ln -s custom-qdesktop.cpp libraries/qtopia/custom.cpp
else
    for ext in h cpp
    do
	rm -f libraries/qtopia/custom.$ext
	if [ -f libraries/qtopia/custom-$PLATFORM.$ext ]
	then
	    ln -s custom-$PLATFORM.$ext libraries/qtopia/custom.$ext
	else
	    ln -s custom-linux-generic-g++.$ext libraries/qtopia/custom.$ext
	fi
    done
fi

rm -f ../etc/defaultbuttons.conf
KEYARCH=$(echo $PLATFORM | sed -e 's/^[^-]*-\([^-]*\).*/\1/')
if [ -f ../etc/defaultbuttons-$KEYARCH.conf ]
then
    cp ../etc/defaultbuttons-$KEYARCH.conf ../etc/defaultbuttons.conf
fi

mkdir -p ../include/qtopia
rm -rf ../include/qpe
ln -s ../include/qtopia ../include/qpe
mkdir -p ../include/qtopia/private

# Need to rm the .h files for include qtopia now as it is not safe in the loop 
# (Library order may change
rm -f ../include/qtopia/*.h

cat > Makefile <<EOF
default: $DEFAULTBUILD

EOF

if [ -f ${QPEDIR}/src/Makefile.local ]
then
    cat ${QPEDIR}/src/Makefile.local >> Makefile
fi

cat Makefile.linux >> Makefile

for LIBRARY in $(make showlibraries | grep -v '^make[^ ]*:')
do
    SRCDIR=../../src
    case $LIBRARY in
     3rdparty/libraries/libavcodec)
	;;
     3rdparty/libraries/libavformat)
	;;
     libraries/qtopia1)
        ( cd ../include/qtopia; ln -s $SRCDIR/$LIBRARY/*.h .; \
	for i in `find $SRCDIR/$LIBRARY -name *.ui`;do j=`echo $i | sed -e 's/ui$/h/'`;done )
	;;
     libraries/qtopia*)
	B=${LIBRARY#libraries/qtopia}
	if [ -z "$B" ]
	then
	    SRCDIR=../../src
	else
	    SRCDIR=../../../src
	fi
	mkdir -p ../include/qtopia/$B
	if [ -z "$B" ]
	then
	    ( cd ../include/qtopia; ln -s $SRCDIR/$LIBRARY/*.h .; \
	    for i in `find $SRCDIR/$LIBRARY -name *.ui`;do j=`echo $i | sed -e 's/ui$/h/'`;ln -s $j .;done ; rm -f *_p.h )
	else
	    ( cd ../include/qtopia/$B && rm -f *.h; ln -s $SRCDIR/$LIBRARY/*.h .; \
	    for i in `find $SRCDIR/$LIBRARY -name *.ui`;do j=`echo $i | sed -e 's/ui$/h/'`;ln -s $j .;done ; rm -f *_p.h )
	fi
	;;
    3rdparty/libraries/freetype)
	# don't link, no header files.
	;;
	*)
	( cd ../include/qtopia; ln -s $SRCDIR/$LIBRARY/*.h .; \
	for i in `find $SRCDIR/$LIBRARY -name *.ui`;do j=`echo $i | sed -e 's/ui$/h/'`;ln -s $SRCDIR/$LIBRARY/$j .;done )
    esac
done
( cd ../include/qtopia/private && rm -f *.h; ln -s ../../../src/libraries/qtopia/*_p.h .; ln -s ../../../src/libraries/qtopia/backend/*.h .; \
	for i in `ls ../../../src/libraries/qtopia/*_p.ui`;do j=`echo $i | sed -e 's/ui$/h/'`;ln -s $j .;done ; )
if [ -d ../include/qtopia/pim ]
then
    mkdir -p ../include/qtopia/pim/private
    ( cd ../include/qtopia/pim/private && rm -f *.h; ln -s ../../../../src/libraries/qtopiapim/*_p.h .; )
fi


if [ "$QT3" = yes ]
then
    VCONFIG="CONFIG+=qt3"
else
    VCONFIG="CONFIG+=qt2"
fi

H=`pwd`
if [ -z "$TOMAKE" ]
then
    if [ "$QDESKTOP" = "/qdesktop" ]
    then
	TOMAKE=`make showdesktopcomponents | grep -v '^make[^ ]*:'`
    else
	TOMAKE=`make showcomponents | grep -v '^make[^ ]*:'`
    fi
fi

if [ -f $TMAKEPATH/tmake.conf ]
then
    # You have tmake. We'll regenerate the file for you...
    echo "Makefiles will be regenerated."
elif [ ! -f server/Makefile.in ]
then
    if [ -z "$QDESKTOP" ]
    then
        echo >&2 "You do not have \$TMAKEPATH/tmake.conf,
	    and you do not have pregenerated Makefile.in files.
	    Install tmake, or reinstall package to recover Makefile.in files."
        exit 1
    fi
fi

for a in $TOMAKE server; do
    N=$a/Makefile
    M=$a/Makefile.in
    O=$a/Makefile.add
    f=`basename $a`
    HAD_TO_USE_QMAKE=

    if [ -f $TMAKEPATH/tmake.conf ]
    then
	if [ -f $a/$f.pro ]
	then
	    if [ $f != "server" ]
	    then
		if [ "$SINGLE" = "$SINGLESINGLE" ]
		then
		    if ! grep -e '^singleprocess:' $a/$f.pro >/dev/null 2>&1
		    then
			echo 'all clean distclean:' > $N
			echo -e "\t@echo Not building $f\n" >> $N
			continue
		    fi
		    # Incorrect for non-single, static builds
		    if [ "$STATIC" = "$STATICSTATIC" ]
		    then
			# target becomes a library for the server to link with
			targ=`grep '^TARGET' $a/$f.pro | cut -d'=' -f2 | sed -e 's/ //g'`
			if [ -z $targ ];
			then
			    targ=$f
			fi

			# server links against our deps as well
			lib=`grep '^\(\(static\|unix\)\:\)*LIBS' $a/$f.pro | cut -d'=' -f2`
			if [ ! -z "$lib" ]
			then
			    SERVERLIBS="$SERVERLIBS ${lib}"
			fi

			# plugin-specific
			MAGICPATH=
			# libraries/qtopia contains the definition of Q_CREATE_INSTANCE
			if [ $f != "qtopia" ] && grep Q_CREATE_INSTANCE $a/*.cpp >/dev/null 2>&1
			then
			    # help the server find us
			    dest=`grep '^DESTDIR' $a/$f.pro | cut -d'=' -f2 | sed -e 's/ //g'`
			    if [ ! -z "$dest" ]
			    then
				SERVERLIBS="-L$dest $SERVERLIBS"
			    fi

			    # magic-path for fake plugin loading
			    file="lib${targ}.so"
			    surprisefile=`echo "$dest/$file" | sed "s,\\$(QPEDIR),$QPEDIR,"`
			    echo 'Surprise!' > $surprisefile
			    MAGICPATH="DEFINES+=MYMAGICALPATH='\"'$file'\"'"
			    SERVERPLUGINS="-l${targ} $SERVERPLUGINS"
			else
			    SERVERLIBS="-l${targ} $SERVERLIBS"
			    SERVERDEPS="lib${targ}.a $SERVERDEPS"
			fi
		    fi
		fi
		if [ -n "$QUICKLAUNCH" ]
		then
		    LINK_TO_QUICKLAUNCH=
		    PACKAGE_QUICKLAUNCH=
		    # Not a very good way to detect ql enbaled apps.
		    doql=`grep '^quicklaunch\:' $a/$f.pro`
		    if [ -n "$doql" ]
		    then
			LINK_TO_QUICKLAUNCH="ln_quicklaunch"
			PACKAGE_QUICKLAUNCH="-quicklaunch"
			targ=`grep '^TARGET' $a/$f.pro | cut -d'=' -f2 | sed -e 's/ //g'`
			if [ -z $targ ];
			then
			    targ=$f
			fi
		    fi
		fi
	    fi

	    (   cd $a;
		if [ "$f" = server ]
		then 
		    # Qt dependancies, plugins, applications, libraries
		    EXTRALIBS="-ljpeg -Wl,-Bstatic -Wl,-whole-archive $SERVERPLUGINS -Wl,-no-whole-archive -Wl,-Bdynamic $SERVERLIBS $EXTRALIBS"
		fi
		MOC_DIR=.moc
		OBJECTS_DIR=.obj
		tmake $MAGICPATH $STATIC $SINGLE $QUICKLAUNCH $VCONFIG $QCONFIGARG $ECONFIG LIBS+="$EXTRALIBS" \
		OBJECTS_DIR="\$(OBJECTS_DIR)" MOC_DIR="\$(MOC_DIR)" INCLUDEPATH+="\$(MOC_DIR)" \
		    -t $H/qt/tmake/propagate.t $f.pro |
		    sed -e "s|$QTDIR|\$(QTDIR)|g" -e "s|$QPEDIR|\$(QPEDIR)|g" >Makefile.in;
	    )
	    echo -n "."
	    appname=`grep '^TARGET' $a/$f.pro | sed 's/^TARGET.*=//' | sed 's/ //g'`
	fi
    elif [ -n "$QDESKTOP" ]
    then
	( cd $a;
		qmake DEFINES+=QTOPIA_DESKTOP \
			INCLUDEPATH+='$(QPEDIR)/include' \
			QMAKE_LFLAGS+='-L$(QPEDIR)/lib' \
			CONFIG+=no_include_pwd \
			CONFIG+=qdesktop;
	)
        echo -n "."
        HAD_TO_USE_QMAKE=1
    fi

    if [ -z "$HAD_TO_USE_QMAKE" ]
    then
	if [ -f $a/$f.pro ]
	then
	   DEFAULTTARGET=all
	fi 


	CONTROL=`ls $a/*.control 2> /dev/null`
	if [ $? -eq 0 ]
	then
	    MKPKG_Y="package"
	    CONTROL=`echo $CONTROL`
	else
	    MKPKG_Y=""
	    CONTROL=""
	    if [ ! -f $a/$f.pro ]
	    then
		echo "Warning: $a/$f.pro not found, and no control file either."
	    fi
	fi

	cat > $N <<EOF
#############################################################################
# Automatically generated from $M
# Build options from $1
#############################################################################

default: $DEFAULTTARGET $LINK_TO_QUICKLAUNCH # $MKPKG_Y

CONTROL = $CONTROL

EOF

	if [ -n "$LINK_TO_QUICKLAUNCH" ]
	then
	    cat >> $N <<EOF
ln_quicklaunch:
	-cd \$(QPEDIR)/bin ; ln -sf quicklauncher $targ

EOF
	fi

	SED=
	PLATFORM_CFLAGS=
	OBJECTS_DIR=".obj\\/$PLATFORM$QDESKTOP"

	if [ "$f" = "embeddedkonsole" ]
	then
	    case $PLATFORM in
		*x86*|*generic*|*ipaq*)
		    SED="$SED /^LIBS.*=/s/\$/ -lutil/;"
		    PLATFORM_CFLAGS="-DHAVE_OPENPTY"
		;; *)
		    SED=
	    esac
	elif [ "$f" = "libffmpeg" ]
	then
	    PLATFORM_CFLAGS="-w -O5"
	    # Patch our Makefile.in file with the platform specifics for the libffmpeg plugin library
	    case $PLATFORM
	    in
		  *arm*|*ipaq*|*sharp*)
		    #SED="$SED /SOURCES.*=/s/=/= yuv2rgb_arm4l.S /;"
		    #SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/yuv2rgb_arm4l.o /;"
		    #echo -e "\n.obj/$PLATFORM$QDESKTOP/yuv2rgb_arm4l.o: yuv2rgb_arm4l.S\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		;; *)
	    esac
	elif [ "$f" = "libavformat" ]
	then
	    # Patch our Makefile.in file with the platform specifics for the libavformat library
	    case $PLATFORM
	    in
		  *x86*|*mmx*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_X86=1"
		;;*arm*|*ipaq*|*sharp*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_ARMV4L=1"
		;;*alpha*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_ALPHA=1"
		;;*powerpc*|*ppc*|*altivec*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_POWERPC=1 -DWORDS_BIGENDIAN=1"
		;;*ps2*|*mmi*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_MIPS=1 -DWORDS_BIGENDIAN=1"
		;;*cassiopeia*|*mips*)
		    PLATFORM_CFLAGS="-w -O5 -DARCH_MIPS=1"
		;; *)
		    PLATFORM_CFLAGS="-w -O5"
	    esac
	elif [ "$f" = "libavcodec" ]
	then
	    # Patch our Makefile.in file with the platform specifics for the libavcodec library
	    case $PLATFORM
	    in
		  *x86*|*mmx*)
		    # For x86 try to compile the MMX optimized code (some alternative 3DNow and SSE optimization also appear to exist in libavcodec).
		    PLATFORM_CFLAGS="-w -O5 -DARCH_X86=1 -DHAVE_MMX=1"
		    SED="$SED /SOURCES.*=/s/=/= i386\\/fdct_mmx.c i386\\/cputest.c i386\\/dsputil_mmx.c i386\\/mpegvideo_mmx.c i386\\/idct_mmx.c i386\\/motion_est_mmx.c i386\\/simple_idct_mmx.c \ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/fdct_mmx.o $OBJECTS_DIR\\/cputest.o $OBJECTS_DIR\\/dsputil_mmx.o $OBJECTS_DIR\\/mpegvideo_mmx.o $OBJECTS_DIR\\/idct_mmx.o $OBJECTS_DIR\\/motion_est_mmx.o $OBJECTS_DIR\\/simple_idct_mmx.o \ /;"
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/fdct_mmx.o: i386/fdct_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/cputest.o: i386/cputest.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_mmx.o: i386/dsputil_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_mmx.o: i386/mpegvideo_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/idct_mmx.o: i386/idct_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/motion_est_mmx.o: i386/motion_est_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/simple_idct_mmx.o: i386/simple_idct_mmx.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    #    %_sse.o : %_sse.c
		    #	    $(CC) $(CFLAGS) -msse -c -o $@ $< 
		    #    %_3dnow.o : %_3dnow.c
		    #	    $(CC) $(CFLAGS) -m3dnow -c -o $@ $< 
		;;*arm*|*ipaq*|*sharp*)
		    # For StrongARM processors, compile these extra optimized source code files.
		    PLATFORM_CFLAGS="-w -O5 -DARCH_ARMV4L=1"
		    SED="$SED /SOURCES.*=/s/=/= armv4l\\/dsputil_arm.c armv4l\\/jrevdct_arm.S armv4l\\/mpegvideo_arm.c \ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/dsputil_arm.o $OBJECTS_DIR\\/jrevdct_arm.o $OBJECTS_DIR\\/mpegvideo_arm.o \ /;"
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_arm.o: armv4l/dsputil_arm.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/jrevdct_arm.o: armv4l/jrevdct_arm.S\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_arm.o: armv4l/mpegvideo_arm.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		;;*alpha*)
		    # Ignore for now. Some alpha optimizations exist, but we have no hardware to test against :(
		    PLATFORM_CFLAGS="-w -O5 -DARCH_ALPHA=1"
		    echo There are alpha optimizations for libffmpeg, but they have not been configured.
		;;*powerpc*|*ppc*)
		    # For PowerPC without AltiVec optimizations
		    PLATFORM_CFLAGS="-w -O5 -DARCH_POWERPC=1 -DWORDS_BIGENDIAN=1 -DHAVE_ALTIVEC=0"
		    SED="$SED /SOURCES.*=/s/=/= ppc\\/dsputil_ppc.c ppc\\/mpegvideo_ppc.c \ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/dsputil_ppc.o $OBJECTS_DIR\\/mpegvideo_ppc.o \ /;"
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_ppc.o: ppc/dsputil_ppc.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_ppc.o: ppc/mpegvideo_ppc.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		;;*altivec*)
		    # For PowerPC try to compile the AltiVec optimizations
		    PLATFORM_CFLAGS="-w -O5 -DARCH_POWERPC=1 -DWORDS_BIGENDIAN=1 -faltivec -DHAVE_ALTIVEC=1"
		    SED="$SED /SOURCES.*=/s/=/= ppc\\/dsputil_altivec.c ppc\\/dsputil_ppc.c ppc\\/idct_altivec.c ppc\\/mpegvideo_altivec.c ppc\\/mpegvideo_ppc.c \ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/dsputil_altivec.o $OBJECTS_DIR\\/dsputil_ppc.o $OBJECTS_DIR\\/idct_altivec.o $OBJECTS_DIR\\/mpegvideo_altivec.o $OBJECTS_DIR\\/mpegvideo_ppc.o \ /;"
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_altivec.o: ppc/dsputil_altivec.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_ppc.o: ppc/dsputil_ppc.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/idct_altivec.o: ppc/idct_altivec.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_altivec.o: ppc/mpegvideo_altivec.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_ppc.o: ppc/mpegvideo_ppc.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo Configuring AltiVec optimizations for PowerPC for the libffmpeg plugin.
		;;*ps2*|*mmi*)
		    # For MMI optimizations
		    PLATFORM_CFLAGS="-w -O5 -DARCH_MIPS=1 -DWORDS_BIGENDIAN=1 -DHAVE_MMI=1"
		    SED="$SED /SOURCES.*=/s/=/= ps2\\/dsputil_mmi.c ps2\\/idct_mmi.c ps2\\/mpegvideo_mmi.c \ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/dsputil_mmi.o $OBJECTS_DIR\\/idct_mmi.o $OBJECTS_DIR\\/mpegvideo_mmi.o \ /;"
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/dsputil_mmi.o: ps2/dsputil_mmi.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/idct_mmi.o: ps2/idct_mmi.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo -e "\n.obj/$PLATFORM$QDESKTOP/mpegvideo_mmi.o: ps2/mpegvideo_mmi.c\n\t\$(CC) -c \$(CFLAGS) \$(INCPATH) -o \$@ \$<\n" >> $N
		    echo Configuring MMI optimizations for the libffmpeg plugin.
		;;*cassiopeia*|*mips*)
		    # For MIPS set optimizations to O5
		    PLATFORM_CFLAGS="-w -O5 -DARCH_MIPS=1"
		    echo Configuring MIPS settings for the libffmpeg plugin.
		;; *)
		    # For everything else:
		    PLATFORM_CFLAGS="-w -O5"
	    esac
	elif [ "$f" = "libmpeg3" ]
	then
	    # Patch our Makefile.in file with the platform specifics for the libmpeg3 library
	    # Use the C++ compiler to compile the .c files (because the fixed-point classes are C++ code)
	    SED='s/\$(CC)/\$(CXX)/;'
	    case $PLATFORM
	    in
		# For x86 turn on using floating point, compile mmx and css code
		*x86*)
		    # "-funroll-loops -fomit-frame-pointer -malign-loops=2 -malign-jumps=2 -malign-functions=2 -march=i486"
		    PLATFORM_CFLAGS="-DHAVE_MMX -DHAVE_CSS"
		    SED="$SED /SOURCES.*=/s/=/= video\\/mmxidct.S video\\/reconmmx.s\ /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/mmxidct.o $OBJECTS_DIR\\/reconmmx.o\ /;"
		# For generic turn on using floating point
		;; *generic*)
		    PLATFORM_CFLAGS=""
		# For the ipaq use fixed point maths, don't compile the mmx or css code
		;; *ipaq*)
		    PLATFORM_CFLAGS="-DUSE_FIXED_POINT"
		;; *)
		    # For 'other platforms', turn off optimizations and use fixed point
		    PLATFORM_CFLAGS="-O -DUSE_FIXED_POINT"
	    esac
	elif [ "$f" = "libmad" ]
	then
	    # Patch our Makefile.in file with the platform specifics for the libmad library
	    case $PLATFORM
	    in
		# For x86 use intel optimizations
		*x86*)
		    PLATFORM_CFLAGS="-DFPM_INTEL"
		# For the ipaq use ARM asm optimizations
		;; *ipaq*)
		    PLATFORM_CFLAGS="-DFPM_ARM"
		    SED="$SED /SOURCES.*=/s/=/= idmt_arm.S /;"
		    SED="$SED /OBJECTS.*=/s/=/= $OBJECTS_DIR\\/idmt_arm.o /;"
		# For generic and other platforms use the C 64-bit implementation
		;; *generic*)
		;; *)
		    PLATFORM_CFLAGS="-DFPM_64BIT"
	    esac
	elif [ "$X11" = x11 -a "$f" = library ]
	then
		    SED='s/\$(MOC) qpedecoration_qws.h -o/touch/'
	elif [ "$X11" = x11 -a "$f" = taskbar ]
	then
		    SED='s/\$(MOC) ..\/calibrate\/calibrate.h -o/touch/'
	fi
	if [ -n "$PLATFORM_CFLAGS" ]
	then
	    # Append the addition c-flags we have defined
	    SED="$SED /CFLAGS.*=.*/s/\$/ $PLATFORM_CFLAGS/;"
	    SED="$SED /CXXFLAGS.*=.*/s/\$/ $PLATFORM_CFLAGS/;"
	fi
	cat $TARGET >> $N

	echo >> $N
	echo "# Platform output directories" >> $N
	echo MOC_DIR = .moc/$PLATFORM$QDESKTOP/ >> $N
	echo OBJECTS_DIR = .obj/$PLATFORM$QDESKTOP/ >> $N
	echo >> $N

	mkdir -p $a/.moc/$PLATFORM$QDESKTOP >/dev/null 2>&1
	mkdir -p $a/.obj/$PLATFORM$QDESKTOP >/dev/null 2>&1

	if [ -n "$SED" ]
	then
	    sed -e "$SED" $M >> $N
	else
	    cat $M >> $N
	fi

	TR=
	if [ -f $a/$f.pro ]
	then
	    template=
	    if [ -n "$QUICKLAUNCH" ]
	    then
		template=`grep '^quicklaunch\:TEMPLATE' $a/$f.pro | sed 's/^quicklaunch\:TEMPLATE.*=//' | sed 's/ //g'`
	    fi
	    if [ -z "$template" ]
	    then
		template=`grep '^TEMPLATE' $a/$f.pro | sed 's/^TEMPLATE.*=//' | sed 's/ //g'`
	    fi

	    IPKGDEP="\$(DESTDIR)\$(TARGET)"
	    if [ "$template" = "lib" ]
	    then
		IPKGDEP="\$(DESTDIR)\$(SYSCONF_LINK_TARGET)"
	    fi

	    if grep -q 'TRANSLATION.*\.ts' $a/$f.pro
	    then
		TR=y
	    fi
	else
	    IPKGDEP=
	fi

	if [ -n "$TR" ]
	then
	# Fun escaping...
	    cat >> $N <<EOF

lupdate-check:
	@sed -e '/\$\$\$\$QTOPIA1DIR[^ ]*/ d' \\
	    -e '/\$\$(.*/ d' \\
	    -e 's/^static:.*//' \\
	    <$f.pro >t\$\$\$\$.pro && \\
	rm -rf t\$\$\$\$&& \\
	mkdir t\$\$\$\$ && \\
	cp -a *.ts t\$\$\$\$ && \\
	chmod +w *.ts && \\
	lupdate $lupdateargs t\$\$\$\$.pro && \\
	rm t\$\$\$\$.pro && \\
	for i in *.ts; \\
	do \\
	    if cmp -s \$\$i t\$\$\$\$/\$\$i; \\
	    then \\
		true; \\
	    else \\
		echo out-of-date \$\$i; \\
	    fi; \\
	    rm $\$i; \\
	    mv t\$\$\$\$/\$\$i \$\$i; \\
	done && \\
	rmdir t\$\$\$\$

lupdate:
	sed -e '/\$\$\$\$QTOPIA1DIR[^ ]*/ d' \\
	    -e '/\$\$(.*/ d' \\
	    -e 's/^static:.*//' \\
	    <$f.pro >t\$\$\$\$.pro && \\
	lupdate $lupdateargs t\$\$\$\$.pro && \\
	rm t\$\$\$\$.pro

lrelease:
	lrelease $f.pro
	\$(QPEDIR)/bin/qtopia-linstall $f.pro

EOF
	else
	    cat >> $N <<EOF

lupdate-check:
	@true

lupdate:
	@true

lrelease:
	@echo "No TRANSLATIONS"

EOF
	fi

	cat >> $N <<EOF

ipkg: $IPKGDEP # among other things...
	CTL="\$(CONTROL)"; for ctrl in \$\$CTL; do cd \$(QPEDIR)/ipkg; ../bin/mkipks -platform $DEVICE_ARCH $PACKAGE_QUICKLAUNCH \$(QPEDIR)/src/\$\$ctrl ; done

package: ipkg
	cd \$(QPEDIR)/ipkg; \
	../bin/mkPackages

EOF

	if [ -f $a/$f.pro ]
	then
	    cat >> $N <<EOF
Makefile: $f.pro
	cd \$(QPEDIR)/src; \
	./configure $RECONF -make $a
EOF
	fi
    fi

    if [ -f "$O" ]
    then
	cat >> $N $O
    fi
done

if [ -n "$QDESKTOP" ]
then
    rm -f ../lib/libfreetype.so.6*  # It may get in the way if it exists
    ( cd qtopiadesktop; ./configure -quiet $INSTALL; )
fi

MAKE=make
echo
echo "QPE is now configured for building. Just run \"$MAKE\"."
echo "To reconfigure, run $MAKE clean and configure."
echo
