#!/bin/sh # Installs the somaro CLI from https://somaro.ai into ~/.somaro/bin. set -eu os=$(uname -s | tr '[:upper:]' '[:lower:]') arch=$(uname -m) case "$arch" in x86_64|amd64) arch=amd64;; arm64|aarch64) arch=arm64;; *) echo "unsupported architecture: $arch" >&2; exit 1;; esac case "$os" in darwin|linux) ;; *) echo "unsupported OS: $os (Windows, in PowerShell: irm https://somaro.ai/install.ps1 | iex)" >&2; exit 1;; esac dir="${SOMARO_BIN:-$HOME/.somaro/bin}" mkdir -p "$dir" url="https://somaro.ai/cli/somaro-$os-$arch" # gzipped, less than half the bytes (somaro#110); the plain binary when it is # not there. To a file first: a retried transfer cannot then be cut into # gunzip. A 5xx or a cut connection is retried, and a terminal sees the # progress (somaro#118). if command -v curl >/dev/null; then # no meter in a log, but the retries are told if [ -t 2 ]; then show=--progress-bar; elif curl --help all 2>/dev/null | grep -q -- --no-progress-meter; then show=--no-progress-meter; else show=-sS; fi retry="--retry 3" if curl --help all 2>/dev/null | grep -q -- --retry-all-errors; then retry="$retry --retry-all-errors"; fi get() { curl -fL $show $retry -o "$2" "$1"; } else get() { wget -q --tries=3 -O "$2" "$1"; } fi case "$os-$arch" in darwin-amd64) size="6.3 MB"; sum=1deeca14067db5604ba4ee09f1cba5a5cf5646975a2d9729bdabf829af022b33;; darwin-arm64) size="5.9 MB"; sum=ab97f43a35b003489e135fde52343921eca39c3bd66bca5aae295b8a01161418;; linux-amd64) size="6.3 MB"; sum=c4f43efaeef89bf4f27ebf7ffae954e5fe6527be6ad63e19b824537afef27fdd;; linux-arm64) size="5.8 MB"; sum=f659e974f815470143093701e2212e3e7825600fc3d3b52c946f9055e238c1e4;; *) size=""; sum="";; esac # what was fetched is checked against the sum the server wrote here sumof() { if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; elif command -v shasum >/dev/null; then shasum -a 256 "$1" | cut -d' ' -f1; fi; } check() { [ -n "$2" ] || return 0 got=$(sumof "$1") if [ -n "$got" ] && [ "$got" != "$2" ]; then rm -f "$1" echo "the download's checksum is $got, the server's $2: nothing installed; run the line again" >&2 exit 1 fi } echo "downloading $url.gz${size:+ ($size; through a slow link this can take a few minutes)}" if get "$url.gz" "$dir/somaro.gz"; then check "$dir/somaro.gz" "$sum" gunzip -c "$dir/somaro.gz" > "$dir/somaro.new" rm -f "$dir/somaro.gz" else rm -f "$dir/somaro.gz" echo "downloading $url" get "$url" "$dir/somaro.new" fi [ -s "$dir/somaro.new" ] || { rm -f "$dir/somaro.new"; echo "the download is empty: nothing installed; run the line again" >&2; exit 1; } chmod 755 "$dir/somaro.new" && mv "$dir/somaro.new" "$dir/somaro" echo "installed $dir/somaro ($("$dir/somaro" version 2>/dev/null || echo somaro))" case ":$PATH:" in *":$dir:"*) ;; *) echo "add it to PATH: export PATH=\"$dir:\$PATH\"";; esac # The server this came from is remembered beside the passport, where the # CLI looks when SOMARO_API is not set: an agent's shell keeps no variable # from one call to the next (#273). if mkdir -p "$HOME/.somaro" && printf '%s\n' "https://somaro.ai" > "$HOME/.somaro/server"; then echo "this server, https://somaro.ai, is remembered in $HOME/.somaro/server: somaro talks to it with nothing to export" if [ -n "${SOMARO_API:-}" ] && [ "$SOMARO_API" != "https://somaro.ai" ]; then echo "SOMARO_API is set in this shell to $SOMARO_API, and where it is set it is used instead" fi else echo "the server could not be remembered in $HOME/.somaro/server: export SOMARO_API=https://somaro.ai in every shell that runs somaro" fi echo "then: somaro manual"