12# install.sh - installs a released 012 binary on macOS or Linux. Served3# at the docs site's root with the release archives under /releases/;4# see docs/getting-started/install.md.5#6# curl -fsSL https://012.dev.site/install.sh | sh7# curl -fsSL https://012.dev.site/install.sh | sh -s -- --system8#9# It picks the archive for this OS and architecture, checks its SHA25610# against the release's SHA256SUMS, and puts 012 in ~/.local/bin, in11# $PREFIX/bin when PREFIX is set, or in /usr/local/bin with --system.12# sudo is only run for --system when that directory isn't writable, and13# the command is printed first.14#15# --version v1.2.3 a release other than the latest (or O12_VERSION)16# --system install into /usr/local/bin17# O12_BASE_URL the site serving /releases/ (https://012.dev.site)18set -eu1920base=${O12_BASE_URL:-https://012.dev.site}21version=${O12_VERSION:-latest}22system=02324say() { printf '012 install: %s\n' "$*"; }25die() {26 printf '012 install: %s\n' "$*" >&227 exit 128}2930while [ $# -gt 0 ]; do31 case $1 in32 --version)33 [ $# -ge 2 ] || die "--version needs a version, like v1.2.3"34 version=$235 shift 236 ;;37 --version=*)38 version=${1#--version=}39 shift40 ;;41 --system)42 system=143 shift44 ;;45 -h | --help)46 sed -n '2,17p' "$0" 2>/dev/null | sed 's/^# \{0,1\}//' || true47 exit 048 ;;49 *) die "unknown option $1 (--version v1.2.3, --system)" ;;50 esac51done5253case $version in54latest | v[0-9]*) ;;55[0-9]*) version=v$version ;;56*) die "--version must be a release like v1.2.3 (got $version)" ;;57esac5859os=$(uname -s)60case $os in61Darwin) os=darwin ;;62Linux) os=linux ;;63MINGW* | MSYS* | CYGWIN* | Windows*)64 die "on Windows, run in PowerShell: irm $base/install.ps1 | iex"65 ;;66*) die "no release for $os; build from source: go install src.rest/cat/012/cmd/012@latest" ;;67esac6869arch=$(uname -m)70case $arch in71x86_64 | amd64) arch=amd64 ;;72arm64 | aarch64) arch=arm64 ;;73*) die "no release for $arch; build from source: go install src.rest/cat/012/cmd/012@latest" ;;74esac75# A shell under Rosetta reports x86_64 on an Apple silicon Mac.76if [ "$os/$arch" = darwin/amd64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ]; then77 arch=arm6478fi7980if command -v curl >/dev/null 2>&1; then81 fetch() { curl -fsSL -o "$2" "$1"; }82elif command -v wget >/dev/null 2>&1; then83 fetch() { wget -q -O "$2" "$1"; }84else85 die "needs curl or wget"86fi8788if command -v sha256sum >/dev/null 2>&1; then89 sha256() { sha256sum "$1" | cut -d ' ' -f 1; }90elif command -v shasum >/dev/null 2>&1; then91 sha256() { shasum -a 256 "$1" | cut -d ' ' -f 1; }92elif command -v openssl >/dev/null 2>&1; then93 sha256() { openssl dgst -sha256 -r "$1" | cut -d ' ' -f 1; }94else95 die "needs sha256sum, shasum or openssl to check the download"96fi9798if [ "$system" = 1 ]; then99 bindir=/usr/local/bin100elif [ -n "${PREFIX:-}" ]; then101 bindir=$PREFIX/bin102else103 [ -n "${HOME:-}" ] || die "HOME is not set; set PREFIX to install into \$PREFIX/bin"104 bindir=$HOME/.local/bin105fi106107tmp=$(mktemp -d 2>/dev/null || mktemp -d -t 012install)108trap 'rm -rf "$tmp"' EXIT109trap 'exit 1' INT TERM110111url=$base/releases/$version112fetch "$url/SHA256SUMS" "$tmp/SHA256SUMS" || die "couldn't download $url/SHA256SUMS"113114# A line of SHA256SUMS: the digest, two spaces, 012_1.2.3_linux_amd64.tar.gz.115line=$(grep " 012_[^ ]*_${os}_${arch}\.tar\.gz\$" "$tmp/SHA256SUMS" | head -n 1 || true)116[ -n "$line" ] || die "release $version has no archive for $os/$arch"117want=${line%% *}118file=${line##* }119dir=${file%.tar.gz}120got_version=${dir#012_}121got_version=v${got_version%_"${os}"_"${arch}"}122123say "downloading $url/$file"124fetch "$url/$file" "$tmp/$file" || die "couldn't download $url/$file"125got=$(sha256 "$tmp/$file")126[ "$got" = "$want" ] || die "$file has SHA256 $got, SHA256SUMS says $want; nothing installed"127say "SHA256 checked against SHA256SUMS: $want"128129tar -xzf "$tmp/$file" -C "$tmp" || die "couldn't unpack $file"130[ -f "$tmp/$dir/012" ] || die "$file holds no 012 binary"131132if [ ! -d "$bindir" ] && ! mkdir -p "$bindir" 2>/dev/null; then133 [ "$system" = 1 ] || die "couldn't create $bindir"134fi135if [ -d "$bindir" ] && [ -w "$bindir" ]; then136 cp "$tmp/$dir/012" "$bindir/012.new"137 chmod 755 "$bindir/012.new"138 mv -f "$bindir/012.new" "$bindir/012"139elif [ "$system" = 1 ]; then140 command -v sudo >/dev/null 2>&1 || die "$bindir isn't writable and there is no sudo; run as root or leave out --system"141 say "$bindir isn't writable by $(id -un); running: sudo install -m 755 012 $bindir/012"142 sudo mkdir -p "$bindir"143 sudo install -m 755 "$tmp/$dir/012" "$bindir/012"144else145 die "$bindir isn't writable; set PREFIX to another directory"146fi147148say "installed 012 $got_version ($os/$arch) as $bindir/012"149case ":${PATH:-}:" in150*":$bindir:"*) say "run 012 to start" ;;151*) say "$bindir is not on your PATH; add it (export PATH=\"$bindir:\$PATH\") or run $bindir/012" ;;152esac