#!/usr/bin/env bash
# new-project — scaffold a new claude-projects project + matching ~/projects dir,
# register it, then commit & push claude-projects.
# Usage: new-project <name>
set -euo pipefail

name=${1:?usage: new-project <name>}
case "$name" in
  */* | *" "* | .*) echo "ungültiger Name: $name" >&2; exit 1 ;;
esac

cproot="$HOME/claude-projects"
root="$cproot/$name"
[ -e "$root" ] && { echo "existiert bereits: $root" >&2; exit 1; }

# 1. Code-Verzeichnis unter ~/projects
mkdir -p "$HOME/projects/$name"

# 2. claude-projects-Scaffold
mkdir -p "$root/.claude" "$root/memory"

cat > "$root/.claude/settings.json" <<EOF
{
  "autoMemoryDirectory": "~/claude-projects/$name/memory",
  "permissions": {
    "allow": [
      "Edit(~/projects/$name/**)",
      "Edit(~/claude-projects/$name/**)"
    ],
    "additionalDirectories": [
      "~/projects/$name"
    ]
  }
}
EOF

cat > "$root/.claude/settings.local.json" <<'EOF'
{
  "permissions": {
    "allow": []
  }
}
EOF

printf '# Memory Index\n' > "$root/memory/MEMORY.md"

# 3. claude-projects committen & pushen (nur den neuen Projektordner)
git -C "$cproot" add "$name"
git -C "$cproot" commit -q -m "new project: $name"
git -C "$cproot" push -q

echo "angelegt:"
echo "  ~/projects/$name         (Code-Verzeichnis)"
echo "  ~/claude-projects/$name  (.claude/settings*, memory/MEMORY.md)"
echo "  registriert in additionalDirectories + Edit-Regeln, committet & gepusht"
