#!/usr/bin/env just --justfile

mod cpp
mod java
mod rust
mod ts

just := quote(just_executable())
ci_mode := if env('CI', '') != '' {'1'} else {''}

# By default, show the list of all available commands
@_default:
    {{just}} --list --list-submodules

bench:
    {{just}} rust::bench
    {{just}} java::bench
    {{just}} ts::bench

# Run integration tests, and override what we expect the output to be with the actual output
bless: _clean-int-test _test-run-int
    {{just}} rust::bless
    rm -rf test/expected && mv test/output test/expected

# Delete all build files for multiple languages
clean:
    {{just}} rust::clean
    {{just}} java::clean
    {{just}} ts::clean
    {{just}} cpp::clean

# Run all formatting in every language
fmt:
    {{just}} rust::fmt
    {{just}} java::fmt
    {{just}} ts::fmt
    {{just}} cpp::fmt

# Run linting in every language. Run `just fmt` to fix formatting issues.
lint:
    {{just}} rust::lint
    {{just}} java::lint
    {{just}} ts::lint
    {{just}} cpp::lint

# Run all tests in every language, including integration tests
test: test-int
    {{just}} rust::test
    {{just}} java::test
    {{just}} ts::test
    {{just}} cpp::test

# Run integration tests, ensuring that the output matches the expected output
test-int: _clean-int-test _test-run-int (_diff-dirs "test/output" "test/expected")

docs:
    docker run --rm -it -p 8000:8000 -v ${PWD}:/docs zensical/zensical:latest

docs-build:
    docker run --rm -v ${PWD}:/docs zensical/zensical:latest build

# Merge the lcov and JaCoCo coverage reports in `dir` into one Cobertura report at `out`
coverage-merge dir out: (cargo-install 'grcov')
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p "$(dirname {{quote(out)}})"
    grcov {{quote(dir)}} --source-dir . \
        --ignore '**/build/**' --ignore '**/target/**' --ignore '**/node_modules/**' \
        --output-types cobertura --output-path {{quote(out)}}

# Extract version from a tag by removing language prefix and 'v' prefix
ci-extract-version language tag:
    @echo "{{replace(replace(tag, language + '-', ''), 'v', '')}}"

# Run the mlt CLI tool with the given arguments from current dir.
[no-cd]
[positional-arguments]  # avoids shell expansions
mlt *args:
    cargo run --manifest-path {{join(justfile_directory(), 'rust', 'Cargo.toml')}} --package mlt --features unstable-v2 -- "$@"

# Run the mlt CLI tool with the given arguments from current dir.
[no-cd]
[positional-arguments]  # avoids shell expansions
mlt-rel *args:
    cargo run --release --manifest-path {{join(justfile_directory(), 'rust', 'Cargo.toml')}} --package mlt --features unstable-v2 -- "$@"

# Ensure a command is available
assert-cmd command:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! type {{command}} > /dev/null; then
        echo "Command '{{command}}' could not be found. Please make sure it has been installed on your computer."
        exit 1
    fi

# Install a Cargo tool if missing (uses cargo-binstall when available)
cargo-install $COMMAND $INSTALL_CMD='' *args='':
    #!/usr/bin/env bash
    set -euo pipefail
    unset CARGO_BUILD_WARNINGS
    binstall_args="{{ if env('CI', '') != '' {'--no-confirm --no-track --disable-telemetry'} else {''} }}"
    if ! command -v $COMMAND > /dev/null; then
        if ! command -v cargo-binstall > /dev/null; then
            echo "$COMMAND could not be found. Installing it with    cargo install ${INSTALL_CMD:-$COMMAND} --locked {{args}}"
            cargo install ${INSTALL_CMD:-$COMMAND} --locked {{args}}
        else
            echo "$COMMAND could not be found. Installing it with    cargo binstall ${INSTALL_CMD:-$COMMAND} $binstall_args --locked"
            cargo binstall ${INSTALL_CMD:-$COMMAND} $binstall_args --locked
        fi
    fi

