#!/bin/sh
# Installs tx0: https://tx0.io/docs/install/
#
# curl -LsSf https://tx0.io/install.sh | sh
#
# TX0_VERSION a version to install, such as 0.1.0 (default: the latest release)
# TX0_INSTALL_DIR where to put the binary (default: ~/.local/bin)
# TX0_DOWNLOAD_BASE a mirror to download from instead of https://tx0.io/download, laid out
# the same way: /latest holds the latest version, and the archives
# and their .sha256 files are in //
#
# The archive is checked against its published SHA-256 sum before anything is installed.
# Nothing is written outside the install directory, and no root rights are needed.
set -eu
# Where the archives come from. tx0.io keeps them in // and names the latest
# version in /latest. The GitHub releases keep them in /v/ and are found
# through the redirect of /releases/latest; set default_base to github_base to use them.
site_base="https://tx0.io/download"
github_base="https://github.com/tx0io/tx0/releases/download"
default_base=$site_base
docs="https://tx0.io/docs/install/"
say() { printf 'tx0: %s\n' "$*" >&2; }
fail() { say "$*"; exit 1; }
has() { command -v "$1" > /dev/null 2>&1; }
# download URL FILE
download() {
if has curl; then
curl --proto '=https' --tlsv1.2 -fsSL "$1" -o "$2"
elif has wget; then
wget --https-only -q "$1" -O "$2"
else
fail "needs curl or wget to download"
fi
}
# the latest version, without the leading v
latest() {
case $base in
"$github_base")
if has curl; then
url=$(curl --proto '=https' --tlsv1.2 -fsSLI -o /dev/null -w '%{url_effective}' "${base%/download}/latest")
else
url=$(wget --https-only -q -S --spider "${base%/download}/latest" 2>&1 | sed -n 's/^ *[Ll]ocation: *//p' | tail -n 1)
fi
found=${url##*/tag/}
;;
*)
download "$base/latest" "$tmp/latest" || fail "could not read the latest version from $base/latest"
found=$(tr -d ' \t\r\n' < "$tmp/latest")
;;
esac
found=${found#v}
case $found in [0-9]*) printf '%s\n' "$found" ;; *) fail "could not find the latest version at $base" ;; esac
}
sha256() {
if has sha256sum; then sha256sum "$1" | cut -d ' ' -f 1
elif has shasum; then shasum -a 256 "$1" | cut -d ' ' -f 1
elif has openssl; then openssl dgst -sha256 "$1" | sed 's/^.*= *//'
else fail "needs sha256sum, shasum or openssl to check the download"
fi
}
case $(uname -s) in
Linux) os=unknown-linux-musl ;;
Darwin) os=apple-darwin ;;
*) fail "no build for $(uname -s); on Windows, use install.ps1. Other ways to install: $docs" ;;
esac
case $(uname -m) in
x86_64 | amd64) arch=x86_64 ;;
aarch64 | arm64) arch=aarch64 ;;
*) fail "no build for $(uname -m); other ways to install: $docs" ;;
esac
# a shell running under Rosetta reports x86_64; the native build is the better choice
if [ "$os" = apple-darwin ] && [ "$arch" = x86_64 ] && [ "$(sysctl -n sysctl.proc_translated 2> /dev/null || echo 0)" = 1 ]; then
arch=aarch64
fi
target="$arch-$os"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT INT TERM
base="${TX0_DOWNLOAD_BASE:-$default_base}"
base="${base%/}"
if [ -n "${TX0_VERSION:-}" ]; then
version="${TX0_VERSION#v}"
else
version=$(latest)
fi
tag="v$version"
case $base in
"$github_base") from="$base/$tag" ;;
*) from="$base/$version" ;;
esac
name="tx0-$tag-$target"
dir="${TX0_INSTALL_DIR:-$HOME/.local/bin}"
say "downloading tx0 $version for $target"
download "$from/$name.tar.gz" "$tmp/$name.tar.gz" || fail "could not download $name.tar.gz from $from; tx0 $version may not be built for $target. Other ways to install: $docs"
download "$from/$name.tar.gz.sha256" "$tmp/$name.tar.gz.sha256" || fail "could not download the SHA-256 sum of $name.tar.gz"
want=$(cut -d ' ' -f 1 < "$tmp/$name.tar.gz.sha256")
got=$(sha256 "$tmp/$name.tar.gz")
[ -n "$want" ] && [ "$want" = "$got" ] || fail "the SHA-256 sum of $name.tar.gz does not match the published one; nothing was installed"
tar -xzf "$tmp/$name.tar.gz" -C "$tmp"
mkdir -p "$dir"
cp "$tmp/$name/tx0" "$dir/tx0.new"
chmod 755 "$dir/tx0.new"
mv -f "$dir/tx0.new" "$dir/tx0"
say "installed $("$dir/tx0" --version 2> /dev/null || echo tx0) to $dir/tx0"
case ":${PATH}:" in
*":$dir:"*) ;;
*)
say "$dir is not on your PATH. Add it, for example in ~/.profile or your shell's rc file:"
# shellcheck disable=SC2016 # $PATH is meant literally, for the user to paste
printf '\n export PATH="%s:$PATH"\n\n' "$dir" >&2
;;
esac