# SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
#
# SPDX-License-Identifier: EUPL-1.2

# OpenTalk Compose developer commands

set shell := ["bash", "-ceu"]

# The product version comes from .versions.yml, the single source of truth for
# component versions. CI passes the tag verbatim, including the leading "v", so
# local builds derive it the same way and produce the same package version.
version := `sed -n -E 's/^opentalk_product_version:[[:space:]]*([^[:space:]#]+).*$/\1/p' .versions.yml | head -n1`
# Packaging iteration, not a component version: same sources, rebuilt package.
release := "1"
docs_port := env_var_or_default("DOCS_PORT", "8082")

_default:
    @just --list

# Run the standard repository checks.
check: check-packaging

# Install build dependencies and development tools.
setup: install-deps setup-tools

# Install the tools used for packaging, documentation, and linting.
setup-tools:
    uv tool install --upgrade pre-commit
    uv tool install --upgrade 'mkdocs<2' \
        --with mkdocs-material \
        --with mkdocs-literate-nav \
        --with pymdown-extensions
    if python3 -c 'import importlib.util; raise SystemExit(0 if importlib.util.find_spec("privacy") else 1)' 2>/dev/null; then :; else echo "Warning: MkDocs privacy plugin is not available from PyPI; install the project-specific plugin before running just docs." >&2; fi
    if command -v gem >/dev/null 2>&1; then gem install --user-install mdl; else echo "RubyGems is required for mdl." >&2; exit 1; fi
    pre-commit install

# Verify that packaging prerequisites and scripts are available.
check-packaging:
    extras/packaging/check-packaging.sh

# Check whether all native packaging dependencies are installed.
check-deps:
    BUILD_TARGET=both extras/packaging/install-build-deps.sh --check

# Install native dependencies required to build packages.
install-deps:
    extras/packaging/install-build-deps.sh --install

# Build the Debian package.
build-deb:
    extras/packaging/build-all.sh --deb --version {{version}} --release {{release}}

# Build the Debian package for a supplied version.
build-deb-version package_version:
    extras/packaging/build-all.sh --deb --version {{package_version}} --release {{release}}

# Build the RPM package.
build-rpm:
    extras/packaging/build-all.sh --rpm --version {{version}} --release {{release}}

# Build the RPM package for a supplied version.
build-rpm-version package_version:
    extras/packaging/build-all.sh --rpm --version {{package_version}} --release {{release}}

# Build both Debian and RPM packages.
build:
    extras/packaging/build-all.sh --deb --rpm --auto-revision --version {{version}} --release {{release}}

# Build both packages with an automatically derived local revision.
build-local:
    extras/packaging/build-all.sh --deb --rpm --auto-revision --version {{version}} --release {{release}}

# Build both packages without downloading external dependencies.
build-offline:
    extras/packaging/build-all.sh --deb --rpm --offline --version {{version}} --release {{release}}

# Install the pinned Ansible Collections that the fast tests use. The local
# development stack lives at the repository root: see `just dev`.
collections:
    extras/scripts/local-stack.sh collections

# Verify the contents and metadata of a Debian package.
verify-deb package:
    extras/packaging/verify-packages.sh {{package}}

# Verify the contents and metadata of an RPM package.
verify-rpm package:
    extras/packaging/verify-packages.sh {{package}}

# Run all configured pre-commit checks scoped to this directory.
# `pre-commit` always resolves its config from the repository root, so from
# here that's the monorepo root; scope the file set to `compose/` to avoid
# also running unrelated checks (e.g. the Rust workspace's cargo hooks).
lint:
    pre-commit run --files $(git ls-files .)

# Run fast, Docker-free regression and contract tests.
test-fast: lint test-fast-no-lint

# Run fast, Docker-free regression and contract tests without linting.
test-fast-no-lint:
    find extras/scripts/tests -maxdepth 1 -type f -name 'test_*.sh' -print0 | xargs -0r -n1 bash

# Measure shell line coverage via bash -x tracing and enforce baseline thresholds.
test-coverage:
    bash extras/scripts/tests/test_opentalk_coverage_regression.sh

# Run only a single shell coverage measurement (TARGET is a test script path).
test-coverage-one target:
    python3 extras/scripts/coverage/measure_shell_coverage.py --verbose {{target}}

# Run a single Python coverage measurement (TARGET is a script path, args are forwarded).
test-python-coverage target args="":
    python3 extras/scripts/coverage/measure_python_coverage.py --verbose {{target}} {{args}}

# Run the destructive Docker Compose end-to-end test in an isolated local environment.
test-e2e:
    OPENTALK_E2E=1 bash extras/scripts/tests/integration/test_compose_e2e.sh

# Remove generated build files while keeping package artifacts.
clean:
    extras/packaging/90-cleanup.sh --build-only -y

# Remove generated package artifacts.
clean-artifacts:
    extras/packaging/90-cleanup.sh --artifacts -y

# Remove all generated build files and package artifacts.
clean-all:
    extras/packaging/90-cleanup.sh --all -y
