Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
113 lines (99 loc) · 4.43 KB
/
Copy pathinstall.sh
File metadata and controls
113 lines (99 loc) · 4.43 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
#!/bin/sh
set -eu
repo=Spectra010s/opencode-termux
release_tag=v2.2.7
if [ "$#" -gt 0 ]; then
[ "$#" -eq 2 ] && [ "$1" = --version ] || { echo 'Usage: install.sh [--version X.Y.Z[-N]]' >&2; exit 2; }
base=${2%%-*}
case "$2" in
*-*) case "${2#*-}" in ''|*[!0-9]*|0*) echo 'Invalid package revision.' >&2; exit 2 ;; esac ;;
esac
case "$base" in
*[!0-9.]*|.*|*..*|*.) echo 'Invalid release version.' >&2; exit 2 ;;
esac
printf '%s\n' "$base" | awk -F. 'NF == 3 && $1 != "" && $2 != "" && $3 != "" { ok = 1 } END { exit !ok }' || { echo 'Invalid release version.' >&2; exit 2; }
release_tag=v$2
fi
# The tarball is named after the version; the revision only moves the tag.
version=${release_tag#v}
version=${version%%-*}
prefix=${PREFIX:-/data/data/com.termux/files/usr}
lib_dir=$prefix/lib/opencode
launcher=$prefix/bin/opencode
for tool in curl sha256sum tar sed mktemp install rm 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-bundle"
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.
[ "$(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; }
# bun, ripgrep and libc++ are real dependencies now (same set as the deb `Depends`).
missing=
command -v bun >/dev/null 2>&1 || missing="$missing bun"
command -v rg >/dev/null 2>&1 || missing="$missing ripgrep"
# libc++ has no binary on PATH; the renderer links against its shared library.
[ -e "$prefix/lib/libc++_shared.so" ] || missing="$missing libc++"
if [ -n "$missing" ]; then
if [ "${OPENCODE_INSTALL_DEPS:-1}" = 0 ] || ! command -v pkg >/dev/null 2>&1; then
printf 'opencode needs:%s\n' "$missing" >&2
printf 'Install them with: pkg install%s\n' "$missing" >&2
exit 1
fi
printf 'Installing dependencies:%s\n' "$missing"
# shellcheck disable=SC2086 # the list is word-split into pkg arguments
pkg install -y $missing || {
printf 'Could not install:%s\n' "$missing" >&2
printf 'Run it yourself: pkg install%s\n' "$missing" >&2
exit 1
}
# pkg can report success while the binary lands somewhere off PATH.
for tool in bun rg; do
command -v "$tool" >/dev/null 2>&1 || {
printf 'Installed but not on PATH: %s\n' "$tool" >&2
exit 1
}
done
fi
if command -v dpkg-query >/dev/null 2>&1 &&
dpkg-query -S "$launcher" 2>/dev/null | grep -q '^opencode:'; then
echo 'opencode is installed as a package; upgrade it with: pkg upgrade opencode' >&2
exit 1
fi
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" bin lib
# The bundle is built against @TERMUX_PREFIX@ so one artefact fits every
# Termux prefix; point it at this device's prefix before installing.
find "$temporary_dir/lib" -type f -name '*.js' -exec \
sed -i "s|@TERMUX_PREFIX@/lib/opencode|$lib_dir|g" {} +
if grep -rlq '@TERMUX_PREFIX@' "$temporary_dir/lib"; then
echo 'Release bundle contains an unexpected placeholder path.' >&2
exit 1
fi
mkdir -p "$prefix/lib" "$prefix/bin"
rm -rf "$lib_dir"
mv "$temporary_dir/lib/opencode" "$lib_dir"
install -m 755 "$temporary_dir/bin/opencode" "$launcher.new.$$"
mv -f "$launcher.new.$$" "$launcher"
# 2.2.6 and earlier installed a standalone binary here; two `opencode`
# commands would resolve by PATH order.
legacy=$HOME/.local/bin/opencode
if [ -f "$legacy" ]; then
rm -f "$legacy"
printf 'Removed the previous install: %s\n' "$legacy"
fi
printf 'Installed: %s\n' "$launcher"
printf 'Run: opencode\n'