#!/bin/sh # SPDX-License-Identifier: Apache-2.0 # # Install the `mirror` command on macOS (Apple Silicon) or Linux (x64). # # curl -fsSL https://mirrordimensions.com/install.sh | sh # # or, to read it first: # # curl -fsSLO https://mirrordimensions.com/install.sh # less install.sh # sh install.sh # # What it does, and nothing else: # 1. finds the newest published release of MirrorDimensions on GitHub (or the one you name); # 2. downloads that release's package and checks it against the release's checksums; # 3. downloads uv, the Python tool installer, if this computer has none, and checks it # against the checksum written below; # 4. installs `mirror` for your user with `uv tool install`; # 5. asks this computer whether it can hold an engine (`mirror engine preflight`), which # changes nothing. # # It never uses sudo, writes only under your home folder, and does not create the engine: # that is a download of several gigabytes and is yours to start, with `mirror engine setup`. # # Settings, as environment variables: # MIRROR_VERSION the release to install, such as 1.0.0 (default: the newest) # MIRROR_NO_PREFLIGHT set to 1 to skip step 5 # MIRROR_RELEASE_BASE where to fetch the release's files from, for testing this script; # needs MIRROR_VERSION # HTTPS_PROXY honoured by every download # # The source of this script is scripts/install.sh in # https://github.com/mirrordimensions/mirrordimensions set -eu REPO="mirrordimensions/mirrordimensions" SITE="https://mirrordimensions.com" # uv, pinned: the version and the checksum of each archive, as its publisher lists them at # https://github.com/astral-sh/uv/releases. Change all of them together. UV_VERSION="0.13.13" # shellcheck disable=SC2034 # read by name in install_uv UV_SHA_aarch64_apple_darwin="9e859ab62a9ad9a37ad8f33bb6982fd1529f1d6cd166355b77eb30fec703a5e5" # shellcheck disable=SC2034 UV_SHA_x86_64_unknown_linux_gnu="f5721ccf97321af0b405ca7d615a830a332848e3dfb293bcd93e95c829ab37a9" say() { printf '%s\n' "$*"; } step() { printf '\n==> %s\n' "$*"; } fail() { printf '\ninstall.sh: %s\n' "$1" >&2 shift for line in "$@"; do printf ' %s\n' "$line" >&2; done exit 1 } have() { command -v "$1" >/dev/null 2>&1; } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{print $1}' elif have shasum; then shasum -a 256 "$1" | awk '{print $1}' else fail "No program to check a download with." "Install sha256sum or shasum, then run this again." fi } # fetch : HTTPS only, except for a release base given for testing. fetch() { case "$1" in https://*) set -- "$1" "$2" --proto '=https' --tlsv1.2 ;; esac url="$1" out="$2" shift 2 if ! curl -fsSL --retry 2 "$@" -o "$out" "$url"; then fail "Could not download $url" \ "If your network needs a proxy, set HTTPS_PROXY and run this again." \ "If it replaces certificates or blocks downloads, see $SITE/docs/restricted-networks/" fi } # verify verify() { got="$(sha256_of "$1")" if [ "$got" != "$2" ]; then fail "The checksum of $3 is not the one expected." \ "expected $2" "got $got" \ "Nothing was installed. The download may have been changed on the way: try again" \ "on another network, or see $SITE/docs/restricted-networks/" fi } # The checksum SHA256SUMS lists for a file name, or nothing. listed() { awk -v name="$2" '{ f = $2; sub(/^\*/, "", f); if (f == name) print $1 }' "$1"; } target() { os="$(uname -s)" arch="$(uname -m)" case "$os/$arch" in Darwin/arm64) say "aarch64-apple-darwin" ;; Linux/x86_64 | Linux/amd64) say "x86_64-unknown-linux-gnu" ;; Darwin/*) fail "This Mac has an Intel processor." \ "MirrorDimensions runs on Macs with Apple Silicon (M1 or later)." \ "See $SITE/docs/platforms/" ;; Linux/*) fail "This Linux computer is $arch." \ "MirrorDimensions runs on Linux x64." "See $SITE/docs/platforms/" ;; MINGW* | MSYS* | CYGWIN*) fail "This is Windows." \ "In PowerShell, run: irm $SITE/install.ps1 | iex" ;; *) fail "$os on $arch is not a computer MirrorDimensions runs on." "See $SITE/docs/platforms/" ;; esac } newest_release() { # The address GitHub redirects "latest" to ends with the release's tag. where="$(curl -fsSL --proto '=https' --tlsv1.2 -o /dev/null -w '%{url_effective}' \ "https://github.com/$REPO/releases/latest")" || fail "Could not ask GitHub for the newest release." \ "If your network needs a proxy, set HTTPS_PROXY and run this again." \ "See $SITE/docs/restricted-networks/" case "$where" in */releases/tag/v*) say "${where##*/releases/tag/v}" ;; *) fail "MirrorDimensions has no published release yet." \ "Install it from the source instead: $SITE/docs/getting-started/" ;; esac } install_uv() { triple="$1" work="$2" eval "expected=\${UV_SHA_$(say "$triple" | tr '-' '_')}" archive="uv-$triple.tar.gz" step "Downloading uv $UV_VERSION, which installs Python programs" fetch "https://github.com/astral-sh/uv/releases/download/$UV_VERSION/$archive" "$work/$archive" # shellcheck disable=SC2154 # set by the eval above verify "$work/$archive" "$expected" "uv" tar -xzf "$work/$archive" -C "$work" mkdir -p "$HOME/.local/bin" cp "$work/uv-$triple/uv" "$work/uv-$triple/uvx" "$HOME/.local/bin/" chmod 755 "$HOME/.local/bin/uv" "$HOME/.local/bin/uvx" say "uv is in $HOME/.local/bin" } main() { have curl || fail "curl is needed and was not found." "Install curl, then run this again." have tar || fail "tar is needed and was not found." "Install tar, then run this again." [ -n "${HOME:-}" ] || fail "HOME is not set, so there is nowhere to install to." [ "$(id -u)" != 0 ] || fail "Run this as yourself, not as root or with sudo." \ "The mirror installs for one user and its engine belongs to that user." triple="$(target)" version="${MIRROR_VERSION:-}" version="${version#v}" if [ -n "${MIRROR_RELEASE_BASE:-}" ]; then [ -n "$version" ] || fail "MIRROR_RELEASE_BASE needs MIRROR_VERSION as well." base="${MIRROR_RELEASE_BASE%/}" else [ -n "$version" ] || version="$(newest_release)" base="https://github.com/$REPO/releases/download/v$version" fi work="$(mktemp -d "${TMPDIR:-/tmp}/mirror-install.XXXXXX")" trap 'rm -rf "$work"' EXIT INT TERM step "Downloading MirrorDimensions $version" fetch "$base/SHA256SUMS" "$work/SHA256SUMS" wheel="mirrordimensions-$version-py3-none-any.whl" expected="$(listed "$work/SHA256SUMS" "$wheel")" [ -n "$expected" ] || fail "Release $version lists no package named $wheel." \ "Name another release with MIRROR_VERSION, or see https://github.com/$REPO/releases" fetch "$base/$wheel" "$work/$wheel" verify "$work/$wheel" "$expected" "the package" say "The package matches the release's checksum." # The versions of everything the package needs, as the release was tested with them. constraints="" expected="$(listed "$work/SHA256SUMS" constraints.txt)" if [ -n "$expected" ]; then fetch "$base/constraints.txt" "$work/constraints.txt" verify "$work/constraints.txt" "$expected" "the list of versions" constraints="$work/constraints.txt" fi if have uv; then uv="$(command -v uv)" say "Using the uv already here: $uv" else install_uv "$triple" "$work" uv="$HOME/.local/bin/uv" fi step "Installing the mirror command" set -- tool install --force "$work/$wheel" [ -z "$constraints" ] || set -- "$@" --constraints "$constraints" # A company network that replaces certificates: use the ones this computer trusts. The # option's name depends on uv's version. if "$uv" tool install --help 2>/dev/null | grep -q -- '--system-certs'; then set -- "$@" --system-certs elif "$uv" tool install --help 2>/dev/null | grep -q -- '--native-tls'; then set -- "$@" --native-tls fi "$uv" "$@" || fail "uv could not install the package." \ "What it printed above says why. On a company network, see $SITE/docs/restricted-networks/" bin="$("$uv" tool dir --bin)" [ -x "$bin/mirror" ] || fail "The install finished, but $bin/mirror is not there." say "Installed: $("$bin/mirror" --version)" if [ "${MIRROR_NO_PREFLIGHT:-}" != 1 ]; then step "Can this computer hold an engine? (nothing is changed)" "$bin/mirror" engine preflight || true fi step "Done" case ":$PATH:" in *":$bin:"*) ;; *) say "$bin is not on your PATH yet. Add it for new terminal windows with:" say " $uv tool update-shell" say "and for this one with:" say " export PATH=\"$bin:\$PATH\"" say "" ;; esac say "Next, create the engine. The first time downloads about 3.5 GB and takes a few minutes:" say " mirror engine setup" say " mirror engine check" say "Then a first world and a first run: $SITE/docs/getting-started/" say "To update later, run this script again. To remove it: uv tool uninstall mirrordimensions" } # Everything above is definitions: nothing runs until the whole script has arrived. main "$@"