#!/usr/bin/env bash

set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

python - <<'PY'
from __future__ import annotations

import datetime
import re
from pathlib import Path

pyproject = Path("pyproject.toml")
text = pyproject.read_text(encoding="utf-8")

today = datetime.date.today()
expected = f"{today.year - 2000}.{today.month}.{today.day}"

project_section = re.search(
    r"(?ms)^(\[project\]\s*)(.*?)(?=^\[|\Z)",
    text,
)

if project_section is None:
    raise SystemExit("Could not find [project] section in pyproject.toml")

section = project_section.group(0)

new_section, count = re.subn(
    r'(?m)^(\s*version\s*=\s*)(["\'])(.*?)(\2)(.*)$',
    rf"\g<1>\g<2>{expected}\g<4>\g<5>",
    section,
    count=1,
)

if count != 1:
    raise SystemExit("Could not find version entry in [project] section of pyproject.toml")

if new_section != section:
    updated = text[: project_section.start()] + new_section + text[project_section.end() :]
    pyproject.write_text(updated, encoding="utf-8")
    print(f"Updated pyproject.toml version to {expected}")
else:
    print(f"pyproject.toml version already current: {expected}")
PY

ruff format src tests
ruff check --fix src tests
# Search the user site-packages too, but only if it exists: ty rejects a
# missing --extra-search-path (e.g. no ~/.local, or HOME unset).
ty_args=()
p=$(python3 -m site --user-site 2>/dev/null || true)
if [ -n "$p" ] && [ -d "$p" ]; then
    ty_args+=("--extra-search-path=$p")
fi
ty check "${ty_args[@]}"
bandit -q -c pyproject.toml -r src
pytest tests

# Include the version bump in the commit automatically.
git add pyproject.toml
