#!/bin/bash

starttime=$(date +"%Y%m%d%H%M%S")
logfile=build_$starttime.log
exec   > >(tee -ia $logfile)
exec  2> >(tee -ia $logfile >& 2)
exec 19> $logfile
BASH_XTRACEFD=19

# Set script to log commands and exit on failure
set -x
set -e

# submodule set (not including qtbase)
module_set="qtshadertools qtdeclarative qt5compat qtquick3d"
branch=dev
dir=/home/qt/RTA/submodules

rm -rf /home/qt/RTA/submodules
mkdir -p /home/qt/RTA/submodules
cd /home/qt/RTA/submodules

host_prefix=/home/qt/RTA/submodules/qt6_install
target_prefix=/home/qt/RTA/submodules/qt6_target_install

# Build global exports
export CMAKE_GENERATOR="Ninja"

echo "Remove old cmake from toolchain"
rm -rf /home/qt/RTA/B2Qt_*/*/Boot2Qt/*/toolchain/sysroots/*/usr/lib/cmake/Qt6*
rm -rf /home/qt/RTA/B2Qt_*/*/Boot2Qt/*/toolchain/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake.d/OEQt6Toolchain.cmake

function checkoutQtModule {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    cd $dir
    git clone --progress https://codereview.qt-project.org/qt/$1
    cd $dir/$1
    git reset --hard origin/$2 && git clean -dqfx
    git checkout $2
    echo "Checked out $1 revision $(git rev-parse HEAD)"
    git rev-parse HEAD > ../$1_$2_sha1.txt
    if [[ $1 == 'qtquick3d' ]]; then
        git submodule init
        git submodule update
    fi
    cd $dir
}

function reclone_repos {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    for module in $(echo $module_set) qtbase
    do
        echo ""
        echo "Reclone $module"
        rm -rf $module
        checkoutQtModule $module $branch
    done
}

function recreate_build_dir {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    rm -rf $dir/build
    mkdir $dir/build
}

function build_qt_base_host {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    cd $dir/build
    ${dir}/qtbase/configure  -prefix ${host_prefix} -release -nomake examples -nomake tests
    cmake --build . --parallel 8
    cmake --install .
    rm -rf *
    cd $dir
}

function build_module_host {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    cd $dir/build
    ${host_prefix}/bin/qt-configure-module $dir/$1/
    cmake --build . --parallel 8
    cmake --install .
    rm -rf *
    cd $dir
}

function build_host_modules {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    for module in $(echo $module_set)
    do
        build_module_host $module
    done
}

function target_build_prepare {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    source /home/qt/RTA/B2Qt_6.7.3/6.7.3/Boot2Qt/apalis-imx6/toolchain/environment-setup-cortexa9t2hf-neon-poky-linux-gnueabi
}

function build_qt_base_target {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    cd $dir/build
    ${dir}/qtbase/configure -qt-host-path $host_prefix -prefix /usr/local -extprefix $target_prefix -release -nomake examples -nomake tests
    cmake --build . --parallel 8
    cmake --install .
    rm -rf *
    cd $dir
}

function build_target_module {
    echo "Execution function name and caller function: ${FUNCNAME[*]} args: $1"
    cd $dir/build
    ${target_prefix}/bin/qt-configure-module $dir/$1/
    cmake --build . --parallel 8
    cmake --install .
    rm -rf *
    cd $dir
}

function build_target_modules {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    for module in $(echo $module_set)
    do
        build_target_module $module
    done
}


function print_execution_time_from_starttime {
    end=`date +%s`
    runtime=$((end-starttime))
    echo "Execution time in second is $runtime"
}

function remove_old_installations {
    echo "Execution function name and caller function: ${FUNCNAME[*]}"
    rm -rf target_install/ host_install/ build/
}

function print_succesfully_print_for_verifying_purposes {
    echo "Everything done without errors"
}

remove_old_installations

reclone_repos

recreate_build_dir
build_qt_base_host
build_host_modules

target_build_prepare
build_qt_base_target
build_target_modules
print_execution_time_from_starttime
print_succesfully_print_for_verifying_purposes
