-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·244 lines (220 loc) · 7.29 KB
/
Copy pathuninstall.sh
File metadata and controls
executable file
·244 lines (220 loc) · 7.29 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
#!/usr/bin/env bash
#
# 2ndBrain-mogging — uninstaller
#
# Reverses every install step:
# - unloads + removes launchd plists from ~/Library/LaunchAgents
# - removes our symlinks from ~/.claude/{skills,commands,agents}/
# - restores the most recent settings.json backup (0600 perms)
# - with --hard: also removes the Claude-Memory symlink inside the vault
# - with --keep-backups: leaves ~/.claude/.backups/ alone
#
# Vault content (notes, files) is NEVER touched.
#
set -euo pipefail
IFS=$'\n\t'
SCRIPT_PATH="${BASH_SOURCE[0]}"
while [[ -L "$SCRIPT_PATH" ]]; do
SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)"
SCRIPT_PATH="$(readlink "$SCRIPT_PATH")"
[[ "$SCRIPT_PATH" != /* ]] && SCRIPT_PATH="$SCRIPT_DIR/$SCRIPT_PATH"
done
REPO_ROOT="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)"
CLAUDE_HOME="${CLAUDE_HOME:-$HOME/.claude}"
BACKUP_DIR_ROOT="$CLAUDE_HOME/.backups"
SETTINGS_PATH="$CLAUDE_HOME/settings.json"
LAUNCHAGENTS_DIR="$HOME/Library/LaunchAgents"
HARD=0
KEEP_BACKUPS=0
VAULT=""
VERBOSE=0
usage() {
cat <<'USAGE'
Usage: uninstall.sh [--vault PATH] [--hard] [--keep-backups] [--verbose]
Options:
--vault PATH Path to vault (required with --hard)
--hard Also remove the Claude-Memory symlink inside the vault
--keep-backups Do not delete ~/.claude/.backups (still restores)
--verbose Extra logging
USAGE
}
while [[ $# -gt 0 ]]; do
case "$1" in
--vault) VAULT="${2:-}"; shift 2 ;;
--vault=*) VAULT="${1#*=}"; shift ;;
--hard) HARD=1; shift ;;
--keep-backups) KEEP_BACKUPS=1; shift ;;
--verbose) VERBOSE=1; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "unknown flag: $1" >&2; usage >&2; exit 2 ;;
esac
done
log() { printf '[uninstall] %s\n' "$*" >&2; }
vlog() { [[ "$VERBOSE" -eq 1 ]] && printf '[uninstall:v] %s\n' "$*" >&2 || true; }
warn() { printf '[uninstall:WARN] %s\n' "$*" >&2; }
# ---- 1. unload + remove launchd plists ---------------------------------------
uninstall_launchd() {
log "unload + remove launchd plists"
local src_dir="$REPO_ROOT/scheduled/launchd"
[[ -d "$src_dir" ]] || { vlog "no plists to remove"; return 0; }
shopt -s nullglob
local plist name dest
for plist in "$src_dir"/*.plist; do
name="$(basename "$plist")"
dest="$LAUNCHAGENTS_DIR/$name"
if [[ -f "$dest" ]]; then
launchctl unload "$dest" 2>/dev/null || true
rm -f "$dest"
log "removed $dest"
else
vlog "not installed: $dest"
fi
done
shopt -u nullglob
}
# ---- 2. remove symlinks from ~/.claude/{skills,commands,agents}/ -------------
unlink_kind() {
local kind="$1"
local src_root="$REPO_ROOT/$kind"
local dest_root="$CLAUDE_HOME/$kind"
[[ -d "$src_root" && -d "$dest_root" ]] || { vlog "nothing to unlink for $kind"; return 0; }
shopt -s nullglob
local entry name dest
for entry in "$src_root"/*; do
name="$(basename "$entry")"
dest="$dest_root/$name"
if [[ -L "$dest" ]]; then
local target
target="$(readlink "$dest" 2>/dev/null || true)"
if [[ "$target" == "$entry" ]]; then
rm -f "$dest"
log "unlinked $kind/$name"
else
vlog "$dest points elsewhere ($target); leaving"
fi
else
vlog "$dest is not a symlink; leaving"
fi
done
shopt -u nullglob
}
# ---- 3. restore most-recent settings.json backup -----------------------------
restore_settings() {
log "restore most-recent settings.json backup"
if [[ ! -d "$BACKUP_DIR_ROOT" ]]; then
vlog "no backup root; nothing to restore"
return 0
fi
local latest
# Pick the lexicographically greatest subdir with a settings.json inside.
latest="$(find "$BACKUP_DIR_ROOT" -mindepth 1 -maxdepth 1 -type d -name '20*' \
-exec test -f {}/settings.json \; -print 2>/dev/null \
| sort -r | head -n1)"
if [[ -z "$latest" ]]; then
warn "no viable settings.json backup found"
return 0
fi
cp "$latest/settings.json" "$SETTINGS_PATH.tmp.$$"
chmod 0600 "$SETTINGS_PATH.tmp.$$"
mv "$SETTINGS_PATH.tmp.$$" "$SETTINGS_PATH"
log "restored settings.json from $latest"
}
# ---- 4. optional: remove Claude-Memory link in vault -------------------------
remove_claude_memory_link() {
[[ "$HARD" -eq 1 ]] || { vlog "--hard not set; leaving Claude-Memory link"; return 0; }
if [[ -z "$VAULT" ]]; then
warn "--hard requires --vault PATH; skipping Claude-Memory removal"
return 0
fi
local dest="$VAULT/Claude-Memory"
if [[ -L "$dest" ]]; then
rm -f "$dest"
log "removed Claude-Memory symlink: $dest"
else
vlog "no Claude-Memory symlink at $dest"
fi
}
# ---- 5. optional: purge backups ---------------------------------------------
purge_backups() {
if [[ "$KEEP_BACKUPS" -eq 1 ]]; then
log "keeping backups (--keep-backups)"
return 0
fi
if [[ -d "$BACKUP_DIR_ROOT" ]]; then
rm -rf "$BACKUP_DIR_ROOT"
log "purged $BACKUP_DIR_ROOT"
fi
}
remove_shell_shortcuts() {
local cbrain_path="$HOME/.local/bin/cbrain"
local cbraintg_path="$HOME/.local/bin/cbraintg"
local removed=0
if [[ -f "$cbrain_path" ]]; then
rm -f "$cbrain_path"
log "removed $cbrain_path"
removed=1
fi
if [[ -f "$cbraintg_path" ]]; then
rm -f "$cbraintg_path"
log "removed $cbraintg_path"
removed=1
fi
[[ "$removed" -eq 0 ]] && vlog "no cbrain/cbraintg shortcuts to remove"
# Leave the ~/.local/bin PATH export in ~/.zshrc / ~/.bashrc — cli-maxxing
# Step 1 also relies on it for its ctg script, so removing it would break
# unrelated tooling.
}
remove_statusline_marker() {
local marker="$HOME/.claude/.mogging-vault"
if [[ -f "$marker" ]]; then
rm -f "$marker"
log "removed $marker"
else
vlog "statusline marker not present"
fi
}
# The third-party vault skill packs (step 10.75) are COPIED in, not symlinked,
# so unlink_kind() deliberately leaves them ("not a symlink; leaving") and they
# would otherwise orphan in ~/.claude/skills forever. Remove them here.
#
# Conservative on purpose: only delete a real directory. If the path is a
# symlink, some other tool (or a manual install) owns it — leave it alone.
remove_vault_skill_packs() {
local packs=(obsidian-markdown obsidian-bases obsidian-cli json-canvas defuddle)
local dest_root="$CLAUDE_HOME/skills"
local s dest removed=0
for s in "${packs[@]}"; do
dest="$dest_root/$s"
if [[ -L "$dest" ]]; then
vlog "$dest is a symlink (not ours); leaving"
elif [[ -d "$dest" ]]; then
rm -rf "${dest_root:?}/${s:?}"
log "removed skill pack $s"
removed=$((removed + 1))
else
vlog "skill pack $s not present"
fi
done
[[ "$removed" -eq 0 ]] && vlog "no vault skill packs to remove"
# The defuddle npm CLI is a SHARED global binary — the user may rely on it
# outside this vault. Never yank it silently; just say it's still there.
if command -v defuddle >/dev/null 2>&1; then
log "note: the 'defuddle' CLI is still installed globally (shared tool)."
log " remove it yourself if you want: npm uninstall -g defuddle"
fi
}
main() {
log "starting uninstall"
uninstall_launchd
unlink_kind "skills"
unlink_kind "commands"
unlink_kind "agents"
remove_vault_skill_packs
restore_settings
remove_claude_memory_link
remove_statusline_marker
remove_shell_shortcuts
purge_backups
log "done. Vault content untouched."
}
main "$@"