#!/bin/sh # Installs MAGE (https://summonmage.com) from its latest GitHub release. # # curl -fsSL https://summonmage.com/install.sh | sh # # Settings (environment variables): # MAGE_BUILD default (all providers), no-china, us, airgap or strict [default] # MAGE_VERSION a release tag such as v0.3.0 [latest] # MAGE_BIN_DIR where the binary goes [~/.local/bin] # # The archive is checked against the release's SHA256SUMS, and when minisign is # installed, SHA256SUMS is checked against MAGE's release signing key too (the same key # `mage update` trusts). The desktop entry and icon go to ~/.local/share on Linux. set -eu REPO="LiveLaughLizard/MAGE" KEY="RWSB2eYkIqYziyN/g52gBI5oDby9+F9PLP3jTQFlPsGzWlnrtoDgae+Z" BUILD="${MAGE_BUILD:-default}" VERSION="${MAGE_VERSION:-latest}" BIN_DIR="${MAGE_BIN_DIR:-$HOME/.local/bin}" say() { printf '\033[1;35mmage\033[0m %s\n' "$*"; } die() { printf '\033[1;31merror\033[0m %s\n' "$*" >&2; exit 1; } need() { command -v "$1" >/dev/null 2>&1 || die "this installer needs $1"; } need curl need tar need uname case "$BUILD" in default|no-china|us|airgap|strict) ;; *) die "MAGE_BUILD is default, no-china, us, airgap or strict, not '$BUILD'" ;; esac case "$(uname -s)" in Linux) os=linux ;; Darwin) os=macos ;; *) die "this installer is for Linux and macOS; Windows downloads are at https://github.com/$REPO/releases" ;; esac case "$(uname -m)" in x86_64|amd64) arch=x86_64 ;; aarch64|arm64) arch=aarch64 ;; *) die "no release for $(uname -m)" ;; esac if [ "$os" = macos ]; then [ "$arch" = aarch64 ] || die "macOS releases are for Apple Silicon only" [ "$BUILD" = default ] || die "macOS has only the default build" fi if [ "$VERSION" = latest ]; then api="https://api.github.com/repos/$REPO/releases/latest" else api="https://api.github.com/repos/$REPO/releases/tags/$VERSION" fi say "looking up the $VERSION release" assets=$(curl -fsSL -H "Accept: application/vnd.github+json" "$api" \ | grep -o '"browser_download_url": *"[^"]*"' | sed 's/.*"\(https[^"]*\)"/\1/') \ || die "couldn't read the release from GitHub" suffix="-$BUILD-$arch-$os.tar.gz" url=$(printf '%s\n' "$assets" | grep -- "$suffix\$" | head -n1) [ -n "$url" ] || die "the release has no $BUILD build for $arch $os" sums_url=$(printf '%s\n' "$assets" | grep '/SHA256SUMS$' | head -n1) file=${url##*/} tmp=$(mktemp -d 2>/dev/null || mktemp -d -t mage) trap 'rm -rf "$tmp"' EXIT INT TERM say "downloading $file" curl -fL --progress-bar -o "$tmp/$file" "$url" if [ -n "$sums_url" ]; then curl -fsSL -o "$tmp/SHA256SUMS" "$sums_url" if command -v minisign >/dev/null 2>&1; then curl -fsSL -o "$tmp/SHA256SUMS.minisig" "$sums_url.minisig" \ || die "couldn't download SHA256SUMS.minisig" if ! minisign -Vqm "$tmp/SHA256SUMS" -P "$KEY"; then die "SHA256SUMS isn't signed by MAGE's release key" fi say "signature verified" fi expected=$(grep " \*\{0,1\}$file\$" "$tmp/SHA256SUMS" | cut -d' ' -f1) [ -n "$expected" ] || die "$file isn't listed in SHA256SUMS" if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$tmp/$file" | cut -d' ' -f1) else actual=$(shasum -a 256 "$tmp/$file" | cut -d' ' -f1) fi [ "$actual" = "$expected" ] || die "checksum mismatch for $file" say "checksum verified" else say "warning: the release has no SHA256SUMS; not verified" fi tar -xzf "$tmp/$file" -C "$tmp" dir="$tmp/${file%.tar.gz}" [ -d "$dir" ] || dir=$(find "$tmp" -mindepth 1 -maxdepth 1 -type d | head -n1) # The binary: `mage`, or `crack` in releases from before the rename. name="" for candidate in mage crack; do if [ -f "$dir/$candidate" ]; then name=$candidate; break; fi done [ -n "$name" ] || die "no mage binary in $file" mkdir -p "$BIN_DIR" install -m 755 "$dir/$name" "$BIN_DIR/$name.new" 2>/dev/null \ || { cp "$dir/$name" "$BIN_DIR/$name.new" && chmod 755 "$BIN_DIR/$name.new"; } mv -f "$BIN_DIR/$name.new" "$BIN_DIR/$name" if [ "$name" != mage ]; then ln -sf "$name" "$BIN_DIR/mage" fi say "installed $BIN_DIR/mage" if [ "$os" = linux ] && [ -d "$dir/share" ]; then data="${XDG_DATA_HOME:-$HOME/.local/share}" mkdir -p "$data" cp -R "$dir/share/." "$data/" say "added the launcher entry and icon under $data" fi case ":$PATH:" in *":$BIN_DIR:"*) ;; *) say "add $BIN_DIR to your PATH, e.g.: echo 'export PATH=\"$BIN_DIR:\$PATH\"' >> ~/.profile" ;; esac say "done. Run 'mage' in a project to start; 'mage update' keeps it current."