#!/bin/sh set -eu error() { printf 'trackstore: %s\n' "$*" >&2 exit 1 } download() { if command -v curl >/dev/null 2>&1; then curl -fsSL "$1" -o "$2" || error "download failed: $1" elif command -v wget >/dev/null 2>&1; then wget -qO- "$1" >"$2" || error "download failed: $1" else error "curl or wget is required" fi } cleanup() { if [ -n "$tmp_dir" ]; then rm -rf "$tmp_dir" fi if [ -n "$staging" ]; then rm -f "$staging" fi } main() { base=${TRACKSTORE_BASE_URL:-https://monorail.sh/trackstore} system=$(uname -s) || error "could not determine operating system" machine=$(uname -m) || error "could not determine machine architecture" case "$system" in Darwin) os=darwin ;; Linux) os=linux ;; *) error "unsupported platform $system/$machine; binaries exist for darwin-arm64, darwin-x64, linux-arm64, linux-x64" ;; esac case "$machine" in arm64|aarch64) arch=arm64 ;; x86_64|amd64) arch=x64 ;; *) error "unsupported platform $system/$machine; binaries exist for darwin-arm64, darwin-x64, linux-arm64, linux-x64" ;; esac tmp_dir= staging= trap cleanup 0 trap 'exit 1' 1 2 15 tmp_dir=$(mktemp -d) || error "could not create temporary directory" version=${TRACKSTORE_VERSION:-} if [ -n "$version" ]; then version=${version#v} else download "$base/latest.json" "$tmp_dir/latest.json" version=$(sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$tmp_dir/latest.json" | sed -n '1p') fi case "$version" in ''|*[!0-9A-Za-z.+_-]*) error "could not determine a valid release version" ;; esac filename=trackstore-$os-$arch download "$base/v$version/$filename" "$tmp_dir/$filename" download "$base/v$version/SHA256SUMS" "$tmp_dir/SHA256SUMS" expected_hash= while IFS=' ' read -r hash listed_file; do case "$listed_file" in "$filename"|\*"$filename") expected_hash=$hash break ;; esac done <"$tmp_dir/SHA256SUMS" [ -n "$expected_hash" ] || error "checksum for $filename not found" if command -v sha256sum >/dev/null 2>&1; then hash_output=$(sha256sum "$tmp_dir/$filename") || error "could not compute checksum" elif command -v shasum >/dev/null 2>&1; then hash_output=$(shasum -a 256 "$tmp_dir/$filename") || error "could not compute checksum" else error "sha256sum or shasum is required" fi actual_hash=${hash_output%% *} [ "$actual_hash" = "$expected_hash" ] || error "checksum mismatch for $filename" if [ -n "${TRACKSTORE_INSTALL_DIR:-}" ]; then dir=$TRACKSTORE_INSTALL_DIR else home=${HOME:-} [ -n "$home" ] || error "HOME is not set" dir=$home/.local/bin fi mkdir -p "$dir" || error "could not create install directory $dir" staging=$dir/.trackstore.$$ cp "$tmp_dir/$filename" "$staging" || error "could not copy binary to $staging" chmod 755 "$staging" || error "could not set executable permissions on $staging" mv -f "$staging" "$dir/trackstore" || error "could not install $dir/trackstore" staging= case ":${PATH:-}:" in *":$dir:"*) ;; *) printf "trackstore: warning: %s is not on PATH; add:\n export PATH=\"%s:\$PATH\"\n" "$dir" "$dir" >&2 ;; esac "$dir/trackstore" version } main "$@"