Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·619 lines (526 loc) · 18.3 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·619 lines (526 loc) · 18.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/bin/bash
set -e
# Claude Dotfiles Template Setup Script
# Processes .template files and creates personalized config
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
DRY_RUN=false
FORCE=false
NON_INTERACTIVE=false
MERGE_MODE=false
FRESH_MODE=false
TARGET_DIR=""
# Configuration variables
GIT_NAME=""
SSH_KEY_PATH=""
CLAUDE_DIR=""
OBSIDIAN_VAULT_PATH=""
GITHUB_ORG=""
TWITTER_HANDLE=""
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Options:
-n, --non-interactive Use environment variables or defaults (no prompts)
-f, --force Overwrite existing files without asking
-d, --dry-run Show what would be done without making changes
-m, --merge Merge with existing config (add new, preserve existing)
--fresh Backup existing ~/.claude and replace entirely
-t, --target DIR Target directory (default: ~/.claude in merge mode)
-h, --help Show this help message
Modes:
Fresh install (default):
Run from cloned repo at ~/.claude, processes templates in place
Merge install (--merge):
Run from any location, merge skills/agents into existing ~/.claude
- Adds new skills, agents, scripts that don't exist
- Preserves all existing user files
- Prompts before overwriting config files
Fresh replace (--fresh):
Replace existing ~/.claude entirely
- Backs up existing config to ~/.claude.backup.{timestamp}
- Copies template to ~/.claude
- Processes templates in place
Environment variables (for non-interactive mode):
GIT_NAME Git commit author name (default: your git user.name)
SSH_KEY_PATH Path to SSH key for signing (default: ~/.ssh/id_ed25519)
CLAUDE_DIR Path to Claude config dir (default: current directory)
OBSIDIAN_VAULT_PATH Path to Obsidian vault (default: ~/obsidian)
GITHUB_ORG GitHub organization for dashboard (default: none)
TWITTER_HANDLE Twitter handle for ghostwriting (default: none)
Examples:
# Fresh install (clone to ~/.claude first)
./setup.sh
# Merge into existing config
./setup.sh --merge
# Merge to custom location
./setup.sh --merge --target /path/to/config
# Non-interactive merge
./setup.sh --merge --non-interactive
EOF
exit 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-n|--non-interactive)
NON_INTERACTIVE=true
shift
;;
-f|--force)
FORCE=true
shift
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-m|--merge)
MERGE_MODE=true
shift
;;
--fresh)
FRESH_MODE=true
shift
;;
-t|--target)
if [[ -z "$2" ]] || [[ "$2" == -* ]]; then
echo -e "${RED}Error: --target requires a directory path${NC}"
exit 1
fi
TARGET_DIR="$2"
shift 2
;;
-h|--help)
usage
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
;;
esac
done
# Detect existing config when running from outside ~/.claude (curl-pipe-bash scenario)
if [[ "$MERGE_MODE" != "true" ]] && [[ "$FRESH_MODE" != "true" ]] && [[ "$SCRIPT_DIR" != "$HOME/.claude" ]]; then
if [[ -f "$HOME/.claude/CLAUDE.md" ]] || [[ -d "$HOME/.claude/skills" ]]; then
echo ""
echo -e "${YELLOW}Existing Claude config detected at ~/.claude${NC}"
echo ""
echo "Choose how to proceed:"
echo " ./setup.sh --merge Add new skills/agents, preserve your existing config"
echo " ./setup.sh --fresh Replace ~/.claude entirely (backs up existing)"
echo ""
exit 1
fi
fi
# Set default target directory for merge mode
if [[ "$MERGE_MODE" == "true" ]] && [[ -z "$TARGET_DIR" ]]; then
TARGET_DIR="$HOME/.claude"
fi
# Prompt for value with default
prompt_value() {
local varname="$1"
local prompt="$2"
local default="$3"
local current_value="${!varname}"
if [[ -n "$current_value" ]]; then
# Already set via environment
echo -e "${BLUE}$prompt:${NC} $current_value (from env)"
return
fi
if [[ "$NON_INTERACTIVE" == "true" ]]; then
eval "$varname='$default'"
echo -e "${BLUE}$prompt:${NC} $default (default)"
return
fi
local input
if [[ -n "$default" ]]; then
read -p "$(echo -e "${BLUE}$prompt${NC} [$default]: ")" input
eval "$varname='${input:-$default}'"
else
read -p "$(echo -e "${BLUE}$prompt${NC}: ")" input
eval "$varname='$input'"
fi
}
# Check if file exists and handle accordingly
should_write_file() {
local file="$1"
if [[ ! -f "$file" ]]; then
return 0 # File doesn't exist, ok to write
fi
if [[ "$FORCE" == "true" ]]; then
return 0 # Force mode, ok to overwrite
fi
if [[ "$NON_INTERACTIVE" == "true" ]]; then
echo -e "${YELLOW} Skipping:${NC} $file (exists, use --force to overwrite)"
return 1
fi
local response
read -p "$(echo -e "${YELLOW}$file exists. Overwrite? [y/N]:${NC} ")" response
case "$response" in
[yY][eE][sS]|[yY])
return 0
;;
*)
echo -e "${YELLOW} Skipping:${NC} $file"
return 1
;;
esac
}
# Process a template file
process_template() {
local template="$1"
local output="${template%.template}"
if ! should_write_file "$output"; then
return
fi
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${GREEN} Would create:${NC} $output"
return
fi
mkdir -p "$(dirname "$output")"
# Use envsubst to process template
export GIT_NAME SSH_KEY_PATH CLAUDE_DIR OBSIDIAN_VAULT_PATH GITHUB_ORG TWITTER_HANDLE
envsubst < "$template" > "$output"
echo -e "${GREEN} Created:${NC} $output"
}
# Detect and display existing configuration
detect_existing_config() {
local target="$1"
echo ""
echo -e "${YELLOW}Existing configuration detected at $target${NC}"
echo ""
# Show what exists
[[ -f "$target/CLAUDE.md" ]] && echo " - CLAUDE.md (global instructions)"
[[ -f "$target/settings.json" ]] && echo " - settings.json (CLI settings)"
if [[ -d "$target/skills" ]]; then
local skill_count=$(ls -1 "$target/skills" 2>/dev/null | wc -l | tr -d ' ')
echo " - skills/ ($skill_count skills)"
fi
if [[ -d "$target/agents" ]]; then
local agent_count=$(ls -1 "$target/agents" 2>/dev/null | wc -l | tr -d ' ')
echo " - agents/ ($agent_count agents)"
fi
[[ -d "$target/scripts" ]] && echo " - scripts/"
[[ -f "$target/config/paths.env" ]] && echo " - config/paths.env"
echo ""
}
# Merge a directory: add new items, preserve existing
merge_directory() {
local src_dir="$1"
local dest_dir="$2"
local dir_name="$3"
if [[ ! -d "$src_dir" ]]; then
return
fi
mkdir -p "$dest_dir"
local added=0
local skipped=0
for item in "$src_dir"/*; do
[[ ! -e "$item" ]] && continue
local name=$(basename "$item")
local dest_item="$dest_dir/$name"
if [[ -e "$dest_item" ]]; then
((skipped++))
else
if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${GREEN}Would add:${NC} $dir_name/$name"
else
cp -r "$item" "$dest_item"
echo -e " ${GREEN}Added:${NC} $dir_name/$name"
fi
((added++))
fi
done
if [[ $added -gt 0 ]] || [[ $skipped -gt 0 ]]; then
echo -e " ${BLUE}$dir_name:${NC} $added added, $skipped existing (preserved)"
fi
}
# Handle config file merge with user prompts
merge_config_file() {
local src="$1"
local dest="$2"
local name="$3"
if [[ ! -f "$dest" ]]; then
# No existing file - proceed normally
return 0
fi
if [[ "$FORCE" == "true" ]]; then
# Backup then overwrite
cp "$dest" "$dest.backup.$(date +%Y%m%d-%H%M%S)"
echo -e " ${YELLOW}Backed up:${NC} $name -> $name.backup.*"
return 0
fi
if [[ "$NON_INTERACTIVE" == "true" ]]; then
echo -e " ${YELLOW}Skipped:${NC} $name (exists, use --force to overwrite)"
return 1
fi
echo ""
echo -e "${YELLOW}$name exists.${NC}"
echo " 1) Skip (keep existing)"
echo " 2) Backup and replace"
echo " 3) View diff"
read -p "Choice [1]: " choice
case "$choice" in
2)
cp "$dest" "$dest.backup.$(date +%Y%m%d-%H%M%S)"
echo -e " ${YELLOW}Backed up:${NC} $name"
return 0
;;
3)
echo ""
echo -e "${BLUE}--- Existing file ---${NC}"
diff --color=auto "$dest" "$src" 2>/dev/null || true
echo ""
merge_config_file "$src" "$dest" "$name" # Re-prompt
return $?
;;
*)
echo -e " ${YELLOW}Skipped:${NC} $name"
return 1
;;
esac
}
# Merge paths.env: preserve existing values, add new variables
merge_paths_env() {
local src="$1"
local dest="$2"
if [[ ! -f "$dest" ]]; then
# No existing file - will be created normally
return 0
fi
if [[ "$FORCE" == "true" ]]; then
cp "$dest" "$dest.backup.$(date +%Y%m%d-%H%M%S)"
echo -e " ${YELLOW}Backed up:${NC} paths.env -> paths.env.backup.*"
return 0
fi
# In merge mode without force, preserve existing paths.env completely
echo -e " ${YELLOW}Preserved:${NC} config/paths.env (existing values kept)"
return 1
}
# Process template for merge mode (to target directory)
process_template_to_target() {
local template="$1"
local target_dir="$2"
# Get relative path from SCRIPT_DIR
local rel_path="${template#$SCRIPT_DIR/}"
local output="$target_dir/${rel_path%.template}"
if ! should_write_file "$output"; then
return
fi
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${GREEN} Would create:${NC} $output"
return
fi
mkdir -p "$(dirname "$output")"
# Use envsubst to process template
export GIT_NAME SSH_KEY_PATH CLAUDE_DIR OBSIDIAN_VAULT_PATH GITHUB_ORG TWITTER_HANDLE
envsubst < "$template" > "$output"
echo -e "${GREEN} Created:${NC} $output"
}
echo ""
echo -e "${GREEN}Claude Dotfiles Template Setup${NC}"
echo "================================"
echo ""
# Merge mode validation
if [[ "$MERGE_MODE" == "true" ]]; then
# Validate we're not trying to merge into ourselves
if [[ "$(cd "$SCRIPT_DIR" && pwd)" == "$(cd "$TARGET_DIR" 2>/dev/null && pwd)" ]]; then
echo -e "${RED}Error: Cannot merge into self.${NC}"
echo "Run from a cloned template directory, not from the target."
exit 1
fi
echo -e "${BLUE}Mode:${NC} Merge install"
echo -e "${BLUE}Source:${NC} $SCRIPT_DIR"
echo -e "${BLUE}Target:${NC} $TARGET_DIR"
# Detect and show existing config
if [[ -d "$TARGET_DIR" ]]; then
detect_existing_config "$TARGET_DIR"
else
echo ""
echo -e "${BLUE}No existing config at $TARGET_DIR - will create fresh.${NC}"
echo ""
fi
fi
# Fresh mode: backup existing and replace
if [[ "$FRESH_MODE" == "true" ]]; then
echo -e "${BLUE}Mode:${NC} Fresh replace"
echo -e "${BLUE}Source:${NC} $SCRIPT_DIR"
echo -e "${BLUE}Target:${NC} $HOME/.claude"
echo ""
# Backup existing config if present
if [[ -d "$HOME/.claude" ]]; then
BACKUP_DIR="$HOME/.claude.backup.$(date +%Y%m%d-%H%M%S)"
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}Would backup:${NC} ~/.claude -> $BACKUP_DIR"
else
echo -e "${YELLOW}Backing up:${NC} ~/.claude -> $BACKUP_DIR"
mv "$HOME/.claude" "$BACKUP_DIR"
fi
echo ""
fi
# Copy template to ~/.claude
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}Would copy:${NC} $SCRIPT_DIR -> ~/.claude"
else
echo -e "${GREEN}Copying:${NC} $SCRIPT_DIR -> ~/.claude"
cp -r "$SCRIPT_DIR" "$HOME/.claude"
# Update SCRIPT_DIR to point to new location for template processing
SCRIPT_DIR="$HOME/.claude"
fi
echo ""
fi
# Gather configuration values
echo -e "${YELLOW}Configuration${NC}"
echo ""
# Get defaults
default_git_name=$(git config --global user.name 2>/dev/null || echo "")
default_ssh_key="$HOME/.ssh/id_ed25519"
if [[ "$MERGE_MODE" == "true" ]]; then
default_claude_dir="$TARGET_DIR"
elif [[ "$FRESH_MODE" == "true" ]]; then
default_claude_dir="$HOME/.claude"
else
default_claude_dir="$SCRIPT_DIR"
fi
default_obsidian="$HOME/obsidian"
prompt_value GIT_NAME "Git commit author name" "$default_git_name"
prompt_value SSH_KEY_PATH "SSH key path for signing" "$default_ssh_key"
prompt_value CLAUDE_DIR "Claude config directory" "$default_claude_dir"
prompt_value OBSIDIAN_VAULT_PATH "Obsidian vault path" "$default_obsidian"
prompt_value GITHUB_ORG "GitHub organization (for dashboard)" ""
prompt_value TWITTER_HANDLE "Twitter handle (for ghostwriting)" ""
echo ""
# Validate SSH key exists
if [[ ! -f "$SSH_KEY_PATH" ]] && [[ "$SSH_KEY_PATH" != *'$'* ]]; then
echo -e "${YELLOW}Warning:${NC} SSH key not found at $SSH_KEY_PATH"
echo "You may need to create one or update the path in generated files."
echo ""
fi
# Validate Obsidian vault
if [[ ! -d "$OBSIDIAN_VAULT_PATH" ]] && [[ "$OBSIDIAN_VAULT_PATH" != *'$'* ]]; then
echo -e "${YELLOW}Note:${NC} Obsidian vault not found at $OBSIDIAN_VAULT_PATH"
echo "Run ./setup-obsidian.sh after setup to create the vault structure."
echo ""
fi
# Show summary
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}DRY RUN - No changes will be made${NC}"
echo ""
fi
# Branch based on mode
if [[ "$MERGE_MODE" == "true" ]]; then
# === MERGE MODE ===
# Merge directories (add new, preserve existing)
echo -e "${YELLOW}Merging directories...${NC}"
merge_directory "$SCRIPT_DIR/skills" "$TARGET_DIR/skills" "skills"
merge_directory "$SCRIPT_DIR/agents" "$TARGET_DIR/agents" "agents"
merge_directory "$SCRIPT_DIR/scripts" "$TARGET_DIR/scripts" "scripts"
merge_directory "$SCRIPT_DIR/voice-profiles" "$TARGET_DIR/voice-profiles" "voice-profiles"
echo ""
# Handle config files with merge logic
echo -e "${YELLOW}Processing config files...${NC}"
# Ensure target config directory exists
mkdir -p "$TARGET_DIR/config"
# CLAUDE.md
if merge_config_file "$SCRIPT_DIR/CLAUDE.md.template" "$TARGET_DIR/CLAUDE.md" "CLAUDE.md"; then
process_template_to_target "$SCRIPT_DIR/CLAUDE.md.template" "$TARGET_DIR"
fi
# settings.json
if merge_config_file "$SCRIPT_DIR/settings.json.template" "$TARGET_DIR/settings.json" "settings.json"; then
process_template_to_target "$SCRIPT_DIR/settings.json.template" "$TARGET_DIR"
fi
# paths.env
if merge_paths_env "$SCRIPT_DIR/config/paths.env.template" "$TARGET_DIR/config/paths.env"; then
if [[ "$DRY_RUN" != "true" ]]; then
cat > "$TARGET_DIR/config/paths.env" << EOF
# Generated by setup.sh - paths for Claude skills
# Edit these values to match your system
CLAUDE_DIR="$CLAUDE_DIR"
OBSIDIAN_VAULT_PATH="$OBSIDIAN_VAULT_PATH"
SSH_KEY_PATH="$SSH_KEY_PATH"
GITHUB_ORG="$GITHUB_ORG"
TWITTER_HANDLE="$TWITTER_HANDLE"
EOF
echo -e "${GREEN} Created:${NC} config/paths.env"
else
echo -e "${GREEN} Would create:${NC} config/paths.env"
fi
fi
# Process remaining templates (skill templates, etc.)
echo ""
echo -e "${YELLOW}Processing skill templates...${NC}"
find "$SCRIPT_DIR" -name "*.template" -type f | while read template; do
# Skip top-level config templates (already handled)
case "$template" in
"$SCRIPT_DIR/CLAUDE.md.template"|"$SCRIPT_DIR/settings.json.template"|"$SCRIPT_DIR/config/paths.env.template")
continue
;;
esac
process_template_to_target "$template" "$TARGET_DIR"
done
echo ""
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}Dry run complete. Run without --dry-run to apply changes.${NC}"
else
echo -e "${GREEN}Merge complete!${NC}"
echo ""
echo "Summary:"
echo " - New skills/agents added to $TARGET_DIR"
echo " - Existing files preserved"
echo ""
echo "Next steps:"
echo " 1. Review merged files in $TARGET_DIR"
if [[ ! -d "$OBSIDIAN_VAULT_PATH" ]]; then
echo " 2. Run ./setup-obsidian.sh to create Obsidian vault structure"
fi
echo " 3. Start Claude Code to test your configuration"
fi
else
# === FRESH INSTALL MODE (original behavior) ===
# Process template files
echo -e "${YELLOW}Processing templates...${NC}"
find "$SCRIPT_DIR" -name "*.template" -type f | while read template; do
process_template "$template"
done
echo ""
# Create paths.env if config directory exists
if [[ -d "$SCRIPT_DIR/config" ]]; then
paths_env="$SCRIPT_DIR/config/paths.env"
if should_write_file "$paths_env"; then
if [[ "$DRY_RUN" != "true" ]]; then
cat > "$paths_env" << EOF
# Generated by setup.sh - paths for Claude skills
# Edit these values to match your system
CLAUDE_DIR="$CLAUDE_DIR"
OBSIDIAN_VAULT_PATH="$OBSIDIAN_VAULT_PATH"
SSH_KEY_PATH="$SSH_KEY_PATH"
GITHUB_ORG="$GITHUB_ORG"
TWITTER_HANDLE="$TWITTER_HANDLE"
EOF
echo -e "${GREEN} Created:${NC} config/paths.env"
else
echo -e "${GREEN} Would create:${NC} config/paths.env"
fi
fi
fi
echo ""
if [[ "$DRY_RUN" == "true" ]]; then
echo -e "${YELLOW}Dry run complete. Run without --dry-run to apply changes.${NC}"
else
echo -e "${GREEN}Setup complete!${NC}"
echo ""
echo "Next steps:"
echo " 1. Review generated files in $SCRIPT_DIR"
if [[ ! -d "$OBSIDIAN_VAULT_PATH" ]]; then
echo " 2. Run ./setup-obsidian.sh to create Obsidian vault structure"
fi
echo " 3. Start Claude Code to test your configuration"
fi
fi