# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

_reponame=qtutilities
pkgname=qtutilities-git
_name=${pkgname%-git}
_tools=(icontool translator)
pkgver=777.9a60e59
pkgrel=1.1
pkgdesc='Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models'
arch=('x86_64')
url="https://github.com/Martchus/${_reponame}"
license=('GPL-2.0-or-later')
depends=(
    'glibc'
    'libgcc'
    'libstdc++'
    'libx11'
    'openssl' # required by translator utility
    'qt6-base'
)
makedepends=(
    'cargo'
    'cmake'
    'clang'
    'git'
    'ninja'
    'c++utilities-git'
    'qt6-tools'
    'qt6-declarative'
)
optdepends=(
  "$_name-doc: for API documentation"
)
provides=(
    'libqtutilities-git.so'
)
source=("${_reponame}::${MARTCHUS_GIT_URL_PREFIX:-git+https://github.com/Martchus}/${_reponame}.git")
sha256sums=('SKIP')

pkgver() {
    echo "$(git -C "${_reponame}" rev-list --count HEAD).$(git -C "${_reponame}" rev-parse --short HEAD)"
}

prepare() {
    cd "${PROJECT_DIR_NAME:-$_reponame}"
    export RUSTUP_TOOLCHAIN=stable
    local manifest
    for manifest in */Cargo.toml; do
        cargo fetch --locked --target host-tuple --manifest-path "$manifest"
    done
}

build() {
    local cmake_options=(
        -B build
        -S "${PROJECT_DIR_NAME:-$_reponame}"
        -G Ninja
        -D QT_PACKAGE_PREFIX=Qt6
        -D CMAKE_BUILD_TYPE=Release
        -D CMAKE_INSTALL_PREFIX=/usr
        -D BUILD_SHARED_LIBS=ON
        -D BUILTIN_TRANSLATIONS=ON
        -D BUILTIN_TRANSLATIONS_OF_QT=OFF
        -D CONFIGURATION_NAME:STRING='git'
        -D CONFIGURATION_PACKAGE_SUFFIX:STRING='-git'
        -D CONFIGURATION_TARGET_SUFFIX:STRING='git'
        -D RUST_BASED_TOOLS:BOOL=OFF
    )
    cmake "${cmake_options[@]}"
    cmake --build build --target all

    cd "${PROJECT_DIR_NAME:-$_reponame}"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    local manifest
    for manifest in */Cargo.toml; do
        cargo build --manifest-path "$manifest" --frozen --release --all-features
    done
}

check() {
    cmake --build build --target tests

    local ctest_flags=(
        --test-dir build
        --output-on-failure
        --parallel $(nproc)
    )
    export QT_QPA_PLATFORM=offscreen
    ctest "${ctest_flags[@]}"

    cd "${PROJECT_DIR_NAME:-$_reponame}"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    local manifest
    for manifest in */Cargo.toml; do
        cargo test --manifest-path "$manifest" --frozen --all-features
    done
}

package() {
    depends+=(
        'libc++utilities-git.so'
        'libcrypto.so'
        'libssl.so'
    )

    DESTDIR="${pkgdir}" cmake --install build
    cd "${PROJECT_DIR_NAME:-$_reponame}"
    local manifest
    for manifest in */Cargo.toml; do
        install --verbose -Dm0755 -t "$pkgdir/usr/lib/$pkgname/" "target/release/${manifest%/*}"
    done
}
