Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·141 lines (135 loc) · 6.31 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·141 lines (135 loc) · 6.31 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
#!/bin/sh
# Install orchard, the Orchard command line, or replace it with its latest
# release:
# curl -fsSL https://github.com/OrchardLinux/tools/releases/latest/download/install.sh | sh
# It goes into ~/.local/bin (ORCHARD_BIN names another directory), which new
# terminals then find on PATH (one line in the shell's startup file); then, on
# Linux, `orchard setup --ask` offers what this computer lacks for installing
# (usbmuxd; in WSL usbipd-win), asking first. `orchard update` replaces it later.
# sh -s -- --nightly the newest build of main instead of the release
# sh -s -- --window also the libraries the installer's window needs
# (the distributions' install.sh passes it)
set -eu
NAME=orchard
BUILDS=${ORCHARD_BUILDS_URL:-https://github.com/OrchardLinux/tools/releases/latest/download}
bin=${ORCHARD_BIN:-$HOME/.local/bin}
needs="usbmuxd usbipd"
nightly=
say() { printf '%s\n' "$*"; }
for arg in "$@"; do
case "$arg" in
--nightly) nightly=1; BUILDS=${ORCHARD_BUILDS_URL:-https://github.com/OrchardLinux/tools/releases/download/nightly} ;;
--window) needs="usbmuxd usbipd window" ;;
*) say "Unknown option $arg (--nightly, --window)." >&2; exit 1 ;;
esac
done
fail() { say "$*" >&2; exit 1; }
case "$(uname -s)" in
Darwin) asset=$NAME-macos ;;
Linux)
case "$(uname -m)" in
x86_64|amd64) asset=$NAME-linux-x86_64 ;;
aarch64|arm64) asset=$NAME-linux-aarch64 ;;
*) fail "No build for this computer ($(uname -m))." ;;
esac ;;
*) fail "No build for $(uname -s): macOS and Linux only." ;;
esac
command -v curl >/dev/null 2>&1 || fail 'curl is needed to download it.'
if command -v sha256sum >/dev/null 2>&1; then
sha256() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
sha256() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
fail 'sha256sum or shasum is needed to check the download.'
fi
# Whether version $1 is at least $2 ("0.10.0" > "0.9.3").
at_least() {
[ -n "$1" ] && [ -n "$2" ] && awk -v a="$1" -v b="$2" 'BEGIN {
n = split(a, x, "."); m = split(b, y, "."); if (m > n) n = m
for (i = 1; i <= n; i++) { if (x[i] + 0 > y[i] + 0) exit 0; if (x[i] + 0 < y[i] + 0) exit 1 }
exit 0 }'
}
# Puts $bin on PATH for the login shell's new terminals, once: a line in its
# startup files under the home directory, marked so a rerun finds it. The
# line itself checks PATH first, so a file read twice adds nothing twice.
# Prints the command for this terminal; fails if no file could be written.
MARK="# orchard: its directory on PATH (added by orchard's install.sh)"
on_path() {
case "$bin" in
"$HOME"/*) dir="\$HOME/${bin#"$HOME"/}" ;;
*) dir=$bin ;;
esac
shell=$(basename -- "${SHELL:-sh}")
case "$shell" in
fish)
conf=${XDG_CONFIG_HOME:-$HOME/.config}
case "$conf" in "$HOME"/*) ;; *) conf=$HOME/.config ;; esac
file=$conf/fish/conf.d/orchard.fish
if ! grep -qsF "$MARK" "$file"; then
mkdir -p "$(dirname -- "$file")" || return 1
printf '%s\ncontains -- "%s" $PATH; or set -gx PATH "%s" $PATH\n' "$MARK" "$dir" "$dir" > "$file" || return 1
fi
say "set -gx PATH \"$dir\" \$PATH"
return 0 ;;
zsh)
zdot=${ZDOTDIR:-$HOME}
case "$zdot" in "$HOME"|"$HOME"/*) ;; *) zdot=$HOME ;; esac
files="$zdot/.zshrc" ;;
bash)
# Terminals on Linux start bash without a login (~/.bashrc); macOS
# and SSH log in, reading the first of these three.
login=$HOME/.profile
for f in "$HOME/.bash_profile" "$HOME/.bash_login"; do
[ ! -f "$f" ] || { login=$f; break; }
done
files="$HOME/.bashrc $login" ;;
*) files="$HOME/.profile" ;;
esac
for file in $files; do
grep -qsF "$MARK" "$file" && continue
printf '\n%s\ncase ":$PATH:" in *":%s:"*) ;; *) export PATH="%s:$PATH" ;; esac\n' "$MARK" "$dir" "$dir" >> "$file" || return 1
done
say "export PATH=\"$dir:\$PATH\""
}
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
# An installed orchard as new as the release, or newer (a nightly), stays;
# --nightly always takes the newest build of main.
have=$([ -x "$bin/$NAME" ] && "$bin/$NAME" --version 2>/dev/null | awk '{print $2}' || true)
want=$([ -n "$nightly" ] || curl -fsSL "$BUILDS/VERSION" 2>/dev/null || true)
if [ -z "$nightly" ] && at_least "$have" "$want"; then
say "orchard $have is installed already (the latest release is $want)."
else
curl -fsSL -o "$work/SHA256SUMS" "$BUILDS/SHA256SUMS" || fail "Couldn't download the build's checksums."
curl -fL --progress-bar -o "$work/$asset" "$BUILDS/$asset" || fail "Couldn't download $asset."
expected=$(awk -v name="$asset" '$2 == name || $2 == "./" name {print $1}' "$work/SHA256SUMS")
[ -n "$expected" ] || fail "The build lists no $asset."
[ "$(sha256 "$work/$asset")" = "$expected" ] || fail "$asset does not match the build's checksum; nothing was installed."
# Beside its place first, so that taking it is one rename, also while the
# old one runs.
mkdir -p "$bin"
chmod 755 "$work/$asset"
cp "$work/$asset" "$bin/.$NAME-$$"
mv -f "$bin/.$NAME-$$" "$bin/$NAME"
say "Installed $("$bin/$NAME" --version) in $bin."
fi
case ":$PATH:" in
*":$bin:"*)
# Another program of the name earlier on PATH would be run instead.
found=$(command -v "$NAME" 2>/dev/null || true)
[ -z "$found" ] || [ "$found" = "$bin/$NAME" ] || say "Your shell finds $found first: remove it, or put $bin before it on PATH." ;;
*)
if now=$(on_path 2>/dev/null); then
say "New terminals find $NAME; in this one, run: $now"
else
say "$bin is not on your PATH: add it, or run $bin/$NAME."
fi ;;
esac
# The launcher this replaces kept its downloaded nightlies, which nothing reads now.
old=${XDG_CACHE_HOME:-$HOME/.cache}/orchard/tooling
[ ! -d "$old" ] || say "The earlier launcher's downloaded nightlies are still in $old; nothing uses them any more."
# What installing on a device needs from this computer, offered before the
# first install (asks on the terminal; never fails this script).
if [ "$(uname -s)" = Linux ] && "$bin/$NAME" setup --help 2>/dev/null | grep -q -- --ask; then
"$bin/$NAME" setup --ask $needs || true
fi