Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
63 lines (53 loc) · 2.82 KB
/
Copy pathinstall.sh
File metadata and controls
63 lines (53 loc) · 2.82 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
#!/bin/sh
set -eu
repo=Spectra010s/opencode-termux
release_tag=v2.2.5
if [ "$#" -gt 0 ]; then
[ "$#" -eq 2 ] && [ "$1" = --version ] || { echo 'Usage: install.sh [--version X.Y.Z]' >&2; exit 2; }
case "$2" in
*[!0-9.]*|.*|*..*|*.) echo 'Invalid release version.' >&2; exit 2 ;;
esac
printf '%s\n' "$2" | awk -F. 'NF == 3 && $1 != "" && $2 != "" && $3 != "" { ok = 1 } END { exit !ok }' || { echo 'Invalid release version.' >&2; exit 2; }
release_tag=v$2
fi
version=${release_tag#v}
destination="$HOME/.local/bin/opencode"
for tool in curl sha256sum tar mktemp install mv uname getprop awk; do
command -v "$tool" >/dev/null 2>&1 || { printf 'Missing command: %s\n' "$tool" >&2; exit 1; }
done
asset="opencode-$version-android-aarch64-source"
archive="$asset.tar.gz"
base_url="https://github.com/$repo/releases/download/$release_tag"
# 64-bit ARM Android reports aarch64 here; x86, armv7 and 32-bit userspace
# report something else and cannot run this binary.
[ "$(uname -m)" = aarch64 ] || { echo 'This release requires Android aarch64.' >&2; exit 1; }
android_api=$(getprop ro.build.version.sdk)
case "$android_api" in
''|*[!0-9]*) echo 'Could not detect Android API level.' >&2; exit 1 ;;
esac
[ "$android_api" -ge 28 ] || { echo 'This release requires Android API 28 or newer.' >&2; exit 1; }
temporary_dir=$(mktemp -d)
trap 'rm -rf -- "$temporary_dir"' EXIT HUP INT TERM
curl -fL --retry 3 -o "$temporary_dir/SHA256SUMS" "$base_url/SHA256SUMS"
curl -fL --retry 3 -o "$temporary_dir/$archive" "$base_url/$archive"
checksum_line=$(awk -v name="$archive" '$2 == name && length($1) == 64 { print }' "$temporary_dir/SHA256SUMS")
[ -n "$checksum_line" ] || { echo 'Release checksum for archive is missing.' >&2; exit 1; }
[ "$(printf '%s\n' "$checksum_line" | awk 'END { print NR }')" -eq 1 ] || { echo 'Release archive checksum is ambiguous.' >&2; exit 1; }
printf '%s\n' "$checksum_line" | (cd "$temporary_dir" && sha256sum -c -)
tar -xzf "$temporary_dir/$archive" -C "$temporary_dir" "$asset"
checksum_line=$(awk -v name="$asset" '$2 == name && length($1) == 64 { print }' "$temporary_dir/SHA256SUMS")
[ -n "$checksum_line" ] || { echo 'Release checksum for binary is missing.' >&2; exit 1; }
[ "$(printf '%s\n' "$checksum_line" | awk 'END { print NR }')" -eq 1 ] || { echo 'Release binary checksum is ambiguous.' >&2; exit 1; }
printf '%s\n' "$checksum_line" | (cd "$temporary_dir" && sha256sum -c -)
mkdir -p "$HOME/.local/bin"
install -m 755 "$temporary_dir/$asset" "$destination.new.$$"
mv -f "$destination.new.$$" "$destination"
printf 'Installed: %s\n' "$destination"
case ":$PATH:" in
*":$HOME/.local/bin:"*) printf 'Run: opencode\n' ;;
*)
printf 'Run now: %s\n' "$destination"
printf 'To add it to PATH in your shell, add this line to ~/.bashrc (or your shell startup file):\n'
printf 'export PATH="$HOME/.local/bin:$PATH"\n'
;;
esac