#!/bin/sh # Public bootstrap only: verify a qualified archive, then let its Rust CLI # activate the pair. No JSON interpreter, compiler, private API or eval. set -eu fail() { printf 'lema install: %s\n' "$*" >&2; exit 1; } version= prefix=${XDG_DATA_HOME:-${HOME:?HOME must be set}/.local/share}/lema explicit_dir=no while [ "$#" -gt 0 ]; do case $1 in --version) [ "$#" -ge 2 ] || fail '--version needs X.Y.Z'; version=$2; shift 2 ;; --dir) [ "$#" -ge 2 ] || fail '--dir needs an absolute directory'; prefix=$2; explicit_dir=yes; shift 2 ;; -h|--help) printf '%s\n' 'Usage: sh install.sh [--version X.Y.Z] [--dir DIR]'; exit 0 ;; *) fail "unknown option: $1" ;; esac done case $prefix in /*) ;; *) fail '--dir and XDG_DATA_HOME must be absolute paths' ;; esac LC_ALL=C export LC_ALL printf '%s' "$prefix" | grep '[[:cntrl:]]' >/dev/null 2>&1 && fail 'installation paths cannot contain control characters; spaces and non-ASCII text are supported' case $prefix in *' '*) fail 'installation paths cannot contain newlines' ;; esac # The UTF-8 encodings of the remaining C1 control characters (U+0080..009F). printf '%s' "$prefix" | grep "$(printf '\302[\200-\237]')" >/dev/null 2>&1 && fail 'installation paths cannot contain control characters' case "$(uname -s)/$(uname -m)" in Darwin/arm64) platform=darwin-aarch64 ;; Linux/x86_64) [ -x /lib64/ld-linux-x86-64.so.2 ] || [ -x /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ] || fail 'Linux installation requires glibc; this host has no supported glibc loader' platform=linux-x86_64 ;; *) fail 'supported hosts are Apple Silicon macOS and Linux x86_64 with glibc' ;; esac for tool in curl tar gzip mktemp wc grep cut mkdir rm stat id dirname; do command -v "$tool" >/dev/null 2>&1 || fail "install the bootstrap prerequisite: $tool"; done # The runtime requires protected executable ancestry. Refuse an unsafe custom # destination before downloading or writing an installation, rather than leave # a pair which cannot run. Homebrew's separate formula handles its own prefix. check_path=$prefix while [ ! -e "$check_path" ] && [ ! -L "$check_path" ]; do check_path=$(dirname "$check_path"); done [ -w "$check_path" ] || fail "installation directory is not writable by this user: $check_path; choose a directory under your home" install_uid=$(id -u) while :; do [ -d "$check_path" ] && [ ! -L "$check_path" ] || fail "installation ancestor must be a real directory: $check_path" if [ "$platform" = darwin-aarch64 ]; then details=$(stat -f '%Lp:%u' "$check_path") else details=$(stat -c '%a:%u' "$check_path"); fi mode=${details%:*}; owner=${details#*:} [ "$owner" = 0 ] || [ "$owner" = "$install_uid" ] || fail "installation ancestor has another owner: $check_path" [ "$((0$mode & 022))" -eq 0 ] || fail "installation ancestor is writable by another user/group: $check_path; choose a directory under your home" [ "$check_path" = / ] && break check_path=$(dirname "$check_path") done if command -v sha256sum >/dev/null 2>&1; then digest() { sha256sum "$1" | cut -d ' ' -f 1; } elif command -v shasum >/dev/null 2>&1; then digest() { shasum -a 256 "$1" | cut -d ' ' -f 1; } else fail 'install sha256sum or shasum to verify downloads'; fi check_version() { printf '%s\n' "$1" | grep -E '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' >/dev/null || fail 'version must be an unprefixed X.Y.Z' [ "${#1}" -le 32 ] || fail 'version is too long' } base=https://www.lema-lang.org/downloads if [ -n "$version" ]; then check_version "$version"; descriptor=$base/v$version/install-$platform.txt else descriptor=$base/install-$platform.txt; fi existing=$(command -v lema 2>/dev/null || :) if [ -n "$existing" ] && [ "$existing" != "$prefix/bin/lema" ]; then [ "$explicit_dir" = yes ] || fail "Lema is already on PATH at $existing; use its package manager, or pass --dir explicitly to install alongside it" printf 'Existing Lema on PATH: %s. Installing separately at %s.\n' "$existing" "$prefix" >&2 fi work=$(mktemp -d "${TMPDIR:-/tmp}/lema-install.XXXXXXXX") || fail 'could not create a private temporary directory' trap 'rm -rf -- "$work"' EXIT trap 'exit 1' HUP INT TERM download() { curl --proto '=https' --proto-redir '=https' --tlsv1.2 --fail --location --silent --show-error --connect-timeout 20 --max-time 600 --max-filesize "$3" --output "$2" "$1" || fail "download failed: $1" } download "$descriptor" "$work/descriptor" 4096 [ "$(wc -c < "$work/descriptor")" -le 4096 ] || fail 'installation descriptor is too large' line=0 while IFS='=' read -r key value; do line=$((line + 1)) case $line:$key in 1:schema) [ "$value" = lema.install/v1 ] || fail 'unsupported descriptor schema' ;; 2:version) release_version=$value ;; 3:platform) [ "$value" = "$platform" ] || fail 'descriptor has the wrong platform' ;; 4:archive_url) archive_url=$value ;; 5:archive_sha256) archive_sha256=$value ;; 6:archive_size) archive_size=$value ;; 7:metadata_url) metadata_url=$value ;; 8:metadata_sha256) metadata_sha256=$value ;; 9:metadata_size) metadata_size=$value ;; *) fail 'unexpected, duplicate or out-of-order installation descriptor field' ;; esac done < "$work/descriptor" [ "$line" -eq 9 ] || fail 'incomplete installation descriptor' [ -z "$key$value" ] || fail 'installation descriptor must end with a newline' check_version "$release_version" [ -z "$version" ] || [ "$release_version" = "$version" ] || fail 'descriptor does not match the requested version' stem=lema-v$release_version-$platform [ "$archive_url" = "$base/v$release_version/$stem.tar.gz" ] || fail 'unexpected archive URL' [ "$metadata_url" = "$base/v$release_version/$stem.json" ] || fail 'unexpected metadata URL' for sha in "$archive_sha256" "$metadata_sha256"; do [ "${#sha}" -eq 64 ] || fail 'invalid SHA-256 length' case $sha in *[!0-9a-f]*) fail 'invalid SHA-256' ;; esac done for size in "$archive_size" "$metadata_size"; do [ -n "$size" ] && [ "${#size}" -le 10 ] || fail 'invalid download size' case $size in *[!0-9]*) fail 'invalid download size' ;; esac [ "$size" -gt 0 ] || fail 'empty download' done [ "$archive_size" -le 1073741824 ] && [ "$metadata_size" -le 1048576 ] || fail 'download exceeds the installation size limit' download "$metadata_url" "$work/metadata.json" "$metadata_size" [ "$(wc -c < "$work/metadata.json")" -eq "$metadata_size" ] && [ "$(digest "$work/metadata.json")" = "$metadata_sha256" ] || fail 'release metadata integrity check failed' download "$archive_url" "$work/archive.tar.gz" "$archive_size" [ "$(wc -c < "$work/archive.tar.gz")" -eq "$archive_size" ] && [ "$(digest "$work/archive.tar.gz")" = "$archive_sha256" ] || fail 'archive integrity check failed' # The packager emits these six regular files and two explicit directories. # Match each path's type; refuse links, special files and duplicate names. tar -tzf "$work/archive.tar.gz" > "$work/names" || fail 'could not list archive paths' [ "$(wc -l < "$work/names")" -eq 8 ] || fail 'unexpected archive entries' for name in "$stem/" "$stem/bin/"; do [ "$(grep -Fxc "$name" "$work/names" || :)" -eq 1 ] || fail "missing or duplicate archive directory: $name" done for name in bin/lema bin/lema-vm-runner release.json README.txt LICENSE THIRD-PARTY-NOTICES.txt; do [ "$(grep -Fxc "$stem/$name" "$work/names" || :)" -eq 1 ] || fail "missing or duplicate archive entry: $name" done tar -tvzf "$work/archive.tar.gz" > "$work/types" || fail 'could not inspect archive types' [ "$(wc -l < "$work/types")" -eq 8 ] || fail 'unexpected archive entry types' while IFS= read -r name; do IFS= read -r details <&3 || fail 'missing archive entry type' case $name in "$stem/"|"$stem/bin/") case $details in d*) ;; *) fail 'archive directory has the wrong type' ;; esac ;; *) case $details in -*) ;; *) fail 'archive contains a link or special file' ;; esac ;; esac done < "$work/names" 3< "$work/types" mkdir "$work/unpack" tar -xzf "$work/archive.tar.gz" -C "$work/unpack" --no-same-owner --no-same-permissions || fail 'archive extraction failed' "$work/unpack/$stem/bin/lema" self install --bundle "$work/unpack/$stem" --metadata "$work/metadata.json" --dir "$prefix" --shell "${SHELL:-sh}"