#!/usr/bin/env bash # Install a Gale compiler release. Requires Bash 3.2+, curl and SHA-256. # Keep the implementation in a function so a truncated curl pipe cannot run it. gale_install() ( set -euo pipefail gale_version=latest gale_version_option=0 gale_uninstall=0 gale_runtime=auto gale_proof=auto gale_profile=auto gale_yes=0 gale_setup=0 gale_base_url=${GALE_DOWNLOAD_BASE_URL:-https://downloads.gale-lang.org} gale_install_dir=${GALE_INSTALL_DIR:-${HOME:?HOME is required}/.local/bin} gale_tmp= gale_staged= gale_manifest_staged= gale_fail() { printf 'gale: %s\n' "$*" >&2; exit 1; } gale_usage() { cat <<'USAGE' Install Gale for Linux x86_64 or macOS ARM64. curl -fsSL https://gale-lang.org/install.sh | bash curl -fsSL https://gale-lang.org/install.sh | bash -s -- --version 0.1.0 Options: --version VERSION Select an immutable Gale release (default: latest) --install-dir DIR Destination for gale (default: ~/.local/bin) --with-runtime Install recommended Elixir/OTP if no compatible runtime exists --without-runtime Skip runtime setup --with-prover Install/check the matching adapter, Why3 and Z3 --without-prover Skip proving setup --modify-path Add a marked PATH block to your Bash/Zsh profile --no-modify-path Leave shell profiles unchanged --yes, -y Use defaults without prompting (proving stays opt-in) --uninstall Remove Gale, its managed tools and its unchanged PATH blocks --base-url URL Public R2 download origin; GALE_DOWNLOAD_BASE_URL also works --help Show this help The installer verifies SHA-256 and replaces the compiler atomically. Rerun it with another --version to switch releases. Uninstall removes the matching gale executable and install record from the selected install directory. It does not require sudo for the compiler. Optional dependency setup may use apt/Homebrew. Interactive installs offer runtime, proving and PATH setup. Without a terminal, optional setup is skipped unless selected with flags. Shared system packages and pre-existing tools are retained on uninstall. USAGE } while [ "$#" -gt 0 ]; do case "$1" in --version|--install-dir|--base-url) [ "$#" -ge 2 ] && [ -n "$2" ] || gale_fail "$1 needs a value" case "$1" in --version) gale_version=$2; gale_version_option=1 ;; --install-dir) gale_install_dir=$2 ;; --base-url) gale_base_url=$2 ;; esac shift 2 ;; --with-runtime) gale_runtime=yes; gale_setup=1; shift ;; --without-runtime) gale_runtime=no; shift ;; --with-prover) gale_proof=yes; gale_setup=1; shift ;; --without-prover) gale_proof=no; shift ;; --modify-path) gale_profile=yes; gale_setup=1; shift ;; --no-modify-path) gale_profile=no; shift ;; --yes|-y) gale_yes=1; gale_setup=1; shift ;; --uninstall) gale_uninstall=1; shift ;; --help|-h) gale_usage; exit 0 ;; *) gale_fail "unknown option: $1 (use --help)" ;; esac done gale_valid_version() { [[ "$1" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$ ]] || return 1 if [[ "$1" == *-* ]]; then gale_prerelease=${1#*-} IFS=. read -r -a gale_identifiers <<< "$gale_prerelease" for gale_identifier in "${gale_identifiers[@]}"; do if [[ "$gale_identifier" =~ ^[0-9]+$ && ${#gale_identifier} -gt 1 && "$gale_identifier" == 0* ]]; then return 1 fi done fi } case "$gale_install_dir" in *$'\n'*|*$'\r'*) gale_fail 'install directory must not contain newlines' ;; esac case "$gale_install_dir" in -*) gale_fail 'install directory must not start with -' ;; esac if command -v sha256sum >/dev/null 2>&1; then gale_checksum() { sha256sum "$1"; } elif command -v shasum >/dev/null 2>&1; then gale_checksum() { shasum -a 256 "$1"; } else gale_fail 'required command is missing: sha256sum or shasum' fi if [ -d "$gale_install_dir" ]; then gale_install_dir=$(cd -- "$gale_install_dir" && pwd); fi if [ "$gale_uninstall" -eq 1 ]; then [ "$gale_version_option" -eq 0 ] || gale_fail '--uninstall cannot be combined with --version' command -v rm >/dev/null 2>&1 || gale_fail 'required command is missing: rm' gale_installed="$gale_install_dir/gale" if [ ! -e "$gale_installed" ] && [ ! -L "$gale_installed" ]; then if [ ! -e "$gale_install_dir/.gale-install" ] && [ ! -L "$gale_install_dir/.gale-install" ]; then printf 'Gale is not installed at %s\n' "$gale_installed" exit 0 fi gale_fail "Gale install record exists but the executable is missing: $gale_installed" fi [ -f "$gale_installed" ] && [ ! -L "$gale_installed" ] || gale_fail "refusing to remove a non-regular file: $gale_installed" [ -x "$gale_installed" ] || gale_fail "refusing to remove a non-executable file: $gale_installed" gale_manifest="$gale_install_dir/.gale-install" [ -f "$gale_manifest" ] && [ ! -L "$gale_manifest" ] || gale_fail "Gale install record is missing or invalid: $gale_manifest" exec 3< "$gale_manifest" || gale_fail "cannot read Gale install record: $gale_manifest" if ! IFS= read -r gale_manifest_version_line <&3; then exec 3<&- gale_fail "invalid Gale install record: $gale_manifest" fi if ! IFS= read -r gale_manifest_digest_line <&3; then exec 3<&- gale_fail "invalid Gale install record: $gale_manifest" fi gale_manifest_extra= if IFS= read -r gale_manifest_extra <&3 || [ -n "$gale_manifest_extra" ]; then exec 3<&- gale_fail "invalid Gale install record: $gale_manifest" fi exec 3<&- case "$gale_manifest_version_line" in version=*) gale_installed_version=${gale_manifest_version_line#version=} ;; *) gale_fail "invalid Gale install record: $gale_manifest" ;; esac case "$gale_manifest_digest_line" in sha256=*) gale_expected=${gale_manifest_digest_line#sha256=} ;; *) gale_fail "invalid Gale install record: $gale_manifest" ;; esac gale_valid_version "$gale_installed_version" || gale_fail "invalid Gale version in install record: $gale_manifest" [[ "$gale_expected" =~ ^[a-f0-9]{64}$ ]] || gale_fail "invalid checksum in Gale install record: $gale_manifest" gale_actual=$(gale_checksum "$gale_installed") || gale_fail "cannot checksum Gale at $gale_installed; leaving it unchanged" gale_actual=${gale_actual%% *} [ "$gale_actual" = "$gale_expected" ] || gale_fail "Gale does not match its install record; leaving it unchanged: $gale_installed" gale_tools="$gale_install_dir/.gale-toolchain" if [ -e "$gale_tools" ] || [ -L "$gale_tools" ]; then [ -d "$gale_tools" ] && [ ! -L "$gale_tools" ] && [ -f "$gale_tools/owner" ] || gale_fail "unrecognized toolchain directory: $gale_tools" [ "$(cat "$gale_tools/owner")" = gale-managed-toolchain-v1 ] || gale_fail "unrecognized toolchain directory: $gale_tools" if [ -f "$gale_tools/adapter.sha256" ]; then [ -f "$gale_install_dir/gale-why3-adapter" ] && [ ! -L "$gale_install_dir/gale-why3-adapter" ] || gale_fail 'managed adapter is missing or replaced; leaving the installation unchanged' gale_actual=$(gale_checksum "$gale_install_dir/gale-why3-adapter") [ "${gale_actual%% *}" = "$(cat "$gale_tools/adapter.sha256")" ] || gale_fail 'managed adapter was modified; leaving the installation unchanged' fi if [ -f "$gale_tools/profiles" ]; then command -v awk >/dev/null || gale_fail 'awk is required to remove PATH blocks' while IFS= read -r gale_profile_file; do [ -f "$gale_profile_file" ] || continue gale_profile_tmp=$(mktemp) printf -v gale_activate 'if [ -f %q ]; then . %q; fi' "$gale_tools/env" "$gale_tools/env" GALE_BEGIN="# >>> Gale: $gale_install_dir >>>" GALE_END="# <<< Gale: $gale_install_dir <<<" GALE_ACTIVATE="$gale_activate" \ awk '{ if ($0 == ENVIRON["GALE_BEGIN"]) { first=$0; if ((getline second)>0) { if ((getline third)>0) { if (second == ENVIRON["GALE_ACTIVATE"] && third == ENVIRON["GALE_END"]) next; print first; print second; print third; next }; print first; print second; next }; print first; next }; print }' "$gale_profile_file" > "$gale_profile_tmp" cat "$gale_profile_tmp" > "$gale_profile_file" rm -f "$gale_profile_tmp" done < "$gale_tools/profiles" fi [ ! -f "$gale_tools/adapter.sha256" ] || rm -f -- "$gale_install_dir/gale-why3-adapter" rm -rf -- "$gale_tools" printf 'Removed managed runtime and proof tools and unchanged PATH blocks. Shared system packages were retained.\n' fi rm -f -- "$gale_installed" "$gale_manifest" printf 'Removed Gale %s from %s\n' "$gale_installed_version" "$gale_installed" exit 0 fi [ "$gale_version" = latest ] || gale_valid_version "$gale_version" || gale_fail 'version must be a semantic version such as 0.1.0' gale_base_url=${gale_base_url%/} case "$gale_base_url" in https://*) gale_protocol='=https' ;; http://127.0.0.1:*|http://localhost:*) gale_protocol='=http,https' ;; *) gale_fail 'download URL must use HTTPS (loopback HTTP is allowed for local testing)' ;; esac case "$gale_base_url" in *\?*|*\#*|*$'\n'*|*$'\r'*) gale_fail 'download URL must not contain a query, fragment or newline' ;; esac for gale_command in curl uname mktemp chmod mv mkdir rm cat; do command -v "$gale_command" >/dev/null 2>&1 || gale_fail "required command is missing: $gale_command" done gale_os=$(uname -s) gale_arch=$(uname -m) case "$gale_os/$gale_arch" in Linux/x86_64|Linux/amd64) gale_platform=linux-x86_64 ;; Darwin/arm64|Darwin/aarch64) gale_platform=macos-arm64 ;; *) gale_fail "unsupported system: $gale_os/$gale_arch; supported systems are Linux x86_64 and macOS ARM64" ;; esac # Invoked by the EXIT trap. # shellcheck disable=SC2329 gale_cleanup() { [ -z "$gale_tmp" ] || rm -rf -- "$gale_tmp" [ -z "$gale_staged" ] || rm -f -- "$gale_staged" [ -z "$gale_manifest_staged" ] || rm -f -- "$gale_manifest_staged" } trap gale_cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM gale_tmp=$(mktemp -d "${TMPDIR:-/tmp}/gale-install.XXXXXXXX") gale_download() { gale_http_status=$(curl --fail --location --silent --show-error --retry 3 \ --proto "$gale_protocol" --proto-redir "$gale_protocol" --tlsv1.2 \ --connect-timeout 15 --max-time 180 --max-filesize "$3" \ --write-out '%{http_code}' --output "$2" "$1") || { if [ "$gale_http_status" = 404 ]; then gale_fail "release file is not published: $1; check the requested version or try again after a release is published: https://gale-lang.org/quick-start/" fi gale_fail "could not download $1" } } if [ "$gale_version" = latest ]; then gale_download "$gale_base_url/latest/$gale_platform" "$gale_tmp/latest" 64 gale_version=$(cat "$gale_tmp/latest") gale_valid_version "$gale_version" || gale_fail 'latest contains an invalid Gale release version' fi gale_asset="gale-$gale_platform" gale_release_url="$gale_base_url/releases/$gale_version" printf 'Installing Gale %s for %s…\n' "$gale_version" "$gale_platform" gale_download "$gale_release_url/$gale_asset.sha256" "$gale_tmp/checksum" 128 gale_expected=$(cat "$gale_tmp/checksum") [[ "$gale_expected" =~ ^[a-f0-9]{64}$ ]] || gale_fail 'release checksum must be one SHA-256 digest' gale_download "$gale_release_url/$gale_asset" "$gale_tmp/gale" 104857600 gale_actual=$(gale_checksum "$gale_tmp/gale") gale_actual=${gale_actual%% *} [ "$gale_actual" = "$gale_expected" ] || gale_fail 'checksum mismatch; the installed compiler has not been changed' chmod 755 "$gale_tmp/gale" "$gale_tmp/gale" version >/dev/null || gale_fail 'downloaded compiler cannot run on this system; the installed compiler has not been changed' gale_binary_version=$("$gale_tmp/gale" --version) || gale_fail 'downloaded compiler version check failed; the installed compiler has not been changed' [ "$gale_binary_version" = "$gale_version" ] || gale_fail 'downloaded compiler version does not match the requested release; the installed compiler has not been changed' if [ "$gale_yes" = 0 ] && { : /dev/null; then gale_setup=1; fi if [ "$gale_runtime/$gale_proof/$gale_profile" = no/no/no ]; then gale_setup=0; fi if [ "$gale_setup" = 1 ]; then command -v tar >/dev/null || gale_fail 'tar is required for optional toolchain setup' gale_tools_asset="gale-toolchain-$gale_platform.tar.gz" gale_download "$gale_release_url/$gale_tools_asset.sha256" "$gale_tmp/tools.sha256" 128 gale_tools_digest=$(cat "$gale_tmp/tools.sha256") [[ "$gale_tools_digest" =~ ^[a-f0-9]{64}$ ]] || gale_fail 'invalid toolchain checksum' gale_download "$gale_release_url/$gale_tools_asset" "$gale_tmp/tools.tar.gz" 10485760 gale_tools_actual=$(gale_checksum "$gale_tmp/tools.tar.gz") [ "${gale_tools_actual%% *}" = "$gale_tools_digest" ] || gale_fail 'toolchain checksum mismatch; the installed compiler has not been changed' mkdir "$gale_tmp/tools" tar -xzf "$gale_tmp/tools.tar.gz" -C "$gale_tmp/tools" setup.sh main.ml versions.sh for gale_member in setup.sh main.ml versions.sh; do [ -f "$gale_tmp/tools/$gale_member" ] && [ ! -L "$gale_tmp/tools/$gale_member" ] || gale_fail 'invalid toolchain archive member' done fi mkdir -p -- "$gale_install_dir" [ ! -d "$gale_install_dir/gale" ] || gale_fail 'destination gale is a directory' [ ! -d "$gale_install_dir/.gale-install" ] || gale_fail 'destination .gale-install is a directory' gale_staged=$(mktemp "$gale_install_dir/.gale-install.XXXXXXXX") gale_manifest_staged=$(mktemp "$gale_install_dir/.gale-install-record.XXXXXXXX") cat "$gale_tmp/gale" > "$gale_staged" chmod 755 "$gale_staged" printf 'version=%s\nsha256=%s\n' "$gale_version" "$gale_actual" > "$gale_manifest_staged" chmod 644 "$gale_manifest_staged" mv -f -- "$gale_staged" "$gale_install_dir/gale" gale_staged= mv -f -- "$gale_manifest_staged" "$gale_install_dir/.gale-install" gale_manifest_staged= printf 'Installed Gale %s at %s/gale\n' "$gale_version" "$gale_install_dir" "$gale_install_dir/gale" version if [ "$gale_setup" = 1 ]; then gale_install_dir=$(cd -- "$gale_install_dir" && pwd) bash "$gale_tmp/tools/setup.sh" "$gale_install_dir" "$gale_version" \ "$gale_runtime" "$gale_proof" "$gale_profile" "$gale_yes" || gale_fail 'compiler installed, but optional setup did not finish; rerun the installer to retry' fi # Keep $PATH literal in the command printed for the user. # shellcheck disable=SC2016 case ":${PATH:-}:" in *":$gale_install_dir:"*) ;; *) printf '\nAdd Gale to your PATH for this shell:\n export PATH=%q:"$PATH"\n' "$gale_install_dir" ;; esac ) gale_install "$@"