Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·247 lines (220 loc) · 6.27 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·247 lines (220 loc) · 6.27 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
#!/usr/bin/env bash
set -euo pipefail
REPO="openclaw/ocm"
usage() {
cat <<'EOF'
Install ocm from GitHub release binaries.
Usage:
install.sh [--version <tag>] [--prefix <dir>] [--bin-dir <dir>] [--force]
Options:
--version <tag> Release tag to install. Defaults to the latest release.
--prefix <dir> Base install prefix. Defaults to ~/.local, which needs HOME.
--bin-dir <dir> Install directory for the ocm binary. Defaults to <prefix>/bin.
--force Overwrite an existing ocm binary.
--help Show this help.
EOF
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "error: required command not found: $1" >&2
exit 1
fi
}
detect_target() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Darwin) os="apple-darwin" ;;
Linux) os="unknown-linux-gnu" ;;
*)
echo "error: unsupported operating system: $os" >&2
exit 1
;;
esac
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
arm64|aarch64) arch="aarch64" ;;
*)
echo "error: unsupported architecture: $arch" >&2
exit 1
;;
esac
if [[ "$os" == "unknown-linux-gnu" && "$arch" == "aarch64" ]]; then
echo "error: unsupported platform: aarch64-unknown-linux-gnu" >&2
exit 1
fi
printf '%s-%s\n' "$arch" "$os"
}
download_url_for() {
local version="$1"
local asset="$2"
if [[ "$version" == "latest" ]]; then
printf 'https://github.com/%s/releases/latest/download/%s\n' "$REPO" "$asset"
else
printf 'https://github.com/%s/releases/download/%s/%s\n' "$REPO" "$version" "$asset"
fi
}
version="latest"
prefix=""
prefix_given="false"
bin_dir=""
force="false"
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
shift
[[ $# -gt 0 && -n "$1" ]] || { echo "error: --version requires a value" >&2; exit 1; }
version="$1"
;;
--prefix)
shift
[[ $# -gt 0 && -n "$1" ]] || { echo "error: --prefix requires a value" >&2; exit 1; }
prefix="$1"
prefix_given="true"
;;
--bin-dir)
shift
[[ $# -gt 0 && -n "$1" ]] || { echo "error: --bin-dir requires a value" >&2; exit 1; }
bin_dir="$1"
;;
--force)
force="true"
;;
--help|-h)
usage
exit 0
;;
*)
echo "error: unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
shift
done
require_command curl
require_command tar
require_command mktemp
require_command uname
require_command awk
require_command tr
if ! command -v sha256sum >/dev/null 2>&1 && ! command -v shasum >/dev/null 2>&1; then
echo "error: required command not found: sha256sum or shasum" >&2
exit 1
fi
if [[ -z "$bin_dir" ]]; then
if [[ "$prefix_given" != "true" ]]; then
if [[ -z "${HOME:-}" ]]; then
echo "error: HOME is not set; pass --prefix or --bin-dir to choose where to install ocm" >&2
exit 1
fi
prefix="${HOME}/.local"
fi
bin_dir="${prefix}/bin"
fi
destination="${bin_dir}/ocm"
protect_destination() {
local resolved_bin suffix
if [[ -L "$destination" ]]; then
echo "error: refusing to replace a symlink; update ocm through its owning installer or package manager" >&2
exit 1
fi
resolved_bin="$bin_dir"
suffix=""
while [[ ! -d "$resolved_bin" ]]; do
suffix="/$(basename -- "$resolved_bin")${suffix}"
resolved_bin="$(dirname -- "$resolved_bin")"
done
resolved_bin="$(cd -- "$resolved_bin" && pwd -P)${suffix}"
# npm owns files below node_modules even when aliases or manifests change.
# Do not execute an existing binary or require Node to protect that boundary.
case "${resolved_bin}/" in
*/node_modules/*)
echo "error: npm manages this destination; update @openclaw/ocm with npm" >&2
exit 1
;;
esac
if [[ -f "${resolved_bin}/../INSTALL_RECEIPT.json" ]]; then
echo "error: Homebrew manages this destination; run brew upgrade openclaw/tap/ocm" >&2
exit 1
fi
if [[ -d "$destination" ]]; then
echo "error: ${destination} is a directory; remove it or choose another --bin-dir" >&2
exit 1
fi
if [[ -e "$destination" && "$force" != "true" ]]; then
echo "error: ${destination} already exists; run \"ocm self update\" or rerun with --force to replace it" >&2
exit 1
fi
}
protect_destination
target="$(detect_target)"
asset="ocm-${target}.tar.gz"
url="$(download_url_for "$version" "$asset")"
checksums_url="$(download_url_for "$version" "SHA256SUMS")"
tmp_dir="$(mktemp -d)"
staged_path=""
cleanup() {
rm -rf "$tmp_dir"
if [[ -n "$staged_path" ]]; then
rm -f "$staged_path"
fi
}
trap cleanup EXIT
archive_path="${tmp_dir}/${asset}"
checksums_path="${tmp_dir}/SHA256SUMS"
echo "Downloading ${url}"
curl -fsSL "$url" -o "$archive_path"
curl -fsSL "$checksums_url" -o "$checksums_path"
expected_sha256="$(
awk -v asset="$asset" '
$2 == asset || $2 == "*" asset {
print $1
found = 1
exit
}
END {
if (!found) exit 1
}
' "$checksums_path"
)" || {
echo "error: SHA256SUMS does not contain ${asset}" >&2
exit 1
}
if [[ ! "$expected_sha256" =~ ^[0-9a-fA-F]{64}$ ]]; then
echo "error: SHA256SUMS contains an invalid digest for ${asset}" >&2
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
actual_sha256="$(sha256sum "$archive_path" | awk '{print $1}')"
else
actual_sha256="$(shasum -a 256 "$archive_path" | awk '{print $1}')"
fi
actual_sha256="$(printf '%s' "$actual_sha256" | tr '[:upper:]' '[:lower:]')"
expected_sha256="$(printf '%s' "$expected_sha256" | tr '[:upper:]' '[:lower:]')"
if [[ "$actual_sha256" != "$expected_sha256" ]]; then
echo "error: checksum mismatch for ${asset}" >&2
exit 1
fi
tar -xzf "$archive_path" -C "$tmp_dir"
binary_path="${tmp_dir}/ocm"
if [[ ! -f "$binary_path" || -L "$binary_path" ]]; then
echo "error: release archive did not contain a regular ocm binary" >&2
exit 1
fi
mkdir -p "$bin_dir"
protect_destination
# Write the new binary beside the destination and rename it into place, so a
# failed or interrupted write never leaves a partial ocm or removes the old one.
staged_path="$(mktemp "${bin_dir}/.ocm-install.XXXXXX")"
install -m 0755 "$binary_path" "$staged_path"
mv -f "$staged_path" "$destination"
staged_path=""
echo "Installed ocm to ${destination}"
case ":${PATH}:" in
*":${bin_dir}:"*) ;;
*)
echo "Add ${bin_dir} to your PATH to run ocm from any shell."
;;
esac