#!/bin/sh # The Felt Club CLI installer (OPS-077). # # curl -fsSL https:///install.sh | sh # # Detects the OS and CPU, downloads the matching `tfc` binary and the release's # SHA256SUMS, checks the binary against its published SHA-256 checksum, and # installs it as `tfc` in a user-writable directory. No `tfc` is installed or # replaced when any step fails. # # The release job (scripts/package-cli-release.js) renders this template: it # replaces the download base URL and the release version placeholders below. # # Settings (environment variables): # TFC_INSTALL_DIR Where to install `tfc` (default: $HOME/.local/bin). # TFC_VERSION Release to install (default: the release this # installer was published with). # TFC_DOWNLOAD_BASE_URL Host the release files are served from. Files are # read from //tfc-- and # //SHA256SUMS. https only # (file:// is accepted for local mirrors and tests). # # Supported: macOS and Linux (glibc) on arm64 and x64. set -eu # Everything runs inside main, so a download cut short by the network cannot # run half of this script. main() { base_url="${TFC_DOWNLOAD_BASE_URL:-https://downloads.thefeltclub.poker}" version="${TFC_VERSION:-0.1.0}" install_dir="${TFC_INSTALL_DIR:-${HOME:?HOME is not set; set TFC_INSTALL_DIR}/.local/bin}" case "$base_url$version" in *__TFC_*) fail "this installer was not rendered by a release; download install.sh from the release host" ;; esac case "$base_url" in https://*) ;; file://*) ;; # local mirrors and the installer's own tests *) fail "TFC_DOWNLOAD_BASE_URL must be an https URL, got: $base_url" ;; esac case "$version" in [!0-9]* | *[!0-9A-Za-z.+-]* | '') fail "invalid TFC_VERSION: $version" ;; esac base_url="${base_url%/}" # One substitution per assignment, so `set -e` stops on a failed detection. target_os="$(detect_os)" target_arch="$(detect_arch)" target="$target_os-$target_arch" file="tfc-$target" checksum_cmd="$(find_checksum_tool)" command -v curl >/dev/null 2>&1 || fail "need curl to download tfc" tmp="$(mktemp -d 2>/dev/null || mktemp -d -t tfc-install)" staged='' trap 'rm -rf "$tmp"; if [ -n "$staged" ]; then rm -f "$staged"; fi' EXIT trap 'exit 130' INT trap 'exit 143' TERM say "Downloading tfc $version for $target" download "$base_url/$version/$file" "$tmp/$file" download "$base_url/$version/SHA256SUMS" "$tmp/SHA256SUMS" expected="$(awk -v f="$file" '$2 == f || $2 == "*" f { print $1; exit }' "$tmp/SHA256SUMS")" case "$expected" in '') fail "SHA256SUMS lists no checksum for $file" ;; esac if [ "${#expected}" -ne 64 ]; then fail "SHA256SUMS has a malformed checksum for $file"; fi case "$expected" in *[!0-9a-f]*) fail "SHA256SUMS has a malformed checksum for $file" ;; esac actual="$($checksum_cmd "$tmp/$file" | awk '{ print $1 }')" if [ -z "$actual" ]; then fail "$checksum_cmd could not read the download; nothing was installed"; fi if [ "$actual" != "$expected" ]; then fail "checksum mismatch for $file (expected $expected, got $actual); nothing was installed" fi say "Checksum verified (SHA-256 $expected)" mkdir -p "$install_dir" || fail "cannot create $install_dir; set TFC_INSTALL_DIR to a writable directory" dest="$install_dir/tfc" if [ -d "$dest" ]; then fail "$dest is a directory; remove it or set TFC_INSTALL_DIR"; fi staged="$install_dir/.tfc-install.$$" cp "$tmp/$file" "$staged" || fail "cannot write to $install_dir; set TFC_INSTALL_DIR to a writable directory" chmod 755 "$staged" # Run the new binary before it replaces anything: a checksum proves the download, not that # this machine can run it (an old glibc, a noexec mount). if ! installed="$("$staged" --version 2>&1)"; then fail "the downloaded tfc does not run on this machine; $dest was left as it was. Output: $installed" fi # A rename in the same directory replaces the old binary in one step, even while it runs. if [ -e "$dest" ]; then say "Replacing the existing $dest"; fi mv -f "$staged" "$dest" || fail "cannot replace $dest" staged='' say "Installed $installed to $dest" case ":${PATH:-}:" in *":$install_dir:"*) say "Run: tfc" ;; *) say "$install_dir is not on your PATH. Add this line to your shell profile:" say " export PATH=\"$install_dir:\$PATH\"" ;; esac } say() { printf 'tfc-install: %s\n' "$1" } fail() { printf 'tfc-install: error: %s\n' "$1" >&2 exit 1 } detect_os() { os="$(uname -s)" case "$os" in Darwin) echo darwin ;; Linux) if is_musl; then fail "musl-based Linux (such as Alpine) is not supported; tfc needs glibc"; fi echo linux ;; MINGW* | MSYS* | CYGWIN* | Windows_NT) fail "Windows is not supported; tfc runs on macOS and Linux" ;; *) fail "unsupported operating system: $os (tfc runs on macOS and Linux)" ;; esac } is_musl() { if command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl; then return 0; fi for loader in /lib/ld-musl-*; do if [ -e "$loader" ]; then return 0; fi done return 1 } detect_arch() { arch="$(uname -m)" case "$arch" in arm64 | aarch64) echo arm64 ;; x86_64 | amd64) # A shell running under Rosetta reports x86_64 on an Apple silicon Mac. if [ "$(uname -s)" = Darwin ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then echo arm64 else echo x64 fi ;; *) fail "unsupported CPU architecture: $arch (tfc runs on arm64 and x64)" ;; esac } find_checksum_tool() { if command -v sha256sum >/dev/null 2>&1; then echo sha256sum elif command -v shasum >/dev/null 2>&1; then echo 'shasum -a 256' elif command -v openssl >/dev/null 2>&1; then echo 'openssl dgst -sha256 -r' else fail "need sha256sum, shasum or openssl to verify the download" fi } download() { # curl only: it can refuse a redirect from https to http (--proto), which wget cannot for a # single file. macOS and most Linux distributions ship curl. case "$1" in https://*) curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1" || fail "download failed: $1" ;; *) curl -fsSL -o "$2" "$1" || fail "download failed: $1" ;; esac } main "$@"