#!/bin/sh # fedtrustctl installer for macOS and Linux. # # curl -fsSL https://federatedtrust.com/install.sh | sh # # Downloads the published fedtrustctl for this computer, checks its SHA-256 # against the published release index, and installs it for the current user at # ~/.local/bin/fedtrustctl. It never uses sudo, and it installs nothing unless # the checksum matches. # # Environment: # FEDTRUSTCTL_INSTALL_DIR Directory to install into (default: ~/.local/bin). # FEDTRUSTCTL_INSTALL_ORIGIN Origin to download from (default: # https://federatedtrust.com). For testing a mirror. # # POSIX sh. The whole script is one function called on the last line, so a # download that stops part-way runs nothing. set -eu PROGRAM="fedtrustctl" DEFAULT_ORIGIN="https://federatedtrust.com" INDEX_PATH="/downloads/fedtrustctl/latest.json" DOWNLOAD_PREFIX="/downloads/fedtrustctl" INDEX_SCHEMA="federatedtrust.fedtrustctl-release/v1" TMP_INDEX="" TMP_FILE="" say() { printf '%s\n' "$*" } fail() { printf '%s install: %s\n' "$PROGRAM" "$*" >&2 exit 1 } cleanup() { if [ -n "$TMP_INDEX" ]; then rm -f "$TMP_INDEX"; fi if [ -n "$TMP_FILE" ]; then rm -f "$TMP_FILE"; fi } # Sets PLATFORM to a release platform id, or stops. detect_platform() { os=$(uname -s) arch=$(uname -m) case "$os" in Darwin) case "$arch" in arm64) PLATFORM="macos-arm64" ;; x86_64) # A translated (Rosetta) shell on Apple silicon reports x86_64. if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = "1" ]; then PLATFORM="macos-arm64" else PLATFORM="macos-x64" fi ;; *) fail "there is no build for macOS on $arch." ;; esac ;; Linux) if (ldd --version 2>&1 || true) | grep -qi musl; then fail "there is no build for musl-based Linux (such as Alpine); the Linux builds need glibc." fi case "$arch" in x86_64 | amd64) PLATFORM="linux-x64" ;; aarch64 | arm64) PLATFORM="linux-arm64" ;; *) fail "there is no build for Linux on $arch." ;; esac ;; *) fail "there is no build for $os. On Windows, run this in PowerShell instead: irm $DEFAULT_ORIGIN/install.ps1 | iex" ;; esac } # fetch fetch() { if command -v curl >/dev/null 2>&1; then case "$1" in https://*) curl -fsSL --proto '=https' --proto-redir '=https' -o "$2" "$1" ;; *) curl -fsSL -o "$2" "$1" ;; esac elif command -v wget >/dev/null 2>&1; then wget -q -O "$2" "$1" else fail "curl or wget is required." fi } # sha256_of prints the lowercase hex SHA-256 of the file. sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 -r "$1" | cut -d ' ' -f 1 else fail "sha256sum, shasum or openssl is required to check the download." fi } # The index is served re-encoded from a validated value: flat objects whose # strings never contain a brace. One object per line, then pick this platform's. index_records() { tr -d ' \t\r\n' <"$1" | tr '{}' '\n\n' } # index_sha256 index_sha256() { index_records "$1" | grep "\"platform\":\"$2\"" | sed -n 's/.*"sha256":"\([0-9a-f]\{64\}\)".*/\1/p' | head -n 1 } # index_version index_version() { index_records "$1" | sed -n 's/.*"version":"\([0-9][0-9.]*\)".*/\1/p' | head -n 1 } main() { origin="${FEDTRUSTCTL_INSTALL_ORIGIN:-$DEFAULT_ORIGIN}" origin="${origin%/}" if [ -n "${FEDTRUSTCTL_INSTALL_DIR:-}" ]; then install_dir="$FEDTRUSTCTL_INSTALL_DIR" else [ -n "${HOME:-}" ] || fail "HOME is not set; set FEDTRUSTCTL_INSTALL_DIR to choose where to install." install_dir="$HOME/.local/bin" fi PLATFORM="" detect_platform mkdir -p "$install_dir" || fail "could not create $install_dir." destination="$install_dir/$PROGRAM" trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM # Both temporary files live beside the destination, so the final step is a rename on one filesystem. TMP_INDEX="$install_dir/.$PROGRAM.$$.index" TMP_FILE="$install_dir/.$PROGRAM.$$.download" fetch "$origin$INDEX_PATH" "$TMP_INDEX" || fail "could not read the release index at $origin$INDEX_PATH. No release may be published yet. Nothing was installed." grep -q "\"schema\":[[:space:]]*\"$INDEX_SCHEMA\"" "$TMP_INDEX" || fail "the release index at $origin$INDEX_PATH is not one this installer understands. Nothing was installed." version=$(index_version "$TMP_INDEX") expected=$(index_sha256 "$TMP_INDEX" "$PLATFORM") [ -n "$expected" ] || fail "the published release has no build for $PLATFORM. Nothing was installed." say "Downloading $PROGRAM ${version:-} for $PLATFORM..." fetch "$origin$DOWNLOAD_PREFIX/$PLATFORM" "$TMP_FILE" || fail "the download from $origin$DOWNLOAD_PREFIX/$PLATFORM failed. Nothing was installed." actual=$(sha256_of "$TMP_FILE") if [ "$actual" != "$expected" ]; then fail "checksum mismatch for $PLATFORM: the release index says $expected but the download is ${actual:-unreadable}. Nothing was installed." fi chmod 755 "$TMP_FILE" mv -f "$TMP_FILE" "$destination" || fail "could not write $destination. Nothing was installed." TMP_FILE="" say "Installed $PROGRAM ${version:-} to $destination (SHA-256 $expected)." case ":${PATH:-}:" in *":$install_dir:"*) say "Next: $PROGRAM resources" ;; *) say "$install_dir is not on your PATH. Add it for new shells with:" say " export PATH=\"$install_dir:\$PATH\"" say "Next: $destination resources" ;; esac } main "$@"