#!/bin/bash


if [ ! -d toolchain ]
then
    if [ ! -f required/toolchain.tar.gz ]
    then
        echo "Please install the toolchain to $PWD/toolchain/mips32 ..."
        exit
    else
        echo "Extracting toolchain..."
        tar zxf required/toolchain.tar.gz
    fi
fi

export TOOLCHAIN=$PWD/toolchain/mips32
export PATH=$TOOLCHAIN/bin:$PATH


if [ ! -d qtopia-phone-4.3.0 ]
then
    if [ ! -f required/qtopia-phone-commercial-src-4.3.0.tar.gz ]
    then
        echo "Please install the qtopia source code to $PWD/qtopia-phone-4.3.0 ..."
	exit
    else
        echo "Extracting Qtopia..."
        tar zxf required/qtopia-phone-commercial-src-4.3.0.tar.gz
	echo Patching Qt to fix issue with display of images in 16bit modes
	patch -p0 < qtpyui/patches/smooth.diff
	echo Copying QCONFIG file qconfig-lgn.h to Qt source
	cp qtpyui/patches/qconfig-lgn.h ./qtopia-phone-4.3.0/qtopiacore/qt/src/corelib/global/
    fi
fi

export QPE_SRC=$PWD/qtopia-phone-4.3.0


if [ ! -d qtopia-phone-4.3.0/devices/brcm1103 ]
then
    if [ ! -f required/ip3870-device-profile.tar.gz ]
    then
        echo "Please install the device configuration profile to $PWD/qtopia-phone-4.3.0/devices/brcm1103 ..."
	exit
    else
        echo "Extracting Device Profile..."
        cd qtopia-phone-4.3.0/devices
        tar zxf ../../required/ip3870-device-profile.tar.gz
        cd ../../ 
    fi
fi

export DEVICE=brcm1103


if [ ! -f ~/.qt-license ]
then
    if [ ! -f required/qt-license.txt ]
    then
        echo "Please ensure you have a Qt license file installed to ~/.qt-license"
        exit 
    else
        echo "Copying Qt License File..."
        cp required/qt-license.txt ~/.qt-license
    fi
fi


if [ ! -d build-qte ]
then
    echo "Creating Build Directory..."
    mkdir build-qte
fi


if [ ! -f build-qte/lib/libQtGui.so.4.3.2 ]
then
    cd build-qte
    echo "Configuring Qt..."
    # echo yes | $QPE_SRC/configure -device $DEVICE -prefix /usr/opt/Qtopia
    # mkdir -p mkspecs/qws/linux-brcm1103-g++
    # cp $QPE_SRC/devices/brcm1103/mkspecs/qws/linux-brcm1103-g++/* mkspecs/qws/linux-brcm1103-g++ 
    mkdir -p mkspecs/qws
    tar -C mkspecs/qws -zxf ../required/linux-brcm1103-g++.tar.gz 
    echo yes | $QPE_SRC/qtopiacore/qt/configure -embedded mips -platform linux-g++ -xplatform $PWD/mkspecs/qws/linux-brcm1103-g++ -DQTOPIA_NO_ICONV_OPEN_ERRORS -prefix /usr/opt/Qtopia -make src -make tools -no-stl -no-exceptions -no-qt3support -no-glib -no-cups -no-qdbus -no-accessibility -DQT_QWS_DISABLE_FLUSHCLIPPING -confirm-license -depths 16,32 -qconfig lgn -DQT_NO_PLUGIN_CHECK -DQT_EXTERNAL_SOUND_SERVER -DQT_QWS_KEYEVENT_SINGLECLIENT -no-sql-db2 -no-sql-ibase -no-sql-mysql -no-sql-oci -no-sql-odbc -no-sql-psql -no-sql-sqlite -no-sql-sqlite2 -no-sql-tds -qt-sql-sqlite -release -no-gfx-qvfb -no-mouse-qvfb -no-kbd-qvfb -qt-gif -no-separate-debug-info -DQT_KEYPAD_NAVIGATION -qt-libjpeg -no-libmng -qt-libpng -qt-zlib -qt-mouse-linuxtp -big-endian -no-accessibility -D QT_QWS_SCREEN_COORDINATES
    echo "Building Qt..."
    make
    cd ..
fi


export QMAKESPEC=$PWD/build-qte/mkspecs/qws/linux-brcm1103-g++
export QTDIR=$PWD/build-qte
export PATH=$QTDIR/bin:$PATH
export INSTALL_ROOT=$PWD/image
export BUILD_LOG_FILE=$PWD/build.log

mkdir $INSTALL_ROOT

cd qtpyui
echo -e "########################\nConfiguring\n########################" | tee $BUILD_LOG_FILE
qmake 2>&1 | tee -a $BUILD_LOG_FILE
echo -e "########################\nBuilding   \n########################" | tee -a $BUILD_LOG_FILE
make --no-print-directory 2>&1 | tee -a $BUILD_LOG_FILE | grep -v "^g++"
echo -e "########################\nInstalling \n########################" | tee -a $BUILD_LOG_FILE
make --no-print-directory install 2>&1 | tee -a $BUILD_LOG_FILE
echo -e "########################\nDone.      \n########################" | tee -a $BUILD_LOG_FILE
cd ..


echo "Preparing IMAGE directory"

mkdir -p image/usr/opt/Qtopia/lib/fonts
cp -a build-qte/lib/libQtCore.so* image/usr/opt/Qtopia/lib
cp -a build-qte/lib/libQtGui.so* image/usr/opt/Qtopia/lib
cp -a build-qte/lib/libQtNetwork.so* image/usr/opt/Qtopia/lib
cp -a build-qte/lib/libQtScript.so* image/usr/opt/Qtopia/lib
cp -a build-qte/lib/libQtXml.so* image/usr/opt/Qtopia/lib
cp -a qtpyui/lib/libPythonQt.so* image/usr/opt/Qtopia/lib
mips-linux-strip -s image/usr/opt/Qtopia/lib/*.so.4.3.2
mips-linux-strip -s image/usr/opt/Qtopia/lib/*.so.1.0.0

mkdir -p image/usr/opt/Qtopia/bin
mv image/QtPythonUI image/usr/opt/Qtopia/bin
mips-linux-strip -s image/usr/opt/Qtopia/bin/QtPythonUI

mkdir -p image/usr/opt/Qtopia/plugins/imageformats
cp -a build-qte/plugins/imageformats/libqjpeg.so image/usr/opt/Qtopia/plugins/imageformats
mips-linux-strip -s image/usr/opt/Qtopia/plugins/imageformats/libqjpeg.so

mkdir -p image/usr/opt/Qtopia/plugins/gfxdrivers
cp -a qtpyui/plugins/gfxdrivers/libBrcm1103Fbscreen.so* image/usr/opt/Qtopia/plugins/gfxdrivers
mips-linux-strip -s image/usr/opt/Qtopia/plugins/gfxdrivers/libBrcm1103Fbscreen.so.1.0.0

mkdir -p image/usr/opt/Qtopia/plugins/kbddrivers
cp -a qtpyui/plugins/kbddrivers/libbrcm1103kbdhandler.so* image/usr/opt/Qtopia/plugins/kbddrivers
mips-linux-strip -s image/usr/opt/Qtopia/plugins/kbddrivers/libbrcm1103kbdhandler.so.1.0.0

cp qtpyui/device-files/* image/usr/opt/Qtopia/

cd image
tar czf image.tar.gz usr 
cd ..




