# Install local development tooling.
install: install-git-hooks check-install npm-install generate-env

# Remove build caches (tsc / eslint) for the trapos-server tool.
clean:
    npm run clean --prefix tools/trapos-server

# Remove generated local environment files (e.g. .env with tokens).
clean-env:
    rm -f .env

# Recreate local generated files and development tooling.
reinstall: clean install

# Install Git hooks for this repository.
install-git-hooks:
    @mkdir -p .git/hooks
    @printf '%s\n' '#!/bin/sh' '' 'just check test' > .git/hooks/pre-commit
    @chmod +x .git/hooks/pre-commit
    @printf '%s\n' 'Installed .git/hooks/pre-commit'
    @printf '%s\n' '#!/bin/sh' '' 'just ci' > .git/hooks/pre-push
    @chmod +x .git/hooks/pre-push
    @printf '%s\n' 'Installed .git/hooks/pre-push'

# Generate local secrets on first install.
generate-env:
    #!/usr/bin/env bash
    set -euo pipefail
    if [ -f .env ]; then
        exit 0
    fi
    while IFS= read -r line || [ -n "$line" ]; do
        case "$line" in
            OPENCODE_SERVER_PASSWORD=*|TRAPOS_SERVER_PASSWORD=*)
                # A fresh secret per service: reusing one across both trust domains
                # would widen the blast radius if either leaks.
                printf '%s\n' "${line%%=*}=$(openssl rand -hex 32)"
                ;;
            *)
                printf '%s\n' "$line"
                ;;
        esac
    done < env.sample > .env
    printf '%s\n' 'Generated .env'
