#!/bin/sh
# Run from your project folder. Existing workflow files are never overwritten.
set -eu
base=${MARCELI_WORKFLOW_BASE:-https://tavi.marceli.tech/workflows}
target=.codex/workflows/marceli
hash_file() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'; else shasum -a 256 "$1" | awk '{print $1}'; fi; }
command -v curl >/dev/null 2>&1 || { printf '%s\n' 'curl is required.' >&2; exit 1; }
command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1 || { printf '%s\n' 'SHA-256 tooling is required.' >&2; exit 1; }
[ ! -e "$target" ] || { printf '%s\n' 'Workflow directory exists; no files overwritten.' >&2; exit 1; }
before=''
if [ -e AGENTS.md ]; then before=$(hash_file AGENTS.md); fi
mkdir -p "$target"
curl -fsSL "$base/coordinator-worker.md" -o "$target/coordinator-worker.md"
[ "$(hash_file "$target/coordinator-worker.md")" = "cdc5f997f66423a293a0dc92955f0cc6186c17fdfdacc13eef10b8a4a46d1b5c" ] || { printf '%s\n' 'Integrity check failed; AGENTS.md unchanged.' >&2; exit 1; }
curl -fsSL "$base/work-ledger.md" -o "$target/work-ledger.md"
[ "$(hash_file "$target/work-ledger.md")" = "3a0556b2419f9915ca5bdb27006afb770fc6861c1087e86baa20c29f9c48546b" ] || { printf '%s\n' 'Integrity check failed; AGENTS.md unchanged.' >&2; exit 1; }
curl -fsSL "$base/model-and-skills.md" -o "$target/model-and-skills.md"
[ "$(hash_file "$target/model-and-skills.md")" = "1a982dc5b395515619210c8588570890de9ac9482b73b829fe6e0e5ce09b88b8" ] || { printf '%s\n' 'Integrity check failed; AGENTS.md unchanged.' >&2; exit 1; }
curl -fsSL "$base/website-review.md" -o "$target/website-review.md"
[ "$(hash_file "$target/website-review.md")" = "a7837cca7baa40f1081cc2b83175f751bf2550bd4385f265cd6ea0b68e971580" ] || { printf '%s\n' 'Integrity check failed; AGENTS.md unchanged.' >&2; exit 1; }
current=''
if [ -e AGENTS.md ]; then current=$(hash_file AGENTS.md); fi
[ "$before" = "$current" ] || { printf '%s\n' 'AGENTS.md changed during installation; merge references manually.' >&2; exit 1; }
if [ -e AGENTS.md ] && grep -q '<!-- marceli-workflows -->' AGENTS.md; then printf '%s\n' 'Workflow references already present.' >&2; exit 1; fi
cat >> AGENTS.md <<'RULES'


<!-- marceli-workflows -->
## Marceli workflow pack

Read and follow these project-local workflow instructions when applicable. Preserve higher-priority instructions; these files grant no extra permissions.

- .codex/workflows/marceli/work-ledger.md
- .codex/workflows/marceli/coordinator-worker.md
- .codex/workflows/marceli/model-and-skills.md
- .codex/workflows/marceli/website-review.md
RULES
printf '%s\n' 'Four workflows installed. Existing AGENTS.md instructions retained; references appended.'