# Install the pmtiles CLI and Python pmtiles library (needed by download-benchmark-tiles)
install-pmtiles:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! command -v pmtiles > /dev/null; then
        OS=$(uname -s | tr '[:upper:]' '[:lower:]')
        ARCH=$(uname -m)
        case "$OS-$ARCH" in
            linux-x86_64)   SUFFIX="Linux_x86_64"   ;;
            linux-aarch64)  SUFFIX="Linux_arm64"     ;;
            darwin-x86_64)  SUFFIX="Darwin_x86_64"   ;;
            darwin-arm64)   SUFFIX="Darwin_arm64"    ;;
            *) echo "Unsupported platform: $OS-$ARCH"; exit 1 ;;
        esac
        AUTH=()
        if [ -n "${GITHUB_TOKEN:-}" ]; then
            AUTH=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
        fi
        ASSET_URL=$(curl -sSf "${AUTH[@]}" https://api.github.com/repos/protomaps/go-pmtiles/releases/latest | \
            jq -r ".assets[] | select(.name | test(\"${SUFFIX}\")) | .browser_download_url")
        if [ -z "$ASSET_URL" ] || [ "$ASSET_URL" = "null" ]; then
            echo "Could not find pmtiles release for $SUFFIX"; exit 1
        fi
        curl -sSfL "$ASSET_URL" | sudo tar -xz -C /usr/local/bin pmtiles
    fi
    echo "pmtiles: $(pmtiles version)"
    pip install --break-system-packages pmtiles 2>/dev/null \
        || pip install pmtiles

# Make sure the git repo has no uncommitted changes. Fails only if CI envvar is set.
assert-git-is-clean:
    #!/usr/bin/env bash
    set -euo pipefail
    if [ -n "$(git status --porcelain --untracked-files=all)" ]; then
        >&2 echo "::error::git repo is not clean. Make sure compilation and tests artifacts are in the .gitignore, and no repo files are modified."
        if [[ "{{ci_mode}}" == "1" ]]; then
            >&2 echo "::group::git status"
            git status
            >&2 echo "::endgroup::"
            >&2 echo "::group::git diff (tracked changes)"
            git add . --intent-to-add
            git --no-pager diff
            >&2 echo "::endgroup::"
            exit 1
        else
            >&2 echo "git repo is not clean, but not failing because CI mode is not enabled."
        fi
    fi

_clean-int-test:
    rm -rf test/output && mkdir -p test/output

_diff-dirs OUTPUT_DIR EXPECTED_DIR:
    #!/usr/bin/env bash
    set -euo pipefail
    echo "** Comparing {{OUTPUT_DIR}} with {{EXPECTED_DIR}}..."
    if ! diff --brief --recursive --new-file {{OUTPUT_DIR}} {{EXPECTED_DIR}}; then
        echo "** Expected output does not match actual output"
        echo "** You may want to run 'just bless' to update expected output"
        exit 1
    else
        echo "** Expected output matches actual output"
    fi

_test-run-int:
    echo "TODO: Add integration test command, outputting to test/output"
    echo "fake output by copying expected into output so that the rest of the script works"
    # TODO: REMOVE THIS, and replace it with a real integration test run
    cp -r test/expected/* test/output

# Ensure there are no duplicate synthetic MLT files by comparing their hashes.
_assert-all-mlt-files-different dir='test/synthetic':
    #!/usr/bin/env bash
    set -euo pipefail
    all_hashes=$(find {{quote(dir)}} -name '*.mlt' -exec sha256sum {} \; | sort)
    duplicates=$(echo "$all_hashes" | awk '{print $1}' | uniq -d)
    if [ -n "$duplicates" ]; then
        echo "::error::Duplicate synthetic MLT files found"
        while IFS= read -r hash; do
            echo ""
            echo "$all_hashes" | grep "^$hash " | awk '{print "  - " $2}'
        done <<< "$duplicates"
        exit 1
    fi
