install.sh
AArch64 (ARM64) architecture stringShell command-substitution syntax
SHA-25621fc2ab620820735d161ca09bf240daf0cabdb75928643827efafc3a22e35bf9
Evidence
5:8… o pipefail
6
7REPO="xberg-io/xberg"
8BINARY_NAME="xberg"
9
10info() { printf '\033[1;34m%s\033[0m\n' "$*"; }
11warn() { printf '\033[1;33m%s\033[0m\n' "$*" >&2; }
12error() {
13 printf '\033[1;31merror: %s\033[0m\n' "$*" >&2
14 exit 1
15}
16
17need_cmd() {
18 if ! command -v "$1" >/dev/null 2>&1; then
19 error "need '$1' (command not found)"
20 fi
21}
22
23detect_os() {
24 local os
25 os="$(uname -s)"
26 case "$os" in
27 Linux*) echo "linux" ;;
28 Darwin*) echo "darwin" ;;
29 *) error "unsupported OS: $os" ;;
30 esac
31}
32
33detect_arch() {
34 local arch
35 arch="$(uname -m)"
36 case "$arch" in
37 x86_64 | amd64) echo "x86_64" ;;
38 aarch64 | arm64) echo "aarch64" ;;
39 *) error "unsupported architecture: $arch" ;;
40 esac
41}
42
43detect_libc() {
44 if [ -e /lib/ld-musl-x86_64.so.1 ] || [ -e /lib/ld-musl-aarch64.so.1 ]; then
45 echo musl
46 else
47 local version
48 version="$(ldd --version 2>&1 || true)"
49 case "$version" in *musl*) echo musl ;; *) echo gnu ;; esac
50 fi
51}
52
142:2… local artifact="$1" archive="$2" metadata="$3" expected actual
143 shift 3
144 expected="$(read_release_metadata "$metadata" "$artifact")" ||
145 error "no valid SHA-256 digest for ${artifact} in GitHub release metadata"
146 actual="$("$@" <"$archive")" || error "failed to compute SHA-256 for ${artifact}"
147 [ "${actual%% *}" = "$expected" ] || error "SHA-256 mismatch for ${artifact}"
148}
149
⋯4 lines
154
155 local -a hash_command
156 if command -v sha256sum >/dev/null 2>&1; then
157 hash_command=(sha256sum)
158 elif command -v shasum >/dev/null 2>&1; then
159 hash_command=(shasum -a 256)
160 else
161 error "need 'sha256sum' or 'shasum' to verify release archives"
162 fi
163
164 local os arch target version install_dir
165
166 os="$(detect_os)"
167 arch="$(detect_arch)"
168 target="$(detect_target "$os" "$arch")"
169
170 tmpdir="$(mktemp -d)"
171 trap 'rm -rf "$tmpdir"' EXIT
172 local metadata="$tmpdir/release.json"
173
174 if [ -n "${XBERG_VERSION:-}" ]; then
175 version="${XBERG_VERSION}"
176 case "$version" in
177 v*) ;;
178 *) version="v${v …
188:16… ing xberg ${version} for ${target}"
189
190 if [ -n "${XBERG_INSTALL:-}" ]; then
191 install_dir="${XBERG_INSTALL}"
192 elif [ "$(id -u)" -eq 0 ]; then
193 install_dir="/usr/local/bin"
194 else
195 install_dir="${HOME}/.xberg/bin"
196 fi
197
198 mkdir -p "$install_dir"
199
200 local stage_dir binary_path version_output artifact url http_status status
201 while :; do
202 artifact="xberg-cli-${target}.tar.gz"
203 url="https://github.com/${REPO}/releases/download/${version}/${artifact}"
204 info "Downloading ${url}"
205 if http_status="$(curl -q -fsSL --proto '=https' --proto-redir '=https' -w '%{http_code}' "$url" -o "${tmpdir}/${artifact}")"; then
206 if [ ! -f "$metadata" ]; then
207 fetch_release_metadata "tags/${version}" "$metadata" ||
208 error "faile …
210:33… "${tmpdir}/${artifact}" "$metadata" "${hash_command[@]}"
211 tar -xzf "${tmpdir}/${artifact}" -C "$tmpdir"
212 stage_dir="${tmpdir}/xberg-cli-${target}"
213 binary_path="${stage_dir}/${BINARY_NAME}"
214 [ -f "$binary_path" ] || error "binary not found in archive at ${binary_path}"
215 if [ "$os" != linux ]; then
216 break
217 fi
218 chmod +x "$binary_path"
219 if [ -f "${stage_dir}/${BINARY_NAME}.bin" ]; then
220 chmod +x "${stage_dir}/${BINARY_NAME}.bin"
221 fi
222 if version_output="$("$binary_path" --version 2>&1)"; then
223 break
224 fi
225 if [ -n "${XBERG_LINUX_LIBC:-}" ] || [[ "$target" != *-linux-gnu ]]; then
226 error "archive for ${target} cannot run on this host: ${version_output}"
227 fi
228 warn "Archive for ${target} cannot run on this host: ${ …
235:25… ${target} is unavailable (HTTP 404)"
236 fi
237 target="${arch}-unknown-linux-musl"
238 info "Trying ${target}"
239 done
240
241 if [ "$os" = linux ]; then
242 local library
243 for library in "${stage_dir}"/*.so "${stage_dir}"/*.so.*; do
244 [ -f "$library" ] || continue
245 cp "$library" "$install_dir/"
246 done
247 fi
248
249 if [ -f "${stage_dir}/${BINARY_NAME}.bin" ]; then
250 cp "${stage_dir}/${BINARY_NAME}.bin" "${install_dir}/${BINARY_NAME}.bin"
251 chmod +x "${install_dir}/${BINARY_NAME}.bin"
252 fi
253
254 if [ -d "${stage_dir}/lib" ] && [ "$(ls -A "${stage_dir}/lib" 2>/dev/null)" ]; then
255 mkdir -p "${install_dir}/lib"
256 # -R so nested library directories (e.g. lib/libheif) are copied, not skipped
257 cp -R "${stage_dir}/lib/"* "${install_dir}/lib/"
258 info "Installed runtime libraries to ${install_dir}/lib/"
259 fi
260
261 for dylib in "${stage_dir}"/*.dylib; do
262 [ -f "$dylib" ] || continue
263 cp "$dylib" "${install_dir}/"
264 info "Installed runtime library $(basename "$dylib") to ${install_dir}/"
265 done
266
267 cp "$binary_path" "${install_dir}/${BINARY_NAME}"
268 chmod +x "${install_dir}/${BINARY_NAME}"
269
270 if version_output="$("${install_dir}/${BINARY_NAME}" --version 2>&1)"; then
271 info "Installed ${BINARY_NAME} to ${install_dir}/${BINARY_NAME}"
272 info "Verified: …