-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathut_install.sh
More file actions
367 lines (328 loc) · 10.8 KB
/
Copy pathut_install.sh
File metadata and controls
367 lines (328 loc) · 10.8 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
#!/usr/bin/env bash
# From https://github.com/oneclickvirt/UnlockTests
set -euo pipefail
repo_url="https://github.com/oneclickvirt/UnlockTests/releases/download/output"
cdn_success_url=""
cdn_urls=(
"https://cdn0.spiritlhl.top/"
"http://cdn3.spiritlhl.net/"
"http://cdn1.spiritlhl.net/"
"http://cdn2.spiritlhl.net/"
)
asset_os=""
asset_arch=""
asset=""
bin_name="ut"
install_dir="${INSTALL_DIR:-}"
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Required command not found: $1" >&2
exit 1
fi
}
is_interactive() {
[ -t 0 ] && [ -z "${CI:-}" ] && [ -z "${NO_INTERACTIVE:-}" ] && [ -z "${NONINTERACTIVE:-}" ] && [ -z "${UT_INSTALL_NONINTERACTIVE:-}" ]
}
confirm_yes() {
[ "${YES:-}" = "1" ] || [ "${YES:-}" = "true" ] || [ "${UT_INSTALL_ASSUME_YES:-}" = "1" ] || [ "${UT_INSTALL_ASSUME_YES:-}" = "true" ]
}
normalize_path() {
local path="$1"
if command -v cygpath >/dev/null 2>&1; then
cygpath -u "$path" 2>/dev/null || printf '%s\n' "$path"
else
printf '%s\n' "$path"
fi
}
windows_path() {
local path="$1"
if command -v cygpath >/dev/null 2>&1; then
cygpath -w "$path" 2>/dev/null || printf '%s\n' "$path"
else
printf '%s\n' "$path"
fi
}
detect_os_arch() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux*) asset_os="linux" ;;
Darwin*) asset_os="darwin" ;;
FreeBSD*) asset_os="freebsd" ;;
OpenBSD*) asset_os="openbsd" ;;
MINGW* | MSYS* | CYGWIN*) asset_os="windows"; bin_name="ut.exe" ;;
*)
echo "Unsupported operating system: $os" >&2
exit 1
;;
esac
case "$arch" in
x86_64 | amd64 | x64) asset_arch="amd64" ;;
i386 | i486 | i586 | i686 | x86) asset_arch="386" ;;
aarch64 | aarch64_be | arm64 | arm64v8) asset_arch="arm64" ;;
arm | armel | armhf | armv5 | armv5l | armv6 | armv6l | armv7 | armv7l | armv8l) asset_arch="arm" ;;
loongarch64 | loong64) asset_arch="loong64" ;;
mips64el | mips64le) asset_arch="mips64le" ;;
mips64) asset_arch="mips64" ;;
mipsel | mipsle | mipsisa32r6el) asset_arch="mipsle" ;;
mips | mipsisa32r6) asset_arch="mips" ;;
ppc64el | ppc64le) asset_arch="ppc64le" ;;
ppc64) asset_arch="ppc64" ;;
riscv64 | riscv64gc) asset_arch="riscv64" ;;
s390x) asset_arch="s390x" ;;
*)
echo "Unsupported architecture: $arch" >&2
exit 1
;;
esac
case "$asset_os:$asset_arch" in
darwin:amd64 | darwin:arm64) ;;
windows:amd64 | windows:386 | windows:arm64) ;;
freebsd:amd64 | freebsd:386 | freebsd:arm64 | freebsd:arm) ;;
openbsd:amd64 | openbsd:386 | openbsd:arm64 | openbsd:arm) ;;
linux:amd64 | linux:386 | linux:arm64 | linux:arm | linux:riscv64 | linux:mips64 | linux:mips64le | linux:mipsle | linux:mips | linux:ppc64 | linux:ppc64le | linux:s390x | linux:loong64) ;;
windows:arm)
echo "Unsupported release: Windows 32-bit ARM is not provided." >&2
exit 1
;;
*)
echo "Unsupported release target: ${asset_os}/${asset_arch} (detected ${os}/${arch})" >&2
exit 1
;;
esac
asset="ut-${asset_os}-${asset_arch}"
}
default_system_dir() {
if [ "$asset_os" = "windows" ]; then
if [ -n "${ProgramFiles:-}" ]; then
normalize_path "${ProgramFiles}\\UnlockTests\\bin"
else
printf '%s\n' "/c/Program Files/UnlockTests/bin"
fi
else
printf '%s\n' "/usr/bin"
fi
}
default_user_dir() {
if [ "$asset_os" = "windows" ]; then
if [ -n "${LOCALAPPDATA:-}" ]; then
normalize_path "${LOCALAPPDATA}\\Microsoft\\WindowsApps"
else
normalize_path "${HOME:-.}\\AppData\\Local\\Microsoft\\WindowsApps"
fi
else
printf '%s\n' "${XDG_BIN_HOME:-${HOME:-.}/.local/bin}"
fi
}
is_admin() {
if [ "$asset_os" = "windows" ]; then
if command -v powershell.exe >/dev/null 2>&1; then
powershell.exe -NoProfile -NonInteractive -Command "[Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() | ForEach-Object { if (\$_.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { exit 0 } else { exit 1 } }" >/dev/null 2>&1
else
return 1
fi
else
[ "$(id -u)" -eq 0 ]
fi
}
can_write_dir() {
local dir="$1"
mkdir -p -- "$dir" 2>/dev/null && [ -w "$dir" ]
}
install_binary() {
local source="$1" dir="$2"
local destination="${dir}/${bin_name}"
mkdir -p -- "$dir"
if command -v install >/dev/null 2>&1; then
install -m 0755 -- "$source" "$destination"
else
cp -- "$source" "$destination"
chmod 0755 -- "$destination"
fi
echo "Installed ${bin_name} to ${destination}"
}
install_binary_elevated() {
local source="$1" dir="$2"
local destination="${dir}/${bin_name}"
if [ "$asset_os" = "windows" ]; then
if ! command -v powershell.exe >/dev/null 2>&1; then
return 1
fi
UT_INSTALL_SRC="$(windows_path "$source")" \
UT_INSTALL_DIR="$(windows_path "$dir")" \
UT_INSTALL_DEST="$(windows_path "$destination")" \
powershell.exe -NoProfile -NonInteractive -Command 'Start-Process -FilePath powershell.exe -Verb RunAs -Wait -ArgumentList @("-NoProfile","-NonInteractive","-Command","New-Item -ItemType Directory -Force -LiteralPath $env:UT_INSTALL_DIR | Out-Null; Copy-Item -LiteralPath $env:UT_INSTALL_SRC -Destination $env:UT_INSTALL_DEST -Force")' >/dev/null 2>&1
if [ -f "$destination" ]; then
echo "Installed ${bin_name} to ${destination}"
return 0
fi
return 1
fi
if command -v sudo >/dev/null 2>&1; then
sudo mkdir -p -- "$dir"
if command -v install >/dev/null 2>&1; then
sudo install -m 0755 -- "$source" "$destination"
else
sudo cp -- "$source" "$destination"
sudo chmod 0755 -- "$destination"
fi
echo "Installed ${bin_name} to ${destination}"
return 0
fi
if command -v doas >/dev/null 2>&1; then
doas mkdir -p -- "$dir"
if command -v install >/dev/null 2>&1; then
doas install -m 0755 -- "$source" "$destination"
else
doas cp -- "$source" "$destination"
doas chmod 0755 -- "$destination"
fi
echo "Installed ${bin_name} to ${destination}"
return 0
fi
return 1
}
choose_install_dir() {
local system_dir user_dir choice
system_dir="$(default_system_dir)"
user_dir="$(default_user_dir)"
if [ -n "$install_dir" ]; then
install_dir="$(normalize_path "$install_dir")"
return
fi
if is_admin || can_write_dir "$system_dir"; then
install_dir="$system_dir"
return
fi
if ! is_interactive || confirm_yes; then
install_dir="$user_dir"
echo "No administrator/root permission detected; falling back to user install: ${install_dir}"
return
fi
echo "No administrator/root permission detected."
echo "1) Try elevated install to ${system_dir}"
echo "2) Install for current user to ${user_dir}"
printf 'Choose [1/2] (default 2): '
read -r choice || choice="2"
case "$choice" in
1) install_dir="$system_dir" ;;
*) install_dir="$user_dir" ;;
esac
}
check_cdn() {
local origin_url="$1"
for cdn_url in "${cdn_urls[@]}"; do
if curl -fsSL -k --max-time 6 -- "${cdn_url}${origin_url}" | grep -q "success"; then
cdn_success_url="$cdn_url"
return 0
fi
sleep 0.5
done
cdn_success_url=""
}
download_asset() {
local asset_name="$1" target="$2" download_url
local url="${repo_url}/${asset_name}"
download_url="${cdn_success_url}${url}"
echo "Downloading ${asset_name}..."
if command -v wget >/dev/null 2>&1; then
if wget -q -O "$target" -- "$download_url"; then
return 0
fi
echo "wget download failed, falling back to curl" >&2
fi
curl -fsSL -o "$target" -- "$download_url"
}
# Replace existing ut binaries found on the system (current dir, PATH, common locations)
# Best-effort: always returns 0 so set -e won't kill the script
replace_existing() {
local source="$1" installed_to="$2"
# 1) Copy to current working directory so ./ut works immediately
if [ -n "${PWD:-}" ] && [ -d "$PWD" ] && [ -w "$PWD" ]; then
cp -f -- "$source" "${PWD}/${bin_name}" 2>/dev/null || true
echo "Copied ${bin_name} to current directory: ${PWD}/${bin_name}"
fi
# 2) Find existing ut in PATH (skip the one we just installed)
local existing
if command -v which >/dev/null 2>&1; then
existing="$(which -a "$bin_name" 2>/dev/null || true)"
elif command -v command >/dev/null 2>&1; then
existing="$(command -v "$bin_name" 2>/dev/null || true)"
fi
if [ -n "$existing" ]; then
local p
while IFS= read -r p; do
[ -z "$p" ] && continue
# Resolve symlinks (best-effort, not all systems have readlink -f)
if [ -L "$p" ] 2>/dev/null; then
p="$(readlink -f "$p" 2>/dev/null || printf '%s' "$p")"
fi
# Skip if it's the one we just installed or copied to PWD
[ "$p" = "$installed_to" ] && continue
[ "$p" = "${PWD}/${bin_name}" ] && continue
if [ -w "$p" ] 2>/dev/null || [ -w "$(dirname "$p")" ] 2>/dev/null; then
cp -f -- "$source" "$p" 2>/dev/null && echo "Replaced existing ${bin_name} at ${p}"
fi
done <<< "$existing"
fi
# 3) Also check common system locations (using HOME with default for set -u safety)
local common_dirs=("/usr/bin" "/usr/local/bin" "${HOME:-}/.local/bin")
local d candidate
for d in "${common_dirs[@]}"; do
candidate="${d}/${bin_name}"
[ "$candidate" = "$installed_to" ] && continue
[ "$candidate" = "${PWD}/${bin_name}" ] && continue
if [ -f "$candidate" ]; then
if [ -w "$candidate" ] 2>/dev/null || [ -w "$d" ] 2>/dev/null; then
cp -f -- "$source" "$candidate" 2>/dev/null && echo "Replaced ${bin_name} at ${candidate}"
fi
fi
done
return 0
}
need_cmd curl
detect_os_arch
choose_install_dir
tmp_file="$(mktemp)"
trap 'rm -f -- "$tmp_file"' EXIT
check_cdn "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
if [ -n "$cdn_success_url" ]; then
echo "CDN available, using CDN"
else
echo "No CDN available, using GitHub release URL"
fi
download_asset "$asset" "$tmp_file"
chmod 0755 -- "$tmp_file"
installed_to=""
if install_binary "$tmp_file" "$install_dir" 2>/dev/null; then
installed_to="${install_dir}/${bin_name}"
replace_existing "$tmp_file" "$installed_to"
exit 0
fi
if [ -n "${INSTALL_DIR:-}" ]; then
echo "Failed to install to INSTALL_DIR=${install_dir}" >&2
exit 1
fi
if [ "$install_dir" = "$(default_system_dir)" ]; then
if is_interactive && ! confirm_yes; then
if install_binary_elevated "$tmp_file" "$install_dir"; then
installed_to="${install_dir}/${bin_name}"
replace_existing "$tmp_file" "$installed_to"
exit 0
fi
fi
install_dir="$(default_user_dir)"
echo "Falling back to user install: ${install_dir}"
if install_binary "$tmp_file" "$install_dir" 2>/dev/null; then
installed_to="${install_dir}/${bin_name}"
replace_existing "$tmp_file" "$installed_to"
exit 0
else
echo "Failed to install to ${install_dir}" >&2
exit 1
fi
else
echo "Failed to install to ${install_dir}" >&2
exit 1
fi