#!/bin/sh
# Installs a command line tool of Stellar Systems from get.stellar-systems.eu.
#
#   curl -fsSL https://get.stellar-systems.eu/install.sh | sh
#   curl -fsSL https://get.stellar-systems.eu/install.sh | sh -s -- --version 0.1.0 --dir ~/bin
#
# Options: --product NAME (stellar), --version X.Y.Z (the latest), --dir DIR (~/.local/bin, or
# /usr/local/bin as root). STELLAR_GET_URL changes the site (https://get.stellar-systems.eu).
# The archive is checked against its SHA-256 before anything is installed.
set -eu

base="${STELLAR_GET_URL:-https://get.stellar-systems.eu}"
product=stellar
version=""
if [ "$(id -u)" -eq 0 ]; then dir=/usr/local/bin; else dir="$HOME/.local/bin"; fi

while [ $# -gt 0 ]; do
    case "$1" in
        --product) product="$2"; shift 2 ;;
        --version) version="$2"; shift 2 ;;
        --dir) dir="$2"; shift 2 ;;
        -h|--help) sed -n '2,10p' "$0" 2>/dev/null || true; exit 0 ;;
        *) echo "install.sh: unknown option $1" >&2; exit 2 ;;
    esac
done

say() { printf '%s\n' "$*"; }
fail() { printf 'install.sh: %s\n' "$*" >&2; exit 1; }

if command -v curl >/dev/null 2>&1; then
    fetch() { curl -fsSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
    fetch() { wget -q "$1" -O "$2"; }
else
    fail "curl or wget is needed"
fi
if command -v sha256sum >/dev/null 2>&1; then
    sha256() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
    sha256() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
    fail "sha256sum or shasum is needed to check the download"
fi

case "$(uname -s)" in
    Linux) os=linux ;;
    Darwin) os=macos ;;
    *) fail "$(uname -s) is not supported yet: see $base" ;;
esac
case "$(uname -m)" in
    x86_64|amd64) arch=x86_64 ;;
    aarch64|arm64) arch=aarch64 ;;
    *) fail "$(uname -m) is not supported yet: see $base" ;;
esac
platform="$os-$arch"

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM

if [ -z "$version" ]; then
    fetch "$base/$product/latest" "$tmp/latest" || fail "no release of $product on $base"
    version="$(tr -d ' \r\n' <"$tmp/latest")"
fi
archive="$product-$version-$platform.tar.gz"
url="$base/$product/$version/$archive"

say "Installing $product $version ($platform) into $dir"
fetch "$url" "$tmp/$archive" || fail "no $product $version for $platform: see $base"
fetch "$url.sha256" "$tmp/$archive.sha256" || fail "no checksum for $archive"
expected="$(cut -d' ' -f1 <"$tmp/$archive.sha256")"
actual="$(sha256 "$tmp/$archive")"
[ "$expected" = "$actual" ] || fail "checksum mismatch for $archive: $actual, expected $expected"

tar -xzf "$tmp/$archive" -C "$tmp"
mkdir -p "$dir"
install -m 0755 "$tmp/$product-$version-$platform/$product" "$dir/$product" 2>/dev/null \
    || { cp "$tmp/$product-$version-$platform/$product" "$dir/$product" && chmod 0755 "$dir/$product"; }

say "Installed: $("$dir/$product" --version 2>/dev/null || echo "$dir/$product")"
case ":$PATH:" in
    *":$dir:"*) ;;
    *) say "Add $dir to your PATH: export PATH=\"$dir:\$PATH\"" ;;
esac
