Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·302 lines (279 loc) · 11.3 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·302 lines (279 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
# Installs the second-brain vault system from this repo into a real vault
# in iCloud Drive, and wires up every AI tool listed in harnesses.conf so
# each one finds the vault from any directory, not just from inside it.
# Safe to re-run: every step checks whether it already happened before
# doing anything, so running this again on the same Mac, or on a second
# Mac after the vault has already synced through iCloud, won't overwrite
# content that's already there.
set -euo pipefail
shopt -s nullglob
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VAULT="$HOME/Library/Mobile Documents/com~apple~CloudDocs/vault"
# A second, space-free path to the same vault. Claude Code's @file import
# syntax silently fails to resolve any path containing a space, and this
# has no fix planned upstream (github.com/anthropics/claude-code/issues/56927,
# closed "not planned"). The vault's real iCloud Drive path always has one
# ("Mobile Documents"), so any global import that needs an absolute path
# is written through this symlink instead of the real path below.
VAULT_LINK="$HOME/.brain-vault"
CONF="$SCRIPT_DIR/harnesses.conf"
ZSHRC="$HOME/.zshrc"
VAULT_ESCAPED="${VAULT// /\\ }"
BRAIN_CD="cd $VAULT_ESCAPED"
echo "Vault path: $VAULT"
if [ ! -f "$CONF" ]; then
echo "harnesses.conf not found next to setup.sh, nothing to wire up. Stopping." >&2
exit 1
fi
# 0. Space-free vault symlink (see VAULT_LINK comment above).
if [ -L "$VAULT_LINK" ]; then
existing_target="$(readlink "$VAULT_LINK")"
if [ "$existing_target" = "$VAULT" ]; then
echo "Space-free vault symlink already in place: $VAULT_LINK"
else
echo "$VAULT_LINK is a symlink to something else ($existing_target)." >&2
echo " Not touching it. Global imports that route through it may not resolve" >&2
echo " correctly until this is fixed: ln -sf \"$VAULT\" \"$VAULT_LINK\"" >&2
fi
elif [ -e "$VAULT_LINK" ]; then
echo "$VAULT_LINK already exists and isn't a symlink, leaving it alone." >&2
echo " Global imports that route through it may not resolve correctly until" >&2
echo " this is fixed: ln -sf \"$VAULT\" \"$VAULT_LINK\"" >&2
else
ln -s "$VAULT" "$VAULT_LINK"
echo "Space-free vault symlink ready: $VAULT_LINK -> $VAULT"
fi
# Parse harnesses.conf into parallel arrays. Plain bash 3.2 (macOS's
# default) has no associative arrays, so this is the portable option.
# harnesses.conf is a small block format, not real YAML: `[key]` starts
# a harness, then plain `field: value` lines until the next `[key]` or
# end of file. See the comments at the top of that file for the schema.
H_KEY=()
H_NAME=()
H_CMD=()
H_SHIM_FILE=()
H_SHIM_CONTENT=()
H_STRATEGY=()
H_GLOBAL=()
H_IMPORT=()
cur_key=""
cur_name=""; cur_cmd=""; cur_shim_file=""; cur_shim_content=""
cur_strategy=""; cur_global=""; cur_import=""
flush_harness() {
if [ -n "$cur_key" ]; then
H_KEY+=("$cur_key")
H_NAME+=("$cur_name")
H_CMD+=("$cur_cmd")
H_SHIM_FILE+=("$cur_shim_file")
H_SHIM_CONTENT+=("$cur_shim_content")
H_STRATEGY+=("$cur_strategy")
H_GLOBAL+=("${cur_global/#\~/$HOME}")
H_IMPORT+=("$cur_import")
fi
}
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
''|'#'*) continue ;;
esac
if [[ "$line" =~ ^\[([A-Za-z0-9_-]+)\]$ ]]; then
flush_harness
cur_key="${BASH_REMATCH[1]}"
cur_name=""; cur_cmd=""; cur_shim_file=""; cur_shim_content=""
cur_strategy=""; cur_global=""; cur_import=""
continue
fi
field="${line%%:*}"
value="${line#*:}"
value="${value# }"
case "$field" in
name) cur_name="$value" ;;
launch) cur_cmd="$value" ;;
project_shim_file) cur_shim_file="$value" ;;
project_shim_content) cur_shim_content="$value" ;;
global_strategy) cur_strategy="$value" ;;
global_memory) cur_global="$value" ;;
global_import) cur_import="$value" ;;
*) echo " warning: unrecognized field '$field' in harnesses.conf under [$cur_key], ignoring." >&2 ;;
esac
done < "$CONF"
flush_harness
if [ "${#H_KEY[@]}" -eq 0 ]; then
echo "harnesses.conf has no harness blocks (everything's commented out or blank). Stopping." >&2
exit 1
fi
echo "Harnesses in harnesses.conf: ${H_NAME[*]} (default: ${H_NAME[0]})"
# 1. Folder structure
mkdir -p "$VAULT"/areas/{career,household,finances,health,people,family,hobbies}
mkdir -p "$VAULT"/learning
mkdir -p "$VAULT"/projects
mkdir -p "$VAULT"/notes
mkdir -p "$VAULT"/raw
mkdir -p "$VAULT"/sources
mkdir -p "$VAULT"/_templates
mkdir -p "$VAULT"/_private
echo "Folder structure ready."
# 2. Core files: copy only if missing, never overwrite something already
# there (on a second Mac these already exist, synced through iCloud).
copy_if_missing() {
local src="$1" dest="$2"
if [ -f "$dest" ]; then
echo " skip (already exists): $dest"
else
cp "$src" "$dest"
echo " created: $dest"
fi
}
echo "Core files:"
copy_if_missing "$SCRIPT_DIR/AGENTS.md" "$VAULT/AGENTS.md"
copy_if_missing "$SCRIPT_DIR/STYLE.md" "$VAULT/STYLE.md"
copy_if_missing "$SCRIPT_DIR/index.md" "$VAULT/index.md"
copy_if_missing "$SCRIPT_DIR/log.md" "$VAULT/log.md"
copy_if_missing "$SCRIPT_DIR/.gitignore" "$VAULT/.gitignore"
echo "Templates:"
for f in "$SCRIPT_DIR"/_templates/*.md; do
copy_if_missing "$f" "$VAULT/_templates/$(basename "$f")"
done
# 3. Project-level shim files, one per harness that needs one. Some tools
# read AGENTS.md directly inside the vault folder (shim file "-" in
# harnesses.conf); others look for their own filename and never read
# AGENTS.md on their own, so they get a one-line file pointing at it.
echo "Project-level files:"
for i in "${!H_KEY[@]}"; do
shim_file="${H_SHIM_FILE[$i]}"
if [ "$shim_file" = "-" ]; then
echo " ${H_NAME[$i]}: reads AGENTS.md directly, no shim needed."
continue
fi
dest="$VAULT/$shim_file"
if [ -f "$dest" ]; then
echo " skip (already exists): $dest"
else
echo "${H_SHIM_CONTENT[$i]}" > "$dest"
echo " created: $dest (${H_NAME[$i]})"
fi
done
# 4. Global wiring, one per harness. This is what makes "save to brain"
# work from any project, not only from a session started inside the
# vault. Two strategies, per harnesses.conf's "global strategy" field:
# append_import - append an import line to the tool's global file,
# without touching anything else already there.
# symlink - the tool has no import syntax, so its global file
# becomes a symlink at AGENTS.md, but only if that file doesn't
# already exist with real content.
echo "Global wiring:"
for i in "${!H_KEY[@]}"; do
name="${H_NAME[$i]}"
strategy="${H_STRATEGY[$i]}"
global_path="${H_GLOBAL[$i]}"
case "$strategy" in
append_import)
import_line="${H_IMPORT[$i]//\{VAULT\}/$VAULT_LINK}"
mkdir -p "$(dirname "$global_path")"
touch "$global_path"
if grep -qF "$import_line" "$global_path" 2>/dev/null; then
echo " $name: already imports the vault, leaving it alone."
else
{
echo ""
echo "$import_line"
} >> "$global_path"
echo " $name: appended vault import to $global_path"
fi
;;
symlink)
target="$VAULT/AGENTS.md"
if [ -L "$global_path" ]; then
current_target="$(readlink "$global_path")"
if [ "$current_target" = "$target" ]; then
echo " $name: already symlinked to the vault, leaving it alone."
else
echo " $name: $global_path is a symlink to something else ($current_target)."
echo " Not touching it. To point it at the vault yourself:"
echo " ln -sf \"$target\" \"$global_path\""
fi
elif [ -e "$global_path" ]; then
echo " $name: $global_path already exists and isn't a symlink."
echo " Not touching it, this tool has no import syntax so merging"
echo " isn't safe to automate. To point it at the vault yourself:"
echo " ln -sf \"$target\" \"$global_path\""
else
mkdir -p "$(dirname "$global_path")"
ln -s "$target" "$global_path"
echo " $name: symlinked $global_path -> $target"
fi
;;
*)
echo " $name: unknown global strategy '$strategy' in harnesses.conf, skipping." >&2
;;
esac
done
# 5. Git, kept out of the iCloud-synced folder itself (see README: "Why
# not a plain git init"). --separate-git-dir puts a small .git file
# (a pointer, not the real git directory) in the vault, and the real
# git directory lives outside iCloud entirely. Checked by whether
# there's an actual commit yet, not just whether .git exists: git
# init creates that pointer file before add/commit run, so if a
# previous run got interrupted between init and commit (no git
# identity configured, say), this retries the rest instead of getting
# permanently stuck reporting "already a git repo."
if git -C "$VAULT" rev-parse -q --verify HEAD > /dev/null 2>&1; then
echo "Vault git repo already has a commit, leaving it alone."
else
mkdir -p "$HOME/.local-git"
( cd "$VAULT" && \
git init --separate-git-dir="$HOME/.local-git/vault.git" && \
git add -A && \
git commit -m "Initial vault setup" )
echo "Initialized git (separate git dir at ~/.local-git/vault.git) and made the first commit."
fi
# 6. Daily-use aliases, one per harness (brain-<key>), plus a plain
# `brain` for whichever harness is the first block in harnesses.conf.
# Not what makes "save to brain" work, that's the global wiring in
# step 4, and it already works from any directory. These are just
# shortcuts for when you want a session that starts inside the vault
# itself, instead of filing into it from somewhere else: cd's into the
# vault's real iCloud Drive path (long enough that typing it by hand
# gets old) and launches that tool there. A session started this way
# also has nothing "outside" its working directory, so it skips the
# cross-directory permission prompt a session started elsewhere would
# hit on its first write into the vault. See README's "Why the alias"
# for more.
echo "Aliases:"
for i in "${!H_KEY[@]}"; do
key="${H_KEY[$i]}"
cmd="${H_CMD[$i]}"
alias_name="brain-$key"
alias_line="alias $alias_name='$BRAIN_CD && $cmd'"
if [ -f "$ZSHRC" ] && grep -qF "alias $alias_name=" "$ZSHRC" 2>/dev/null; then
echo " $alias_name already in $ZSHRC, leaving it alone."
else
{
echo ""
echo "$alias_line"
} >> "$ZSHRC"
echo " added $alias_name (launches ${H_NAME[$i]})"
fi
if [ "$i" -eq 0 ]; then
default_line="alias brain='$BRAIN_CD && $cmd'"
if [ -f "$ZSHRC" ] && grep -qF "alias brain=" "$ZSHRC" 2>/dev/null; then
echo " brain already in $ZSHRC, leaving it alone (may not be ${H_NAME[$i]} if that line was added before this became the first block in harnesses.conf; see harnesses.conf's note on changing the default)."
else
{
echo ""
echo "$default_line"
} >> "$ZSHRC"
echo " added brain -> default, launches ${H_NAME[$i]} (first block in harnesses.conf)"
fi
fi
done
cat <<EOF
Done. Next:
1. Open the vault folder in Obsidian and confirm it renders correctly
(this script can't check that step itself).
2. Run the post-setup test in README.md for each tool you actually
use: open it from an unrelated directory and say "save to brain" to
confirm the whole chain works end to end. \`brain\` launches
${H_NAME[0]} (first block in harnesses.conf); \`brain-<key>\` launches
any other harness listed there, e.g. \`brain-codex\`.
EOF