#!/bin/bash

# Compile
# -------
if [ `uname` == Linux ]; then
    chmod +x configure

    if [ $ARCH == 64 ]; then
        MARCH=x86-64
    else
        MARCH=i686
    fi

    # Executable file names
    EXEC_FILES=( assistant designer lconvert linguist lrelease \
    lupdate moc pixeltool qcollectiongenerator qdbus \
    qdbuscpp2xml qdbusviewer qdbusxml2cpp qdoc3 qhelpconverter \
    qhelpgenerator qmake qmlplugindump qmlviewer qt3to4 qtconfig \
    qttracereplay rcc uic xmlpatterns xmlpatternsvalidator )

    # Building QtWebKit on CentOS 5 fails without setting these flags
    # explicitly. This is caused by using an old gcc version
    # See https://bugs.webkit.org/show_bug.cgi?id=25836#c5
    CFLAGS="-march=${MARCH}" CXXFLAGS="-march=${MARCH}" \
    CPPFLAGS="-march=${MARCH}" LDFLAGS="-march=${MARCH}" \
    ./configure -prefix $PREFIX/lib/qt4 \
                -libdir $PREFIX/lib \
                -bindir $PREFIX/bin \
                -headerdir $PREFIX/include/qt4 \
                -datadir $PREFIX/lib/qt4 \
                -L $PREFIX/lib \
                -I $PREFIX/include \
                -release \
                -fast \
                -no-qt3support \
                -nomake examples \
                -nomake demos \
                -nomake docs \
                -opensource \
                -verbose \
                -openssl \
                -webkit \
                -system-libpng \
                -system-zlib \
                -gtkstyle \
                -dbus

    # Build on RPM based distros fails without setting LD_LIBRARY_PATH
    # to the build lib dir
    # See https://bugreports.qt.io/browse/QTBUG-5385
    LD_LIBRARY_PATH=$SRC_DIR/lib make -j $CPU_COUNT

    make install
fi

if [ `uname` == Darwin ]; then
    # Leave Qt set its own flags and vars, else compilation errors
    # will occur
    for x in OSX_ARCH CFLAGS CXXFLAGS LDFLAGS
    do
        unset $x
    done

    # Executable file names
    EXEC_FILES=( Assistant.app Designer.app Linguist.app QMLViewer.app \
    lconvert lrelease lupdate macdeployqt moc pixeltool.app qcollectiongenerator \
    qdoc3 qhelpconverter.app qhelpgenerator qmake qmlplugindump qt3to4 \
    qttracereplay.app rcc uic xmlpatterns xmlpatternsvalidator )

    chmod +x configure
    ./configure -prefix $PREFIX/lib/qt4 \
                -libdir $PREFIX/lib \
                -bindir $PREFIX/bin \
                -headerdir $PREFIX/include/qt4 \
                -datadir $PREFIX/lib/qt4 \
                -L $PREFIX/lib \
                -I $PREFIX/include \
                -release \
                -fast \
                -no-qt3support \
                -nomake examples \
                -nomake demos \
                -nomake docs \
                -opensource \
                -verbose \
                -openssl \
                -system-libpng \
                -system-zlib \
                -no-framework \
                -platform macx-g++ \
                -arch `uname -m` 

    make -j $(sysctl -n hw.ncpu)
    make install
fi


# Post build setup
# ----------------

# Remove unneeded files
pushd $PREFIX/lib/qt4
rm -rf phrasebooks translations q3porting.xml
popd

# Rename executables to avoid clashes with Qt5
BIN=$PREFIX/bin
for file in "${EXEC_FILES[@]}"
do
    if [[ $file == *.app ]]; then
        mv $BIN/$file $BIN/${file%.app}-qt4.app
    else
        mv $BIN/$file $BIN/$file-qt4
    fi
done

# Add qt.conf file to the package to make it fully relocatable
cp $RECIPE_DIR/qt.conf $BIN/
