Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·420 lines (366 loc) · 11.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·420 lines (366 loc) · 11.9 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
#!/bin/bash
set -e
# Parse command line arguments
VERBOSE=false
UNINSTALL=false
CUSTOM_PROFILE_DIR=""
CUSTOM_CONFIG_DIR=""
SKIP_CONFIRM=false
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-u|--uninstall)
UNINSTALL=true
shift
;;
--profile-dir)
CUSTOM_PROFILE_DIR="$2"
shift 2
;;
--config-dir)
CUSTOM_CONFIG_DIR="$2"
shift 2
;;
-y|--yes)
SKIP_CONFIRM=true
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -v, --verbose Enable verbose output"
echo " -u, --uninstall Remove profile setup and restore backups"
echo " --profile-dir DIR Use custom profile directory (default: script_dir/profile)"
echo " --config-dir DIR Use custom config directory (default: script_dir/config)"
echo " -y, --yes Skip confirmation prompts (assume yes)"
echo " -h, --help Show this help message"
echo ""
echo "Environment variables:"
echo " PROFILE_ROOT Override profile directory"
echo " CONFIG_ROOT Override config directory"
echo " INSTALL_PREFIX Override installation prefix (default: \$HOME)"
echo ""
echo "This script only manages symlinks, backups, and the .zshrc entry point."
echo "Optional developer tools are installed separately:"
echo " ./scripts/bootstrap.sh"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Set up paths with priority: command line > environment > defaults
THIS=$(readlink -f "$0")
ROOT=$(dirname "$THIS")
# Installation prefix (where profile files will be installed)
INSTALL_PREFIX="${INSTALL_PREFIX:-$HOME}"
# Profile directory
if [ -n "$CUSTOM_PROFILE_DIR" ]; then
PROFILE="$CUSTOM_PROFILE_DIR"
elif [ -n "$PROFILE_ROOT" ]; then
PROFILE="$PROFILE_ROOT"
else
PROFILE="$ROOT/profile"
fi
# Config directory
if [ -n "$CUSTOM_CONFIG_DIR" ]; then
CONFIG="$CUSTOM_CONFIG_DIR"
elif [ -n "$CONFIG_ROOT" ]; then
CONFIG="$CONFIG_ROOT"
else
CONFIG="$ROOT/config"
fi
PAD=24
# Function to pad output text
pad_output() {
printf "%-${PAD}s" "$1"
}
# Function for verbose logging
log_verbose() {
if [ "$VERBOSE" = true ]; then
echo " [VERBOSE] $1"
fi
}
# Function to backup existing files
backup_file() {
local file="$1"
if [ -e "$file" ] && [ ! -L "$file" ]; then
local backup
backup="${file}.backup.$(date +%Y%m%d_%H%M%S)"
log_verbose "Creating backup: $file -> $backup"
cp -r "$file" "$backup"
echo " backed up to: $backup"
fi
}
# Function to restore latest backup
restore_backup() {
local file="$1"
local latest_backup=""
local backup
for backup in "${file}.backup."*; do
if [ -e "$backup" ]; then
latest_backup="$backup"
fi
done
if [ -n "$latest_backup" ]; then
log_verbose "Restoring backup: $latest_backup -> $file"
rm -rf "$file"
mv "$latest_backup" "$file"
echo " restored from: $latest_backup"
return 0
fi
return 1
}
# Function to remove symlink if it exists
remove_symlink() {
local file="$1"
if [ -L "$file" ]; then
log_verbose "Removing symlink: $file"
rm "$file"
echo " removed symlink: $file"
fi
}
# Function to create symlink safely
create_symlink() {
local source="$1"
local target="$2"
local target_name="$3"
if [ -L "$target" ]; then
local current_target
current_target=$(readlink "$target")
if [ "$current_target" = "$source" ]; then
log_verbose "Symlink already correct: $target -> $source"
echo " already linked correctly"
return 0
else
log_verbose "Updating symlink: $target ($current_target -> $source)"
rm "$target"
fi
elif [ -e "$target" ]; then
log_verbose "Replacing existing regular file/directory: $target"
rm -rf "$target"
fi
log_verbose "Creating symlink: $source -> $target"
ln -s "$source" "$target"
echo " created: $target_name"
}
# Function to check whether .zshrc is managed by this installer
is_managed_zshrc() {
local file="$1"
[ -f "$file" ] || return 1
if grep -q "^# managed-by: utility-scripts-install$" "$file"; then
return 0
fi
# Backward compatibility with the previous unmanaged template
if grep -q '^export PROFILE=' "$file" \
&& grep -q "^source \\\$PROFILE/zshrc.sh$" "$file"; then
return 0
fi
return 1
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check dependencies
check_dependencies() {
local missing_deps=()
if ! command_exists git; then
missing_deps+=("git")
fi
if ! command_exists curl; then
missing_deps+=("curl")
fi
if [ ${#missing_deps[@]} -gt 0 ]; then
echo "Error: Missing required dependencies: ${missing_deps[*]}"
echo "Please install the missing tools and try again."
echo ""
echo "On Ubuntu/Debian: sudo apt-get install ${missing_deps[*]}"
echo "On CentOS/RHEL: sudo yum install ${missing_deps[*]}"
echo "On macOS: brew install ${missing_deps[*]}"
exit 1
fi
log_verbose "All dependencies available: git, curl"
}
# Function to ask for confirmation
confirm() {
local message="$1"
if [ "$SKIP_CONFIRM" = true ]; then
log_verbose "Skipping confirmation: $message"
return 0
fi
echo -n "$message (y/N): "
read -r response
case "$response" in
[yY]|[yY][eE][sS])
return 0
;;
*)
return 1
;;
esac
}
# Function to validate paths exist
validate_paths() {
if [ ! -d "$PROFILE" ]; then
echo "Error: Profile directory does not exist: $PROFILE"
exit 1
fi
if [ ! -d "$CONFIG" ]; then
echo "Error: Config directory does not exist: $CONFIG"
exit 1
fi
log_verbose "Path validation successful"
}
# Uninstall function
uninstall() {
echo "Uninstalling profile setup..."
log_verbose "Starting uninstall process"
if ! confirm "This will remove dotfile symlinks and restore backups. Continue?"; then
echo "Uninstall cancelled."
exit 0
fi
OUTPUT="removing dircolors"
echo -n "$(pad_output "$OUTPUT"):"
remove_symlink "$INSTALL_PREFIX/.dircolors"
restore_backup "$INSTALL_PREFIX/.dircolors" || true
echo " Done"
OUTPUT="removing vim setup"
echo -n "$(pad_output "$OUTPUT"):"
remove_symlink "$INSTALL_PREFIX/.vimrc"
remove_symlink "$INSTALL_PREFIX/.vim"
restore_backup "$INSTALL_PREFIX/.vimrc" || true
restore_backup "$INSTALL_PREFIX/.vim" || true
echo " Done"
OUTPUT="removing zshrc"
echo -n "$(pad_output "$OUTPUT"):"
remove_symlink "$INSTALL_PREFIX/.zshrc"
if ! restore_backup "$INSTALL_PREFIX/.zshrc"; then
if is_managed_zshrc "$INSTALL_PREFIX/.zshrc"; then
rm -f "$INSTALL_PREFIX/.zshrc"
echo " removed managed file: $INSTALL_PREFIX/.zshrc"
fi
fi
echo " Done"
echo "Uninstall complete. Symlinks removed and backups restored."
echo "Optional tool installations are managed by ./scripts/bootstrap.sh and were left intact."
}
# ---------------------------------------------------------------------------
# Handle uninstall mode
if [ "$UNINSTALL" = true ]; then
uninstall
exit 0
fi
echo "Setting up utility scripts ..."
log_verbose "Verbose mode enabled"
log_verbose "Script location: $THIS"
# Check dependencies before proceeding
check_dependencies
# Validate paths exist
validate_paths
OUTPUT="source directory"
echo "$(pad_output "$OUTPUT"): $ROOT"
OUTPUT="profile directory"
echo "$(pad_output "$OUTPUT"): $PROFILE"
OUTPUT="config directory"
echo "$(pad_output "$OUTPUT"): $CONFIG"
OUTPUT="destination"
echo "$(pad_output "$OUTPUT"): $INSTALL_PREFIX"
# Ask for confirmation before proceeding
echo ""
if ! confirm "Proceed with profile installation?"; then
echo "Installation cancelled."
exit 0
fi
# ---------------------------------------------------------------------------
OUTPUT="setting up dircolors"
echo -n "$(pad_output "$OUTPUT"):"
backup_file "$INSTALL_PREFIX/.dircolors"
create_symlink "$CONFIG/dircolors" "$INSTALL_PREFIX/.dircolors" ".dircolors"
# ---------------------------------------------------------------------------
OUTPUT="setting up vim"
echo -n "$(pad_output "$OUTPUT"):"
backup_file "$INSTALL_PREFIX/.vimrc"
backup_file "$INSTALL_PREFIX/.vim"
create_symlink "$PROFILE/vimrc" "$INSTALL_PREFIX/.vimrc" ".vimrc"
create_symlink "$PROFILE/vim" "$INSTALL_PREFIX/.vim" ".vim"
echo ""
# ---------------------------------------------------------------------------
COLORSCHEME_REPO="https://github.com/rafi/awesome-vim-colorschemes.git"
COLORSCHEME_DIR="$PROFILE/vim/awesome-vim-colorschemes"
COLORSCHEME_COLORS_LINK="$PROFILE/vim/colors"
OUTPUT="initializing vim themes"
echo -n "$(pad_output "$OUTPUT"):"
if [ -d "$COLORSCHEME_DIR/.git" ]; then
log_verbose "awesome-vim-colorschemes already present"
echo " exists"
else
log_verbose "Cloning awesome-vim-colorschemes from $COLORSCHEME_REPO"
if [ "$VERBOSE" = true ]; then
git clone "$COLORSCHEME_REPO" "$COLORSCHEME_DIR"
else
git clone -q "$COLORSCHEME_REPO" "$COLORSCHEME_DIR"
fi
echo " Done"
fi
OUTPUT="linking vim colors"
echo -n "$(pad_output "$OUTPUT"):"
if [ -L "$COLORSCHEME_COLORS_LINK" ] && [ "$(readlink "$COLORSCHEME_COLORS_LINK")" = "$COLORSCHEME_DIR/colors" ]; then
log_verbose "vim/colors symlink already correct"
echo " exists"
elif [ -e "$COLORSCHEME_COLORS_LINK" ] && [ ! -L "$COLORSCHEME_COLORS_LINK" ]; then
log_verbose "vim/colors exists as real directory, skipping symlink"
echo " exists (real dir)"
else
ln -sf "$COLORSCHEME_DIR/colors" "$COLORSCHEME_COLORS_LINK"
log_verbose "Created symlink: vim/colors -> awesome-vim-colorschemes/colors"
echo " Done"
fi
# ---------------------------------------------------------------------------
OUTPUT="writing"
FILE="$INSTALL_PREFIX/.zshrc"
echo "$(pad_output "$OUTPUT"): $FILE"
backup_file "$FILE"
log_verbose "Writing new .zshrc configuration"
cat > "$FILE" <<- EOD
# managed-by: utility-scripts-install
export PROFILE="$PROFILE"
source \$PROFILE/zshrc.sh
EOD
OUTPUT=""
echo "Installation complete!"
echo "--------------------------------------------------------------------------------"
# Offer to source the new configuration
if [ -f "$INSTALL_PREFIX/.zshrc" ]; then
echo "Your new shell configuration is ready."
echo ""
if confirm "Source the new .zshrc configuration now?"; then
if [ -n "$ZSH_VERSION" ]; then
# We're already in zsh, source the config
log_verbose "Sourcing new .zshrc configuration"
source "$INSTALL_PREFIX/.zshrc"
echo "Configuration loaded successfully!"
else
# We're in a different shell, start zsh
log_verbose "Starting new zsh session"
echo "Starting new zsh session..."
exec zsh
fi
else
echo "To use your new configuration:"
echo " source $INSTALL_PREFIX/.zshrc"
echo " # OR restart your terminal"
fi
else
echo "Note: .zshrc was not created. Please restart your terminal to use the new profile setup."
fi
echo ""
echo "Optional developer tools are not installed by this script."
echo "To install them (oh-my-zsh, mise, node, gh, claude, cargo, platformio, opencode):"
echo " ./scripts/bootstrap.sh"