#!/usr/bin/env bash
# Parameterize the walking skeleton into your project. Run once, right after
# creating a repo from this template, then delete this script.
#
# <dist-name> is the pip-installable distribution name, lowercase with
# hyphens (for example: spec-drift). Derived from it:
#   - import package  spec_drift   (hyphens become underscores)
#   - console command spec-drift   (same as the dist name)
#   - env-var prefix  SPEC_DRIFT   (package name upper-cased)
#
# The script rewrites the three skeleton tokens (skeleton-cli, skeleton_cli,
# SKELETON_CLI) across repository text files except itself, LICENSE, Git
# internals, virtual environments, and tool caches. It moves src/skeleton_cli/
# to the new package directory and substitutes the template owner's name and
# contact details using the local Git configuration.
# LICENSE is left untouched: relicensing is a deliberate act, not a rename.

set -euo pipefail

usage() {
  cat <<'EOF'
Usage:
  bash scripts/rename-project DIST

DIST must use lowercase letters, digits, and single hyphens (for example: spec-drift).
The import package uses underscores and the environment prefix uses uppercase underscores.
EOF
}

if [ "$#" -ne 1 ]; then
  usage >&2
  exit 2
fi

DIST="$1"
if ! printf '%s' "$DIST" | grep -Eq '^[a-z][a-z0-9]*(-[a-z0-9]+)*$'; then
  printf 'Invalid dist name %s: use lowercase letters, digits, and single hyphens (e.g. spec-drift).\n' "$DIST" >&2
  exit 2
fi

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PKG="${DIST//-/_}"
ENV_PREFIX="$(printf '%s' "$PKG" | tr '[:lower:]' '[:upper:]')"
TEMPLATE_OWNER="Lila Brooks"
NEW_OWNER="$(git -C "$ROOT" config user.name 2>/dev/null || true)"
TEMPLATE_EMAIL="lila.m.brooks@gmail.com"
NEW_EMAIL="$(git -C "$ROOT" config user.email 2>/dev/null || true)"

if [ -d "$ROOT/src/$PKG" ] && [ "$PKG" != "skeleton_cli" ]; then
  printf 'src/%s already exists; refusing to overwrite.\n' "$PKG" >&2
  exit 1
fi

# Every regular file in the repository except this script, LICENSE, Git
# internals, virtual environments, and tool caches. The substitution helper
# skips non-UTF-8 files. Works in and out of a Git checkout.
target_files() {
  find "$ROOT" \
    -type d \( \
      -name .git -o \
      -name .venv -o \
      -name .venv.nosync -o \
      -name .mypy_cache -o \
      -name .pytest_cache -o \
      -name .ruff_cache \
    \) -prune -o \
    -type f \
    ! -name 'rename-project' \
    ! -name 'LICENSE' \
    ! -name '*.pyc' \
    -print
}

substitute() {
  local file="$1"
  python3 - "$file" "$DIST" "$PKG" "$ENV_PREFIX" "$TEMPLATE_OWNER" "${NEW_OWNER:-}" \
    "$TEMPLATE_EMAIL" "${NEW_EMAIL:-}" <<'PY'
import sys
from pathlib import Path

path, dist, pkg, env_prefix, template_owner, new_owner, template_email, new_email = sys.argv[1:9]
p = Path(path)
try:
    text = p.read_text(encoding="utf-8")
except UnicodeDecodeError:
    sys.exit(0)  # binary file; leave it alone
original = text
text = text.replace("skeleton_cli", pkg)
text = text.replace("skeleton-cli", dist)
text = text.replace("SKELETON_CLI", env_prefix)
if new_owner and new_owner != template_owner:
    text = text.replace(template_owner, new_owner)
if new_email and new_email != template_email:
    text = text.replace(template_email, new_email)
if text != original:
    p.write_text(text, encoding="utf-8")
PY
}

count=0
while IFS= read -r file; do
  substitute "$file"
  count=$((count + 1))
done < <(target_files)

if [ -d "$ROOT/src/skeleton_cli" ] && [ "$PKG" != "skeleton_cli" ]; then
  mv "$ROOT/src/skeleton_cli" "$ROOT/src/$PKG"
fi

# Start the generated project at its own 0.1.0 release and replace the
# template's documentation history. Repository URLs remain a README judgment
# step because a local project may not have a remote yet.
PROJECT_DATE="$(date +%F)"
python3 - "$ROOT" "$PKG" "$PROJECT_DATE" "$TEMPLATE_OWNER" "${NEW_OWNER:-}" <<'PY'
import re
import sys
from pathlib import Path

root_arg, package, project_date, template_owner, configured_owner = sys.argv[1:6]
root = Path(root_arg)
owner = configured_owner or template_owner

pyproject = root / "pyproject.toml"
text = pyproject.read_text(encoding="utf-8")
text = re.sub(r'(?m)^version = "[^"]+"$', 'version = "0.1.0"', text, count=1)
pyproject.write_text(text, encoding="utf-8")

package_init = root / "src" / package / "__init__.py"
text = package_init.read_text(encoding="utf-8")
text = re.sub(r'(?m)^__version__ = "[^"]+"$', '__version__ = "0.1.0"', text, count=1)
package_init.write_text(text, encoding="utf-8")

(root / "CHANGELOG.md").write_text(
    f"""# Changelog

All notable changes to this project are documented here. The format is based on
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## 0.1.0 - {project_date}

### Added

- Initial project skeleton.
""",
    encoding="utf-8",
)

(root / "docs" / "log.md").write_text(
    f"""---
title: Documentation log
type: log
status: current
date: {project_date}
owner: {owner}
deciders: [{owner}]
tags: [documentation, log]
---

# Documentation log

Dated changes to the docs bundle, newest first.

## {project_date}

- Started the project documentation bundle from the Python CLI template.
""",
    encoding="utf-8",
)
PY

printf 'Renamed the skeleton to %s (package %s, env prefix %s_*) across %d files.\n' \
  "$DIST" "$PKG" "$ENV_PREFIX" "$count"
if [ -n "$NEW_OWNER" ] && [ "$NEW_OWNER" != "$TEMPLATE_OWNER" ]; then
  printf 'Owner set to %s in docs and pyproject (LICENSE left as-is).\n' "$NEW_OWNER"
fi
if [ -z "$NEW_EMAIL" ]; then
  printf 'No git user.email found; review maintainer contact details in SECURITY.md and CODE_OF_CONDUCT.md.\n'
fi
cat <<'EOF'

Next steps:
  1. Rewrite README.md for your project (it still describes the template).
  2. Review CHANGELOG.md's new 0.1.0 entry, the reset docs/log.md,
     maintainer contact details, and LICENSE ownership.
  3. Create the environment and verify (preferred):
     uv sync --all-extras && uv run make check
     Fallback: python3 -m venv .venv &&
     .venv/bin/pip install -e ".[dev,anthropic,openai]" && make check
  4. If your CLI does not call language models, delete the provider layer:
     the agents/, config/, core/, providers/, runtime/ packages, their tests,
     the anthropic/openai extras in pyproject.toml and requirements.txt,
     and .env.example.
  5. Install the operating contract on top (goal, mapping, hooks, loop):
     bash /path/to/claude-okf-repo-kit/scripts/update-existing-repo .
  6. Remove the single-use generator files and the template-only CI workflow;
     they have no role downstream and its jobs fail once the scripts are gone:
     rm scripts/rename-project scripts/create-project \
       tests/test_create_project.py .github/workflows/template-chassis.yml
EOF
