#!/bin/sh
# Installer for azw, the Azwright compiler.
#
# curl -fsSL https://azwright.com/install.sh | sh
#
# Installs one static binary to ${AZWRIGHT_HOME:-$HOME/.azwright}/bin/azw. It never uses sudo and never
# edits shell start-up files. Running it again upgrades in place.
#
# Environment:
# AZWRIGHT_VERSION version to install (default: the content of /releases/latest)
# AZWRIGHT_HOME where to install (default: $HOME/.azwright)
# AZWRIGHT_DIST_URL base URL of the release files (default: https://azwright.com). Must be https://,
# except that http:// is accepted for 127.0.0.1 and localhost so that the installer
# can be tested against a local server.
# AZWRIGHT_REQUIRE_SIGNATURE
# set to 1 to refuse to install when the signature cannot be checked because this
# machine has no `ssh-keygen` that can verify one (OpenSSH 8.1 or later). Without it
# the installer prints a warning and goes on with the checksum alone. A signature
# that is wrong or missing is refused in every case.
#
# Uninstall: rm -rf ~/.azwright (and remove the PATH line you added)
#
# Details: docs/install.md in the compiler repository.
#
# The whole script is one function that is called on the last line, so a download that was cut short
# defines a function (or fails to parse) and runs nothing.
set -eu
# The release keys: every release archive is signed with one of them (scripts/sign-release.sh), and the
# private halves are never on the web server. One ` ` per line; several lines allow a key
# to be replaced. The same lines are in keys/release.pub of the compiler repository.
release_keys='
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOEPdjuWr8j9iiej05MQw1H59qUpeh6MwFy9StRmFJPQ
'
signer=release@azwright.com
namespace=azwright-release
say() {
printf '%s\n' "$*"
}
fail() {
printf 'install.sh: error: %s\n' "$*" >&2
exit 1
}
have() {
command -v "$1" >/dev/null 2>&1
}
# check_base : the base URL must be https://, or http:// on this machine only.
# Sets $transport to `https` or `http`.
check_base() {
case "$1" in
https://?*)
transport=https
;;
http://?*)
authority=${1#http://}
authority=${authority%%/*}
case "$authority" in
127.0.0.1 | localhost) ;;
127.0.0.1:* | localhost:*)
port=${authority#*:}
case "$port" in
'' | *[!0-9]*) fail "AZWRIGHT_DIST_URL must use https:// (plain http:// is only accepted for 127.0.0.1 and localhost): $1" ;;
esac
;;
*) fail "AZWRIGHT_DIST_URL must use https:// (plain http:// is only accepted for 127.0.0.1 and localhost): $1" ;;
esac
transport=http
;;
*)
fail "AZWRIGHT_DIST_URL must be an https:// URL: $1"
;;
esac
}
# fetch : download with curl or wget. Only the scheme chosen by check_base is allowed,
# also after redirects.
fetch() {
if [ "$downloader" = curl ]; then
if [ "$transport" = https ]; then
curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 2 -o "$2" "$1"
else
curl --proto '=http' --proto-redir '=http' -fsSL -o "$2" "$1"
fi
else
if [ "$transport" = https ] && [ "$wget_https_only" = yes ]; then
wget -q --https-only -O "$2" "$1"
else
wget -q -O "$2" "$1"
fi
fi
}
# sha256_of : prints the SHA-256 of the file as 64 lower-case hex digits.
sha256_of() {
if have sha256sum; then
sha256sum "$1" | cut -d ' ' -f 1
else
shasum -a 256 "$1" | cut -d ' ' -f 1
fi
}
# can_verify: true if this machine has an ssh-keygen that knows `-Y verify` (OpenSSH 8.1 or later).
# Only the "unknown option" answer of an old ssh-keygen counts as "cannot": anything else is taken as
# "can", so that an unexpected answer leads to a failed verification, not to a skipped one.
can_verify() {
have ssh-keygen || return 1
probe=$(ssh-keygen -Y verify 2>&1 || true)
case "$probe" in
*"option -- "*) return 1 ;;
esac
return 0
}
# verify_signature : true if the signature is one of $release_keys over the file.
verify_signature() {
printf '%s\n' "$release_keys" | while read -r keytype keydata _; do
if [ -n "$keytype" ]; then
printf '%s namespaces="%s" %s %s\n' "$signer" "$namespace" "$keytype" "$keydata"
fi
done >"$tmp/allowed_signers"
ssh-keygen -Y verify -f "$tmp/allowed_signers" -I "$signer" -n "$namespace" -s "$2" <"$1" >"$tmp/verify.log" 2>&1
}
cleanup() {
if [ -n "${tmp:-}" ]; then
rm -rf "$tmp"
fi
if [ -n "${staged:-}" ]; then
rm -f "$staged"
fi
}
main() {
base=${AZWRIGHT_DIST_URL:-https://azwright.com}
base=${base%/}
check_base "$base"
# Platform. Only Linux x86_64 has a prebuilt binary for now.
os=$(uname -s)
arch=$(uname -m)
case "$os" in
Linux) ;;
*) fail "there is no prebuilt azw for $os $arch yet. Only Linux x86_64 is supported for now." ;;
esac
case "$arch" in
x86_64 | amd64) platform=x86_64-linux ;;
*) fail "there is no prebuilt azw for $os $arch yet. Only Linux x86_64 is supported for now." ;;
esac
# Where to install.
if [ -n "${AZWRIGHT_HOME:-}" ]; then
home=$AZWRIGHT_HOME
elif [ -n "${HOME:-}" ]; then
home=$HOME/.azwright
else
fail "neither AZWRIGHT_HOME nor HOME is set, so there is no place to install to"
fi
case "$home" in
/*) ;;
*) fail "AZWRIGHT_HOME must be an absolute path: $home" ;;
esac
bin=$home/bin
# Tools.
if have curl; then
downloader=curl
elif have wget; then
downloader=wget
wget_https_only=no
if wget --help 2>&1 | grep -q -e '--https-only'; then
wget_https_only=yes
fi
else
fail "curl or wget is required to download azw"
fi
if ! have sha256sum && ! have shasum; then
fail "sha256sum or shasum is required to verify the download; refusing to install without a checksum"
fi
for tool in tar gzip mktemp; do
have "$tool" || fail "$tool is required"
done
tmp=
staged=
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
tmp=$(mktemp -d "${TMPDIR:-/tmp}/azwright-install.XXXXXX")
# Version.
version=${AZWRIGHT_VERSION:-}
if [ -z "$version" ]; then
fetch "$base/releases/latest" "$tmp/latest" || fail "cannot download $base/releases/latest"
version=$(tr -d ' \t\r\n' <"$tmp/latest")
fi
version=${version#v}
case "$version" in
'' | *[!0-9A-Za-z.+-]* | .* | *..*) fail "this does not look like a version: '$version'" ;;
esac
name=azw-$version-$platform
url=$base/releases/$version/$name.tar.gz
say "downloading $url"
fetch "$url" "$tmp/$name.tar.gz" || fail "cannot download $url (is $version a released version?)"
fetch "$url.sha256" "$tmp/$name.tar.gz.sha256" || fail "cannot download $url.sha256"
# Checksum: the first word of the .sha256 file (the format `sha256sum` writes).
want=$(sed -n '1{s/[[:space:]].*$//;p;}' "$tmp/$name.tar.gz.sha256" | tr 'A-F' 'a-f')
case "$want" in
*[!0-9a-f]*) fail "$url.sha256 does not contain a SHA-256 checksum" ;;
esac
if [ "${#want}" -ne 64 ]; then
fail "$url.sha256 does not contain a SHA-256 checksum"
fi
got=$(sha256_of "$tmp/$name.tar.gz")
if [ "$got" != "$want" ]; then
fail "checksum mismatch for $name.tar.gz (expected $want, got $got); nothing was installed"
fi
# Signature: made with a key that is not on the server, so it also catches an archive and a checksum
# that were replaced together. The archive is not unpacked and nothing in it runs before this passes.
if can_verify; then
fetch "$url.sig" "$tmp/$name.tar.gz.sig" || fail "cannot download $url.sig; a release without a signature is refused, nothing was installed"
if ! verify_signature "$tmp/$name.tar.gz" "$tmp/$name.tar.gz.sig"; then
fail "the signature of $name.tar.gz is not valid (it was not made with a release key); nothing was installed"
fi
say "signature verified"
elif [ "${AZWRIGHT_REQUIRE_SIGNATURE:-}" = 1 ]; then
fail "the signature cannot be checked: ssh-keygen with \`-Y verify\` (OpenSSH 8.1 or later) is not installed, and AZWRIGHT_REQUIRE_SIGNATURE=1 is set; nothing was installed"
else
printf 'install.sh: warning: the signature was not verified: ssh-keygen with `-Y verify` (OpenSSH 8.1 or later) is not installed. Only the checksum was checked. Set AZWRIGHT_REQUIRE_SIGNATURE=1 to refuse instead.\n' >&2
fi
mkdir "$tmp/unpacked"
tar -xzf "$tmp/$name.tar.gz" -C "$tmp/unpacked" || fail "cannot unpack $name.tar.gz"
[ -f "$tmp/unpacked/$name/azw" ] || fail "$name.tar.gz does not contain $name/azw"
# Install. The new binary is copied next to the old one under a temporary name, tried there, and
# then renamed over it, so `azw` is always either the old or the new complete file.
# `cp` (not `mv` out of the archive) gives the file the time of the installation: azw keys its build
# caches on its own size and modification time, and archive members all carry the release time.
mkdir -p "$bin" || fail "cannot create $bin"
staged=$bin/.azw.new.$$
cp "$tmp/unpacked/$name/azw" "$staged" || fail "cannot write to $bin"
chmod 755 "$staged"
reported=$("$staged" --version) || fail "the downloaded azw does not run on this machine; nothing was installed"
case "$reported" in
"azw $version "*) ;;
*) fail "the downloaded azw reports '$reported', expected version $version; nothing was installed" ;;
esac
mv -f "$staged" "$bin/azw"
staged=
for doc in LICENSE THIRD-PARTY.md; do
if [ -f "$tmp/unpacked/$name/$doc" ]; then
cp "$tmp/unpacked/$name/$doc" "$home/$doc"
fi
done
# The notices of the third-party code linked into azw (THIRD-PARTY.md names them). The directory is
# replaced as a whole, so that it never mixes the notices of two versions.
if [ -d "$tmp/unpacked/$name/licenses" ]; then
rm -rf "$home/licenses.new.$$"
if cp -R "$tmp/unpacked/$name/licenses" "$home/licenses.new.$$"; then
rm -rf "$home/licenses"
mv "$home/licenses.new.$$" "$home/licenses"
else
rm -rf "$home/licenses.new.$$"
fi
fi
say "installed $reported"
say " $bin/azw"
case ":${PATH:-}:" in
*":$bin:"*) ;;
*)
if [ -n "${HOME:-}" ] && [ "$home" = "$HOME/.azwright" ]; then
# shellcheck disable=SC2016
line='export PATH="$HOME/.azwright/bin:$PATH"'
else
line="export PATH=\"$bin:\$PATH\""
fi
say ""
say "$bin is not on your PATH. Add this line to your shell start-up file"
say "(~/.bashrc, ~/.zshrc, ~/.profile, ...) and open a new terminal:"
say ""
say " $line"
;;
esac
if [ -n "${AZWRIGHT_HOME:-}" ] && [ -n "${HOME:-}" ] && [ "$home" != "$HOME/.azwright" ]; then
say ""
say "note: azw keeps its build caches in \$AZWRIGHT_HOME too (default ~/.azwright)."
say " Keep AZWRIGHT_HOME=$home set when you run azw if you want them in the same place."
fi
say ""
say "Next: azw guide (the language guide)"
}
main "$@"