#!/bin/bash -x

QT_BRANCH=6.8.0
QT_VERSION=6.8.0

# Install fedora deps for qt6-qtbase, qt6-qttools
#sudo dnf builddep qt6-qtbase qt6-qttools qt6-qtwebengine -y
#sudo dnf install qt6-qtbase qt6-doctools -y

# Clone full qt tree
#git clone -b $QT_BRANCH git://code.qt.io/qt/qt5.git qt6
git clone -b v$QT_VERSION git://code.qt.io/qt/qt5.git qt6

# Configure using fedora configure basic options
cd qt6 || return
#git submodule foreach "git checkout $QT_BRANCH"
git submodule foreach "git checkout v$QT_VERSION"
git submodule foreach "git fetch"
git submodule foreach "git pull"

# Init the base source
./init-repository

# hard-code docdir for now, rpm --eval %{_qt6_docdir} yields unexpanded %{_docdir}/qt6 , wtf -- rex
./configure -confirm-license -opensource -prefix $(rpm --eval "%{_qt6_prefix}") \
    -archdatadir $(rpm --eval "%{_qt6_archdatadir}") -bindir $(rpm --eval "%{_qt6_bindir}") \
    -libdir $(rpm --eval "%{_qt6_libdir}") -libexecdir $(rpm --eval "%{_qt6_libexecdir}") \
    -datadir $(rpm --eval "%{_qt6_datadir}") -docdir /usr/share/doc/qt6 \
    -examplesdir $(rpm --eval "%{_qt6_examplesdir}") -headerdir $(rpm --eval "%{_qt6_headerdir}") \
    -plugindir $(rpm --eval "%{_qt6_plugindir}") \
    -sysconfdir $(rpm --eval "%{_qt6_sysconfdir}") -translationdir $(rpm --eval "%{_qt6_translationdir}") \
    -platform linux-g++ -release -shared -accessibility -dbus-runtime -fontconfig -glib -gtk \
    -icu -journald -nomake examples -nomake tests -no-rpath -no-separate-debug-info -no-strip \
    -system-libjpeg -system-libpng -system-zlib -no-directfb 

mkdir -p qtbase/lib64/qt6/libexec

ln -s /usr/lib64/qt6/bin/qdoc qtbase/lib64/qt6/bin/qdoc
ln -s /usr/lib64/qt6/bin/qmake qtbase/lib64/qt6/bin/qmake
ln -s /usr/lib64/qt6/libexec/moc qtbase/lib64/qt6/libexec/moc
ln -s /usr/lib64/qt6/libexec/rcc qtbase/lib64/qt6/libexec/rcc
ln -s /usr/lib64/qt6/libexec/uic qtbase/lib64/qt6/libexec/uic
ln -s /usr/lib64/qt6/libexec/qtattributionsscanner qtbase/lib64/qt6/libexec/qtattributionsscanner
ln -s /usr/lib64/qt6/libexec/qhelpgenerator qtbase/lib64/qt6/libexec/qhelpgenerator


# export QT_PLUGIN_PATH to get compiled qhelpgenerator to run
export QT_PLUGIN_PATH=/usr/lib64/qt6/plugins

cmake --build . --target docs
#cmake --build . --target qch_docs

# Install docs on tmp directory
DEST=${PWD}/install
rm -rf $DEST/ && mkdir -p ${DEST}

DESTDIR=$DEST cmake --build . --target install_docs -k

XZ_OPT="-T 2"
tar -C $DEST -cJf ../qt-doc-opensource-src-${QT_VERSION}.tar.xz .
