#!/bin/bash

base_path=$(pwd)
build_path=${base_path}/build
libraries_root="/opt/Libraries"

if command -v cmake >/dev/null 2>&1; then
    cmake_exe=cmake
else
    cmake_exe=/opt/Qt/Tools/CMake/bin/cmake
fi

function cmake_scan() {
    echo "scanning the project..."
    if [ ! -d ${build_path} ]; then
        mkdir -p ${build_path}
    fi
    cd ${build_path}
    ${cmake_exe} -G Ninja -S ${base_path} -B ${build_path} \
        -DCMAKE_BUILD_TYPE=Debug \
        -DMbedTLS_DIR=${libraries_root}/mbedtls-3.6.3.1/lib/cmake/MbedTLS \
        -Dnng_DIR=${libraries_root}/nng-1.11/lib/cmake/nng \
        -DBOOST_ROOT=${libraries_root}/boost_1_88_0
}

function build() {
    echo "building the project..."
    if [ ! -f "${build_path}/CMakeCache.txt" ]; then
        cmake_scan
    fi
    if [ $? -ne 0 ]; then
        exit 1
    fi
    ${cmake_exe} --build ${build_path} --target all
    if [ $? -ne 0 ]; then
        exit 1
    fi
}

function main() {
    local cmd=$1
    shift 1
    case $cmd in
    build)
        build
        ;;
    *)
        build
        ;;
    esac
}

main $@

# docker run -it -d --name ubuntu --hostname Ubuntu -v ~/Projects:/home/amass/Projects registry.cn-shenzhen.aliyuncs.com/amass_toolset/ubuntu_dev:24.04-qt6.9.1
