#!/usr/bin/env bash
# Builds a small practice repository for lesson OPS-250-L01 (Git and GitHub for builders).
# Everything here is SYNTHETIC: the "token" below is a fake demo value, not a secret of any real service.
# Usage:  bash make-practice-repo.sh [target-dir]      (default: ./barg-sabz-automation)
# Windows: run it in Git Bash.
set -euo pipefail
DIR="${1:-barg-sabz-automation}"
if [ -e "$DIR" ]; then echo "error: $DIR already exists; choose another directory" >&2; exit 1; fi
mkdir -p "$DIR" && cd "$DIR"
git init -q -b main
git config user.name "Practice Learner"
git config user.email "learner@example.com"

cat > README.md <<'MD'
# Barg Sabz automation (practice)
Workflows and scripts of the fictional Barg Sabz bookstore. Synthetic data only.
MD
mkdir -p workflows scripts
cat > workflows/order-status.json <<'JSON'
{ "name": "order status reply", "nodes": ["Webhook", "HTTP Request", "Respond to Webhook"], "active": false }
JSON
cat > scripts/check_orders.py <<'PY'
import os
TOKEN = os.environ.get("ORDERS_API_TOKEN")  # read from the environment, never from this file
print("token present:", bool(TOKEN))
PY
git add README.md workflows scripts
git commit -q -m "Add order status workflow and check script"

# A mistake that happens in real projects: a local .env file gets committed...
cat > .env <<'ENV'
ORDERS_API_TOKEN=demo-not-a-real-secret-123
ENV
git add .env
git commit -q -m "Update config"
# ...and is "fixed" by deleting it in the next commit. It is still in the history.
git rm -q .env
git commit -q -m "Remove env file"

# A feature branch with an unmerged change
git switch -q -c feature/timeout
sed -i.bak 's/"active": false/"active": false, "settings": { "timeoutSeconds": 30 }/' workflows/order-status.json && rm -f workflows/order-status.json.bak
git commit -q -am "Add 30s timeout to order status workflow"
git switch -q main

echo "Practice repository ready in: $(pwd)"
git log --oneline --all --graph
