-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·373 lines (319 loc) · 7.75 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·373 lines (319 loc) · 7.75 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$SCRIPT_DIR"
DEFAULT_INSTALL_DIR="$PROJECT_ROOT"
INSTALL_DIR="$DEFAULT_INSTALL_DIR"
BIN_DIR="/usr/local/bin"
STATE_DIR="${HOME}/.local/state/macos-scripts"
STATE_FILE="${STATE_DIR}/install-state.env"
MANAGED_BEGIN="# >>> macos-scripts >>>"
MANAGED_END="# <<< macos-scripts <<<"
DRY_RUN=0
UNINSTALL=0
AUTO_YES=0
LAUNCHER_REL="terminal/launchers/mqlaunch.sh"
ONBOARDING_REL="tools/onboarding.sh"
TARGET_LINK_NAME="mqlaunch"
# Handles log.
log() { printf "\033[1;34m[INFO]\033[0m %s\n" "$*"; }
# Handles ok.
ok() { printf "\033[1;32m[ OK ]\033[0m %s\n" "$*"; }
# Handles warn.
warn() { printf "\033[1;33m[WARN]\033[0m %s\n" "$*" >&2; }
# Handles err.
err() { printf "\033[1;31m[ERR ]\033[0m %s\n" "$*" >&2; }
# Handles die.
die() { err "$*"; exit 1; }
# Prints usage information.
usage() {
cat <<EOF
macos-scripts installer
Usage:
./install.sh [options]
Options:
--install-dir PATH Set repo/install directory reference (default: current repo)
--bin-dir PATH Install symlink into this bin dir (default: /usr/local/bin)
--dry-run Show actions without changing anything
--uninstall Remove symlink and managed shell block
--yes Skip confirmation prompts
-h, --help Show help
Examples:
./install.sh
./install.sh --dry-run
./install.sh --install-dir "\$HOME/macos-scripts"
./install.sh --uninstall
EOF
}
# Handles require cmd.
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
# Handles realpath fallback.
realpath_fallback() {
python3 - <<'PY' "$1"
import os, sys
print(os.path.realpath(sys.argv[1]))
PY
}
# Handles abs path.
abs_path() {
local p="$1"
if command -v realpath >/dev/null 2>&1; then
realpath "$p"
else
realpath_fallback "$p"
fi
}
# Runs cmd.
run_cmd() {
if (( DRY_RUN )); then
printf '[dry-run] '
printf '%q ' "$@"
printf '\n'
else
"$@"
fi
}
# Handles confirm.
confirm() {
local prompt="${1:-Continue? [y/N]}"
if (( AUTO_YES )); then
return 0
fi
read -r -p "$prompt " reply
[[ "$reply" =~ ^[Yy]([Ee][Ss])?$ ]]
}
# Ensures repo layout is ready.
ensure_repo_layout() {
[[ -f "$INSTALL_DIR/$LAUNCHER_REL" ]] || \
die "Missing $LAUNCHER_REL under $INSTALL_DIR. Run from a full repo checkout or use bootstrap.sh."
[[ -f "$INSTALL_DIR/$ONBOARDING_REL" ]] || \
warn "Missing $ONBOARDING_REL under $INSTALL_DIR. Onboarding step will be skipped."
}
# Ensures dirs is ready.
ensure_dirs() {
run_cmd mkdir -p "$STATE_DIR"
}
# Handles target launcher.
target_launcher() {
printf '%s\n' "$INSTALL_DIR/$LAUNCHER_REL"
}
# Handles target link.
target_link() {
printf '%s\n' "$BIN_DIR/$TARGET_LINK_NAME"
}
# Handles shell rc file.
shell_rc_file() {
if [[ -n "${ZDOTDIR:-}" ]]; then
printf '%s\n' "$ZDOTDIR/.zshrc"
else
printf '%s\n' "$HOME/.zshrc"
fi
}
# Handles write state.
write_state() {
local launcher_path link_path
launcher_path="$(target_launcher)"
link_path="$(target_link)"
if (( DRY_RUN )); then
log "Would write install state to $STATE_FILE"
return 0
fi
cat > "$STATE_FILE" <<EOF
INSTALL_DIR='$INSTALL_DIR'
BIN_DIR='$BIN_DIR'
TARGET_LINK='$link_path'
TARGET_LAUNCHER='$launcher_path'
EOF
}
# Handles read state if present.
read_state_if_present() {
if [[ -f "$STATE_FILE" ]]; then
# shellcheck disable=SC1090
source "$STATE_FILE"
fi
}
# Handles install symlink.
install_symlink() {
local launcher_path link_path
launcher_path="$(target_launcher)"
link_path="$(target_link)"
[[ -f "$launcher_path" ]] || die "Launcher not found: $launcher_path"
run_cmd mkdir -p "$BIN_DIR"
if [[ -L "$link_path" || -e "$link_path" ]]; then
if [[ "$(abs_path "$link_path" 2>/dev/null || true)" == "$(abs_path "$launcher_path")" ]]; then
ok "Symlink already correct: $link_path"
return 0
fi
if ! confirm "Replace existing $link_path ? [y/N]"; then
die "Aborted"
fi
run_cmd rm -f "$link_path"
fi
run_cmd ln -s "$launcher_path" "$link_path"
ok "Installed symlink: $link_path -> $launcher_path"
}
# Handles managed block content.
managed_block_content() {
local install_dir_escaped
install_dir_escaped="$INSTALL_DIR"
cat <<EOF
$MANAGED_BEGIN
# Added by macos-scripts installer
export MACOS_SCRIPTS_HOME="$install_dir_escaped"
if [ -d "\$MACOS_SCRIPTS_HOME/bin" ] && [[ ":\$PATH:" != *":\$MACOS_SCRIPTS_HOME/bin:"* ]]; then
export PATH="\$MACOS_SCRIPTS_HOME/bin:\$PATH"
fi
$MANAGED_END
EOF
}
# Handles remove managed block.
remove_managed_block() {
local rc_file tmp_file
rc_file="$(shell_rc_file)"
[[ -f "$rc_file" ]] || return 0
tmp_file="$(mktemp)"
awk -v begin="$MANAGED_BEGIN" -v end="$MANAGED_END" '
$0 == begin { skip=1; next }
$0 == end { skip=0; next }
!skip { print }
' "$rc_file" > "$tmp_file"
if (( DRY_RUN )); then
log "Would remove managed shell block from $rc_file"
rm -f "$tmp_file"
else
mv "$tmp_file" "$rc_file"
ok "Removed managed shell block from $rc_file"
fi
}
# Handles append managed block.
append_managed_block() {
local rc_file
rc_file="$(shell_rc_file)"
if [[ ! -f "$rc_file" ]]; then
if (( DRY_RUN )); then
log "Would create shell rc file: $rc_file"
else
: > "$rc_file"
fi
fi
remove_managed_block
if (( DRY_RUN )); then
log "Would append managed shell block to $rc_file"
else
{
printf '\n'
managed_block_content
printf '\n'
} >> "$rc_file"
ok "Updated shell config: $rc_file"
fi
}
# Runs onboarding if present.
run_onboarding_if_present() {
local onboarding="$INSTALL_DIR/$ONBOARDING_REL"
if [[ -x "$onboarding" ]]; then
if (( DRY_RUN )); then
log "Would run onboarding: $onboarding"
else
"$onboarding" || warn "Onboarding returned non-zero status"
fi
else
warn "Onboarding script not executable or missing: $onboarding"
fi
}
# Handles do install.
do_install() {
ensure_repo_layout
ensure_dirs
log "Install dir: $INSTALL_DIR"
log "Bin dir: $BIN_DIR"
install_symlink
append_managed_block
write_state
run_onboarding_if_present
ok "Installation complete"
printf '\nNext steps:\n'
printf ' source "%s"\n' "$(shell_rc_file)"
printf ' mqlaunch\n'
}
# Handles remove symlink.
remove_symlink() {
local link_path
link_path="${TARGET_LINK:-$(target_link)}"
if [[ -L "$link_path" || -e "$link_path" ]]; then
run_cmd rm -f "$link_path"
ok "Removed: $link_path"
else
warn "Nothing to remove at: $link_path"
fi
}
# Handles remove state.
remove_state() {
if [[ -f "$STATE_FILE" ]]; then
run_cmd rm -f "$STATE_FILE"
ok "Removed state file"
fi
}
# Handles do uninstall.
do_uninstall() {
read_state_if_present
remove_symlink
remove_managed_block
remove_state
ok "Uninstall complete"
}
# Handles parse args.
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--install-dir)
[[ $# -ge 2 ]] || die "Missing value for --install-dir"
INSTALL_DIR="$2"
shift 2
;;
--bin-dir)
[[ $# -ge 2 ]] || die "Missing value for --bin-dir"
BIN_DIR="$2"
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
--uninstall)
UNINSTALL=1
shift
;;
--yes)
AUTO_YES=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "Unknown argument: $1"
;;
esac
done
}
# Runs the main entry point.
main() {
require_cmd bash
require_cmd mkdir
require_cmd ln
require_cmd rm
require_cmd awk
parse_args "$@"
INSTALL_DIR="$(abs_path "$INSTALL_DIR")"
BIN_DIR="$(abs_path "$BIN_DIR")"
if (( UNINSTALL )); then
do_uninstall
else
do_install
fi
}
main "$@"