-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-scripts.sh
More file actions
executable file
Β·216 lines (182 loc) Β· 6.8 KB
/
Copy pathinstall-scripts.sh
File metadata and controls
executable file
Β·216 lines (182 loc) Β· 6.8 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
#!/bin/zsh
################################################################################
# Usage Instructions
################################################################################
#
# Run this script:
# `./install-scripts.sh`
#
# This script creates symlinks in ~/.local/bin for scripts you want accessible
# from anywhere on your PATH. It is idempotent & safe to run multiple times.
#
# On first run, you will be prompted to specify your personal code directory
# (e.g., ~/code/personal or ~/code). This is saved to local/symlinks-config.sh
# & reused on subsequent runs.
#
################################################################################
# Resolve the mac-setup repo directory (even when invoked via symlink)
MAC_SETUP_DIR="${0:A:h}"
source "$MAC_SETUP_DIR/utils/style.sh"
# Added to PATH in ~/.zshrc
SYMLINK_DIR="$HOME/.local/bin"
################################################################################
# Configuration
################################################################################
CONFIG_FILE="$MAC_SETUP_DIR/local/symlinks-config.sh"
# Source existing config if it exists
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
# If PERSONAL_CODE_DIR is not set, prompt for it
if [ -z "$PERSONAL_CODE_DIR" ]; then
echo
echo $BOLD"First-time setup: Please specify your personal code directory."$END
echo $DIM"This is the directory this script will use as reference when"
echo "creating symlinks (e.g., ~/code/personal or ~/code)."$END
echo
echo -n $BOLD"Personal code directory: "$END
read personal_code_dir
# Expand ~ to $HOME
personal_code_dir="${personal_code_dir/#\~/$HOME}"
# Validate the directory exists
if [ ! -d "$personal_code_dir" ]; then
echo
echo $ICON_ERROR$BOLD$RED" Directory does not exist: $personal_code_dir"$END
exit 1
fi
PERSONAL_CODE_DIR="$personal_code_dir"
# Persist the config
echo "# Personal code directory (auto-generated by install-scripts.sh)" >"$CONFIG_FILE"
echo "PERSONAL_CODE_DIR=\"$PERSONAL_CODE_DIR\"" >>"$CONFIG_FILE"
echo
echo $ICON_CHECK$BOLD" Saved config to "$CYAN"local/symlinks-config.sh"$END
echo
echo $BOLD_SEPARATOR
fi
# Validate PERSONAL_CODE_DIR exists
if [ ! -d "$PERSONAL_CODE_DIR" ]; then
echo
echo $ICON_ERROR$BOLD$RED" Personal code directory does not exist: $PERSONAL_CODE_DIR"$END
echo $BOLD"Update or delete "$CYAN"local/symlinks-config.sh"$END$BOLD" & re-run."$END
exit 1
fi
################################################################################
# Symlink definitions
################################################################################
# Each entry is a full path to the script to symlink into ~/.local/bin.
# Use $PERSONAL_CODE_DIR for paths relative to your personal code directory.
# The symlink name will match the original filename.
symlinks=(
"$PERSONAL_CODE_DIR/dotfiles/install-dotfiles.sh"
"$PERSONAL_CODE_DIR/gists/utils/check-repos.sh"
"$PERSONAL_CODE_DIR/gists/utils/stay-awake.sh"
"$PERSONAL_CODE_DIR/mac-setup/brew-installs.sh"
"$PERSONAL_CODE_DIR/mac-setup/install-scripts.sh"
"$PERSONAL_CODE_DIR/strava/strava-sync.sh"
)
################################################################################
# Create symlink directory
################################################################################
if [ ! -d "$SYMLINK_DIR" ]; then
mkdir -p "$SYMLINK_DIR"
if [ $? -ne 0 ]; then
echo
echo $ICON_ERROR$BOLD$RED" Failed to create symlink directory: $SYMLINK_DIR"$END
exit 1
fi
echo
echo $ICON_ARROW$BOLD" Created "$CYAN"$SYMLINK_DIR"$END
fi
################################################################################
# Symlink creation
################################################################################
count_created=0
count_skipped=0
count_updated=0
count_errors=0
create_symlink() {
local source_path="$1"
local symlink_name="$(basename "$source_path")"
local symlink_path="$SYMLINK_DIR/$symlink_name"
# Check if source file exists
if [ ! -f "$source_path" ]; then
echo $TAB$ICON_ERROR$RED" $symlink_name"$END$DIM" β source not found: $source_path"$END
((count_errors++))
return
fi
# Check if symlink already exists &a points to the correct target
if [ -L "$symlink_path" ]; then
local current_target="$(readlink "$symlink_path")"
if [ "$current_target" = "$source_path" ]; then
echo $TAB$ICON_CHECK" $symlink_name"$DIM" β already linked"$END
((count_skipped++))
return
else
echo $TAB$ICON_WARN$YELLOW" $symlink_name"$END$DIM" β updated (was: $current_target)"$END
ln -sf "$source_path" "$symlink_path"
((count_updated++))
return
fi
elif [ -e "$symlink_path" ]; then
echo $TAB$ICON_WARN$YELLOW" $symlink_name"$END$DIM" β replaced existing file"$END
ln -sf "$source_path" "$symlink_path"
((count_updated++))
return
fi
# Create new symlink
ln -s "$source_path" "$symlink_path"
echo $TAB$ICON_ARROW" $symlink_name"$DIM" β linked"$END
((count_created++))
}
echo
echo $GREEN$BOLD"Creating symlinks..."$END
echo $DIM"Symlink directory: $SYMLINK_DIR"$END
echo $DIM"Personal code directory: $PERSONAL_CODE_DIR"$END
echo
for entry in "${symlinks[@]}"; do
create_symlink "$entry"
done
################################################################################
# Cleanup dangling symlinks
################################################################################
count_cleaned=0
echo
echo $LIGHT_SEPARATOR
echo
echo $BOLD"Checking for dangling symlinks..."$END
echo
for symlink in "$SYMLINK_DIR"/*; do
# Skip if not a symlink
[ -L "$symlink" ] || continue
# Check if the symlink target no longer exists
if [ ! -e "$symlink" ]; then
dangling_name="$(basename "$symlink")"
dangling_target="$(readlink "$symlink")"
rm "$symlink"
echo $TAB$ICON_WARN$YELLOW" $dangling_name"$END$DIM" β removed (target gone: $dangling_target)"$END
((count_cleaned++))
fi
done
if [ $count_cleaned -eq 0 ]; then
echo $TAB$ICON_CHECK$DIM" No dangling symlinks found."$END
fi
################################################################################
# Summary
################################################################################
echo
echo $LIGHT_SEPARATOR
echo
echo $ICON_CHECK$BOLD" Done!"$END
echo $TAB$GREEN"Created: $count_created"$END
if [ $count_updated -gt 0 ]; then
echo $TAB$YELLOW"Updated: $count_updated"$END
fi
if [ $count_cleaned -gt 0 ]; then
echo $TAB$YELLOW"Cleaned: $count_cleaned"$END
fi
echo $TAB$DIM"Skipped: $count_skipped"$END
if [ $count_errors -gt 0 ]; then
echo $TAB$RED"Errors: $count_errors"$END
fi
echo
exit 0