-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·491 lines (437 loc) · 16.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·491 lines (437 loc) · 16.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
#!/usr/bin/env bash
set -euo pipefail
SELFISHELL_RELEASE_ROOT="${SELFISHELL_RELEASE_ROOT:-https://github.com/jiminu/selfishell/releases}"
SELFISHELL_TEMP_DIR=""
SELFISHELL_STAGING_DIR=""
# Bootstrap gates colors separately for each stream.
if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
SELFISHELL_COLOR_GREEN=$'\033[32m'
SELFISHELL_COLOR_YELLOW=$'\033[33m'
SELFISHELL_COLOR_CYAN=$'\033[36m'
SELFISHELL_COLOR_BOLD=$'\033[1m'
SELFISHELL_COLOR_RESET=$'\033[0m'
else
SELFISHELL_COLOR_GREEN=
SELFISHELL_COLOR_YELLOW=
SELFISHELL_COLOR_CYAN=
SELFISHELL_COLOR_BOLD=
SELFISHELL_COLOR_RESET=
fi
if [[ -t 2 && -z "${NO_COLOR:-}" ]]; then
SELFISHELL_COLOR_RED_STDERR=$'\033[31m'
SELFISHELL_COLOR_RESET_STDERR=$'\033[0m'
else
SELFISHELL_COLOR_RED_STDERR=
SELFISHELL_COLOR_RESET_STDERR=
fi
bootstrap_error() {
printf '%sselfishell installer:%s %s\n' "$SELFISHELL_COLOR_RED_STDERR" "$SELFISHELL_COLOR_RESET_STDERR" "$*" >&2
}
bootstrap_cleanup() {
if [[ -n "$SELFISHELL_TEMP_DIR" && -d "$SELFISHELL_TEMP_DIR" ]]; then
rm -rf "$SELFISHELL_TEMP_DIR"
fi
if [[ -n "$SELFISHELL_STAGING_DIR" && -d "$SELFISHELL_STAGING_DIR" ]]; then
rm -rf "$SELFISHELL_STAGING_DIR"
fi
}
bootstrap_help() {
cat <<'EOF'
Usage:
install.sh [--version VERSION] [--prefix PATH] [--setup] [--yes]
[--skip-packages]
Options:
--version VERSION Install an exact Selfishell release
--prefix PATH Installation prefix (default: $HOME/.local)
--setup Run 'selfishell install' after installing the CLI
--yes Skip setup confirmation when used with --setup
--skip-packages Pass configuration-only mode to setup
--help Show this help
EOF
}
bootstrap_platform() {
local system_name="${SELFISHELL_BOOTSTRAP_OS:-$(uname -s)}"
case "$system_name" in
Darwin) printf 'macos\n' ;;
Linux) printf 'linux\n' ;;
*)
bootstrap_error "Unsupported operating system: $system_name"
return 1
;;
esac
}
bootstrap_architecture() {
local machine_arch="${SELFISHELL_BOOTSTRAP_ARCH:-$(uname -m)}"
case "$machine_arch" in
arm64 | aarch64) printf 'arm64\n' ;;
x86_64 | amd64) printf 'amd64\n' ;;
*)
bootstrap_error "Unsupported architecture: $machine_arch"
return 1
;;
esac
}
bootstrap_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
else
bootstrap_error "A SHA-256 tool is required (sha256sum or shasum)."
return 1
fi
}
bootstrap_version_is_valid() {
local version="${1:-}"
local prerelease identifier
local identifiers=()
[[ "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$ ]] ||
return 1
[[ "$version" == *-* ]] || return 0
prerelease="${version#*-}"
IFS=. read -r -a identifiers <<<"$prerelease"
for identifier in "${identifiers[@]}"; do
if [[ "$identifier" =~ ^[0-9]+$ && "$identifier" != 0 && "$identifier" == 0* ]]; then
return 1
fi
done
}
bootstrap_validate_version() {
bootstrap_version_is_valid "$1" || {
bootstrap_error "Invalid semantic version: $1"
return 1
}
}
bootstrap_curl() {
local mode="$1"
local connect_timeout="${SELFISHELL_CURL_CONNECT_TIMEOUT:-10}"
local low_speed_limit="${SELFISHELL_CURL_LOW_SPEED_LIMIT:-1024}"
local low_speed_time="${SELFISHELL_CURL_LOW_SPEED_TIME:-30}"
local metadata_max_time="${SELFISHELL_CURL_METADATA_MAX_TIME:-15}"
local value
local arguments=()
shift
for value in "$connect_timeout" "$low_speed_limit" "$low_speed_time" "$metadata_max_time"; do
case "$value" in
"" | *[!0-9]* | 0)
bootstrap_error "Curl timeout and speed settings must be positive integers."
return 2
;;
esac
done
arguments=(
--connect-timeout "$connect_timeout"
--speed-limit "$low_speed_limit"
--speed-time "$low_speed_time"
)
case "$mode" in
metadata) arguments+=(--max-time "$metadata_max_time") ;;
transfer) ;;
*)
bootstrap_error "Unknown curl mode: $mode"
return 2
;;
esac
curl -fsSL "${arguments[@]}" "$@"
}
bootstrap_latest_version() {
local official_root="https://github.com/jiminu/selfishell/releases"
local api_url response version published_version
if version="$(bootstrap_curl metadata "$SELFISHELL_RELEASE_ROOT/latest/download/VERSION" 2>/dev/null)"; then
version="${version#v}"
[[ -n "$version" ]] && {
printf '%s\n' "$version"
return
}
fi
[[ "$SELFISHELL_RELEASE_ROOT" == "$official_root" || -n "${SELFISHELL_RELEASE_TAGS_API_URL:-}" ]] || return 1
api_url="${SELFISHELL_RELEASE_TAGS_API_URL:-https://api.github.com/repos/jiminu/selfishell/tags?per_page=1}"
response="$(bootstrap_curl metadata \
-H 'Accept: application/vnd.github+json' \
-H 'X-GitHub-Api-Version: 2022-11-28' \
"$api_url" 2>/dev/null)" || return 1
version="$(printf '%s\n' "$response" | sed -n \
-e 's/.*"name"[[:space:]]*:[[:space:]]*"v\{0,1\}\([^"]*\)".*/\1/p' | sed -n '1p')"
[[ -n "$version" ]] || return 1
published_version="$(bootstrap_curl metadata "$SELFISHELL_RELEASE_ROOT/download/v${version}/VERSION" 2>/dev/null)" || return 1
published_version="${published_version#v}"
[[ "$published_version" == "$version" ]] || return 1
printf '%s\n' "$version"
}
bootstrap_atomic_link() {
local link_target="$1"
local link_path="$2"
local temporary_link="${link_path}.tmp.$$"
local suffix=0
if [[ -e "$link_path" && ! -L "$link_path" ]]; then
bootstrap_error "Refusing to replace non-link path: $link_path"
return 1
fi
while [[ -e "$temporary_link" || -L "$temporary_link" ]]; do
suffix=$((suffix + 1))
temporary_link="${link_path}.tmp.$$.${suffix}"
done
ln -s "$link_target" "$temporary_link"
if mv -fT "$temporary_link" "$link_path" 2>/dev/null; then
return
fi
mv -fh "$temporary_link" "$link_path"
}
bootstrap_validate_link_path() {
if [[ -e "$1" && ! -L "$1" ]]; then
bootstrap_error "Refusing to replace non-link path: $1"
return 1
fi
}
bootstrap_validate_release_link() {
local link_path="$1"
local target version
bootstrap_validate_link_path "$link_path" || return 1
[[ -L "$link_path" ]] || return 0
target="$(readlink "$link_path")"
version="${target#releases/}"
if [[ "$target" != "releases/$version" ]] || ! bootstrap_version_is_valid "$version"; then
bootstrap_error "Refusing to replace foreign release link: $link_path"
return 1
fi
}
# The CLI link is replaced only when it already points at this installation.
bootstrap_validate_cli_link() {
local link_path="$1"
local expected_target="$2"
bootstrap_validate_link_path "$link_path" || return 1
if [[ -L "$link_path" && "$(readlink "$link_path")" != "$expected_target" ]]; then
bootstrap_error "Refusing to replace $link_path, which points to $(readlink "$link_path"); remove it to install."
return 1
fi
}
# sfs is optional, so another program's sfs is left alone rather than failing.
bootstrap_sfs_link_is_ours() {
[[ ! -e "$1" && ! -L "$1" ]] && return 0
[[ -L "$1" && "$(readlink "$1")" == selfishell ]]
}
# Rejects FIFOs, devices, sockets, and any symlink but an existing sibling (the
# build ships bin/sfs -> selfishell); other targets could escape the release.
bootstrap_reject_unexpected_members() {
local staging="$1"
local unexpected link target
unexpected="$(find "$staging" ! -type f ! -type d ! -type l)"
if [[ -n "$unexpected" ]]; then
bootstrap_error "Release archive contains unsupported file types."
return 1
fi
while IFS= read -r link; do
target="$(readlink "$link")"
case "$target" in
/* | .. | ../* | */.. | */../*)
bootstrap_error "Release archive contains a link pointing outside the release."
return 1
;;
esac
if [[ ! -e "$link" ]]; then
bootstrap_error "Release archive contains a dangling link."
return 1
fi
done < <(find "$staging" -type l)
}
bootstrap_prune_releases() {
local releases_dir="$1"
local current_target="$2"
local previous_target="$3"
local release_dir release_name staging_dir staging_name staging_version staging_suffix
current_target="${current_target##*/}"
previous_target="${previous_target##*/}"
# Remove only old staging with a valid release version in its name.
while IFS= read -r -d '' staging_dir; do
staging_name="${staging_dir##*/}"
staging_version="${staging_name#.}"
staging_version="${staging_version%%.tmp.*}"
staging_suffix="${staging_name#*.tmp.}"
[[ -n "$staging_suffix" ]] || continue
bootstrap_version_is_valid "$staging_version" || continue
rm -rf "$staging_dir"
done < <(find "$releases_dir" -mindepth 1 -maxdepth 1 -type d -name '.*.tmp.*' -mmin +1440 -print0 2>/dev/null)
for release_dir in "$releases_dir"/*; do
[[ -d "$release_dir" && ! -L "$release_dir" ]] || continue
release_name="${release_dir##*/}"
[[ "$release_name" == "$current_target" || "$release_name" == "$previous_target" ]] && continue
bootstrap_version_is_valid "$release_name" || continue
[[ -f "$release_dir/VERSION" && ! -L "$release_dir/VERSION" && "$(<"$release_dir/VERSION")" == "$release_name" ]] || continue
[[ -d "$release_dir/bin" && ! -L "$release_dir/bin" ]] || continue
[[ -f "$release_dir/bin/selfishell" && ! -L "$release_dir/bin/selfishell" && -x "$release_dir/bin/selfishell" ]] || continue
rm -rf "$release_dir"
printf '%sRemoved inactive Selfishell release:%s %s\n' "$SELFISHELL_COLOR_GREEN" "$SELFISHELL_COLOR_RESET" "$release_name"
done
}
bootstrap_print_path_guidance() {
local bin_dir="$1"
printf '\n%sSelfishell is installed, but %s is not in PATH.%s\n' "$SELFISHELL_COLOR_YELLOW" "$bin_dir" "$SELFISHELL_COLOR_RESET"
printf 'Run this in the current shell:\n'
# Print a command for the user; do not expand the installer's PATH here.
# shellcheck disable=SC2016
printf ' %sexport PATH="%s:$PATH"%s\n' "$SELFISHELL_COLOR_BOLD" "$bin_dir" "$SELFISHELL_COLOR_RESET"
printf 'Add this command to your shell startup file to configure future sessions.\n'
printf 'Or continue without changing PATH:\n'
printf ' %s%s/selfishell install%s\n' "$SELFISHELL_COLOR_BOLD" "$bin_dir" "$SELFISHELL_COLOR_RESET"
}
main() {
local version=""
local prefix="${HOME}/.local"
local setup=0
local assume_yes=0
local skip_packages=0
local platform
local architecture
local release_url
local archive_name
local archive_file
local checksum_file
local expected_checksum
local actual_checksum
local share_dir
local releases_dir
local release_dir
local staging_dir
local bin_dir
local setup_args=()
local current_target=""
local current_version=""
local previous_target=""
local install_sfs=1
local nested_staging
while (("$#" > 0)); do
case "$1" in
--version)
shift
(("$#" > 0)) || {
bootstrap_error "--version requires a value"
return 2
}
version="${1#v}"
[[ -n "$version" ]] || {
bootstrap_error "Invalid semantic version: $1"
return 2
}
;;
--prefix)
shift
(("$#" > 0)) || {
bootstrap_error "--prefix requires a path"
return 2
}
prefix="$1"
;;
--setup) setup=1 ;;
--yes) assume_yes=1 ;;
--skip-packages) skip_packages=1 ;;
help | --help | -h)
bootstrap_help
return
;;
*)
bootstrap_error "Unknown option: $1"
return 2
;;
esac
shift
done
[[ "$prefix" == /* ]] || {
bootstrap_error "--prefix must be an absolute path: $prefix"
return 2
}
platform="$(bootstrap_platform)"
architecture="$(bootstrap_architecture)"
if [[ -z "$version" ]]; then
version="$(bootstrap_latest_version)" || {
bootstrap_error "Unable to determine the latest Selfishell release. Use --version VERSION to select one."
return 1
}
fi
bootstrap_validate_version "$version"
release_url="$SELFISHELL_RELEASE_ROOT/download/v${version}"
archive_name="selfishell-${version}-${platform}-${architecture}.tar.gz"
SELFISHELL_TEMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/selfishell-install.XXXXXX")"
# A signal must stop the install, not only clean up: continuing could still activate.
trap bootstrap_cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
archive_file="$SELFISHELL_TEMP_DIR/$archive_name"
checksum_file="$SELFISHELL_TEMP_DIR/SHA256SUMS"
printf '%sDownloading Selfishell %s for %s/%s%s\n' "$SELFISHELL_COLOR_CYAN" "$version" "$platform" "$architecture" "$SELFISHELL_COLOR_RESET"
bootstrap_curl transfer "$release_url/$archive_name" -o "$archive_file"
bootstrap_curl transfer "$release_url/SHA256SUMS" -o "$checksum_file"
# Collapse agreeing checksum entries; conflicting duplicates still fail verification.
expected_checksum="$(awk -v archive="$archive_name" '$2 == archive { print $1 }' "$checksum_file" | sort -u)"
if [[ -z "$expected_checksum" || "$expected_checksum" == *[!0-9a-fA-F]* ]]; then
bootstrap_error "No valid checksum found for $archive_name"
return 1
fi
actual_checksum="$(bootstrap_sha256 "$archive_file")"
if [[ "$actual_checksum" != "$expected_checksum" ]]; then
bootstrap_error "Checksum mismatch for $archive_name"
return 1
fi
share_dir="$prefix/share/selfishell"
releases_dir="$share_dir/releases"
release_dir="$releases_dir/$version"
bin_dir="$prefix/bin"
bootstrap_validate_release_link "$share_dir/current"
bootstrap_validate_release_link "$share_dir/previous"
bootstrap_validate_cli_link "$bin_dir/selfishell" "$share_dir/current/bin/selfishell"
if ! bootstrap_sfs_link_is_ours "$bin_dir/sfs"; then
printf '%sLeaving %s in place; it is not the Selfishell sfs link.%s\n' \
"$SELFISHELL_COLOR_YELLOW" "$bin_dir/sfs" "$SELFISHELL_COLOR_RESET"
install_sfs=0
fi
mkdir -p "$releases_dir" "$bin_dir"
if [[ -e "$release_dir" || -L "$release_dir" ]]; then
if [[ ! -d "$release_dir" || -L "$release_dir" ]]; then
bootstrap_error "Release path is not a directory: $release_dir"
return 1
fi
if [[ ! -r "$release_dir/VERSION" || "$(<"$release_dir/VERSION")" != "$version" || ! -x "$release_dir/bin/selfishell" ]]; then
bootstrap_error "Existing release is incomplete: $release_dir"
return 1
fi
printf '%sRelease already installed:%s %s\n' "$SELFISHELL_COLOR_CYAN" "$SELFISHELL_COLOR_RESET" "$release_dir"
else
staging_dir="$(mktemp -d "$releases_dir/.${version}.tmp.XXXXXX")"
SELFISHELL_STAGING_DIR="$staging_dir"
tar -xzf "$archive_file" -C "$staging_dir"
bootstrap_reject_unexpected_members "$staging_dir" || return 1
if [[ ! -r "$staging_dir/VERSION" || "$(<"$staging_dir/VERSION")" != "$version" || ! -x "$staging_dir/bin/selfishell" ]]; then
bootstrap_error "Release archive is invalid or has the wrong version."
return 1
fi
mv "$staging_dir" "$release_dir"
SELFISHELL_STAGING_DIR=""
# A concurrent install that created the release first receives this staging inside it.
nested_staging="$release_dir/${staging_dir##*/}"
[[ ! -d "$nested_staging" ]] || rm -rf "$nested_staging"
fi
bootstrap_validate_release_link "$share_dir/current"
bootstrap_validate_release_link "$share_dir/previous"
[[ ! -L "$share_dir/current" ]] || current_target="$(readlink "$share_dir/current")"
[[ ! -L "$share_dir/previous" ]] || previous_target="$(readlink "$share_dir/previous")"
current_version="${current_target##*/}"
if [[ -n "$current_target" && "$current_version" != "$version" ]]; then
bootstrap_atomic_link "$current_target" "$share_dir/previous"
previous_target="$current_target"
fi
bootstrap_atomic_link "releases/$version" "$share_dir/current"
bootstrap_atomic_link "$share_dir/current/bin/selfishell" "$bin_dir/selfishell"
[[ "$install_sfs" == 0 ]] || bootstrap_atomic_link selfishell "$bin_dir/sfs"
bootstrap_prune_releases "$releases_dir" "releases/$version" "$previous_target"
printf '%sInstalled Selfishell %s%s\n' "$SELFISHELL_COLOR_GREEN" "$version" "$SELFISHELL_COLOR_RESET"
if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
bootstrap_print_path_guidance "$bin_dir"
fi
if [[ "$setup" == "1" ]]; then
setup_args=(install)
[[ "$skip_packages" == "1" ]] && setup_args+=(--skip-packages)
[[ "$assume_yes" == "1" ]] && setup_args+=(--yes)
"$bin_dir/selfishell" "${setup_args[@]}"
fi
}
main "$@"