#!/bin/sh # Installs Larva, the toolchain of the Antril language, on Linux and macOS: # # curl -fsSL https://antril.org/install.sh | sh # curl -fsSL https://antril.org/install.sh | sh -s -- --no-path # # It reads the latest release from https://antril.org/download/latest.json, downloads Larva---.tar.gz # into a temporary folder, checks its SHA-256, unpacks it and runs the install.sh inside with the options given here: # --prefix , --no-path, --no-hive, --no-types (that install.sh's --help lists them). Larva goes to your user, # no root needed. # # Settings: # --dry-run or LARVA_INSTALL_DRYRUN=1 download, check and unpack, then stop before installing # LARVA_INSTALL_OS, LARVA_INSTALL_ARCH another system's build (linux or osx; x64 or arm64) set -eu main() { base=${LARVA_INSTALL_BASE:-https://antril.org} dry=${LARVA_INSTALL_DRYRUN:-0} [ "$dry" = 0 ] && dry=no || dry=yes # --dry-run is this script's; every other option goes to Larva's install.sh. for option do shift if [ "$option" = --dry-run ]; then dry=yes; else set -- "$@" "$option"; fi done case "${LARVA_INSTALL_OS:-$(uname -s)}" in Linux|linux) os=linux ;; Darwin|osx|macos) os=osx ;; *) fail "Larva's archives are for Linux and macOS (this is $(uname -s)). On Windows, in PowerShell: irm $base/install.ps1 | iex" ;; esac case "${LARVA_INSTALL_ARCH:-$(uname -m)}" in x86_64|amd64|x64) arch=x64 ;; aarch64|arm64) arch=arm64 ;; *) fail "no Larva build for the $(uname -m) processor" ;; esac if command -v curl > /dev/null 2>&1; then fetch() { curl -fsSL -H 'Cache-Control: no-cache' "$1"; } download() { curl -fSL --progress-bar -o "$2" "$1"; } elif command -v wget > /dev/null 2>&1; then fetch() { wget -q -O - "$1"; } download() { wget -q -O "$2" "$1"; } else fail "curl or wget is needed" fi if command -v sha256sum > /dev/null 2>&1; then sha256() { sha256sum "$1" | cut -d ' ' -f 1; } elif command -v shasum > /dev/null 2>&1; then sha256() { shasum -a 256 "$1" | cut -d ' ' -f 1; } else fail "sha256sum or shasum is needed to check the download" fi command -v tar > /dev/null 2>&1 || fail "tar is needed" step "Looking for the latest Larva at $base" latest=$(fetch "$base/download/latest.json") || fail "could not read $base/download/latest.json" version=$(printf '%s\n' "$latest" | sed -n 's/^ "version": *"\([^"]*\)".*/\1/p' | head -n 1) [ -n "$version" ] || fail "$base/download/latest.json names no version" name="Larva-$version-$os-$arch.tar.gz" # latest.json has one property per line; each asset's name comes before its sha256 and url. found=$(printf '%s\n' "$latest" | awk -v want="$name" ' /"name":/ { n = $0; sub(/.*"name": *"/, "", n); sub(/".*/, "", n) } /"sha256":/ && n == want { s = $0; sub(/.*"sha256": *"/, "", s); sub(/".*/, "", s); print "sha256 " s } /"url":/ && n == want { u = $0; sub(/.*"url": *"/, "", u); sub(/".*/, "", u); print "url " u }') url=$(printf '%s\n' "$found" | sed -n 's/^url //p') expected=$(printf '%s\n' "$found" | sed -n 's/^sha256 //p') [ -n "$url" ] || fail "Larva $version has no build for $os-$arch. See $base/download" [ -n "$expected" ] || fail "$name has no SHA-256 to check it against. See $base/download" folder=$(mktemp -d 2> /dev/null || mktemp -d -t larva-install) trap 'rm -rf "$folder"' EXIT trap 'exit 130' INT TERM step "Downloading $name" download "$url" "$folder/$name" || fail "could not download $url" step "Checking its SHA-256" actual=$(sha256 "$folder/$name") if [ "$actual" != "$expected" ]; then fail "the SHA-256 of $name is $actual, not $expected: the download is damaged or not Larva's. Nothing was installed." fi say " $actual ok" step "Unpacking" tar -xzf "$folder/$name" -C "$folder" || fail "could not unpack $name" installer="$folder/Larva-$version-$os-$arch/install.sh" [ -f "$installer" ] || installer=$(find "$folder" -name install.sh -type f | head -n 1) [ -n "$installer" ] && [ -f "$installer" ] || fail "$name holds no install.sh" if [ "$dry" = yes ]; then step "Dry run: would run $(basename "$(dirname "$installer")")/install.sh $*" say "Nothing was installed (--dry-run)." return 0 fi step "Installing Larva $version" sh "$installer" "$@" || fail "Larva's install.sh failed" say "" say "Next: larva new Console-App Hello --lang antril && cd Hello && larva run" say "(Open a new terminal first if larva is not found: ~/.local/bin was added to your PATH.)" say "Documentation: $base/docs" } say() { printf '%s\n' "$*"; } step() { printf '==> %s\n' "$*"; } fail() { printf 'error: %s\n' "$*" >&2; exit 1; } # Everything runs from here: a download cut short runs nothing. main "$@"