# Local development. `just` lists the recipes.

store := "data"
# Preferred port; the server moves to the next free one if it's taken.
port := env("PORT", "8080")
ts_port := "8443"

default:
    @just --list

# Run everything locally, no sign-in: editor at http://write.localhost:{{port}}, sites at http://<id>.localhost:{{port}}.
dev *args: seed
    cargo run -- serve --dev --store {{store}} --publish {{store}}/public --addr 127.0.0.1:{{port}} {{args}}

# Like `dev`, but with passkey sign-in (create an account with `just invite <id>`).
dev-auth *args: seed
    cargo run -- serve --store {{store}} --publish {{store}}/public --addr 127.0.0.1:{{port}} {{args}}

# Like `dev-auth`, reachable over Tailscale HTTPS (passkeys need https off localhost).
dev-ts: seed
    #!/usr/bin/env bash
    set -euo pipefail
    host=$(tailscale status --self --json | jq -r '.Self.DNSName' | sed 's/\.$//')
    if ! tailscale serve --bg --https={{ts_port}} http://127.0.0.1:{{port}} >/dev/null 2>&1; then
      echo "tailscale serve was refused. Allow your user once with:" >&2
      echo "  sudo tailscale set --operator=\$USER" >&2
      exit 1
    fi
    trap 'tailscale serve --https={{ts_port}} off >/dev/null 2>&1 || true' EXIT
    echo "Editor on https://$host:{{ts_port}} (tailnet only)"
    cargo run -- serve --store {{store}} --publish {{store}}/public --addr 127.0.0.1:{{port}} \
      --editor-origin "https://$host:{{ts_port}}"

# Print a one-time link that creates user ID (the first user becomes admin).
invite id:
    cargo run -q -- invite --id {{id}} --store {{store}} --addr 127.0.0.1:{{port}}

# Create a demo site if the store has none yet.
seed:
    #!/usr/bin/env bash
    set -euo pipefail
    [ -d {{store}}/sites ] && exit 0
    cargo run -q -- new-site main --store {{store}}
    mkdir -p {{store}}/sites/main/content/articles
    printf '+++\ntitle = "Hello"\ndate = %s\n+++\n\nWrite something from **write.localhost**.\n' "$(date +%F)" \
      > {{store}}/sites/main/content/articles/hello.md

# Check the tools you need and install the git hooks.
dev-setup:
    #!/usr/bin/env bash
    set -uo pipefail
    missing=0
    need() { command -v "$1" >/dev/null || { echo "missing: $1 ($2)" >&2; missing=1; }; }
    want() { command -v "$1" >/dev/null || echo "optional: $1 ($2)"; }
    need cargo "https://rustup.rs"
    need git "version control"
    want nasm "faster AVIF encoding"
    want bun "for \`just e2e\` and \`just editor\`"
    want tailscale "only for \`just dev-ts\`"
    want jq "only for \`just dev-ts\`"
    [ $missing = 0 ] || exit 1
    git config core.hooksPath .githooks
    echo "git hooks installed from .githooks/"

fmt:
    cargo fmt

test:
    cargo clippy --all-targets -- -D warnings
    cargo test

# Browser tests for the editor and passkeys (needs bun and a Chromium).
e2e:
    ./e2e/run.sh

# Optimised binary in target/release/blog.
build:
    cargo build --release

# Build the container image.
docker:
    docker build -t blog .

# Run the image locally with a named volume (editor at http://write.localhost:8080).
docker-run:
    docker run --rm -p 8080:8080 -v blog-data:/data blog

# Rebuild the committed CodeMirror bundle after changing editor/src.
editor:
    cd editor && bun install && bun run build
