#!/bin/bash # Astelyx launcher for macOS: install or update it from the release archive. # # curl -fsSL https://astelyx.com/install-mac.sh | bash # curl -fsSL https://astelyx.com/install-mac.sh | bash -s -- --dir ~/Applications # # WHY A TERMINAL COMMAND. The macOS launcher is not yet signed with an Apple # Developer ID or notarized; it carries an ad hoc signature. A copy a browser # downloads is quarantined, and Gatekeeper refuses to open it. A file curl # downloads is not quarantined, so this script can install the launcher # without asking you to change any security setting. # # WHAT THIS DOES, AND NOTHING ELSE: # 1. asks the Astelyx registry for the newest stable macOS launcher; # 2. downloads it over HTTPS from the short-lived link the registry hands # out, and checks its size and SHA-256 against the registry's answer; # 3. checks the app's code signature and bundle identifier; # 4. puts Astelyx.app in /Applications when you can write there, otherwise # in ~/Applications, replacing only an earlier Astelyx.app; # 5. prints what it did. # It never opens the app, never asks for sudo or a password, never removes a # quarantine attribute or changes a Gatekeeper, firewall or system setting, # and writes nothing but Astelyx.app. set -euo pipefail # The registry this copy talks to. astelyx.com fills it in when it serves the # script; ASTELYX_REGISTRY_URL overrides it (https, or http on this machine). REGISTRY_URL_SERVED='https://api.astelyx.net' DOWNLOAD_PAGE='https://astelyx.com/download' BUNDLE_ID='com.astelyx.launcher' APP_NAME='Astelyx.app' say() { printf 'astelyx: %s\n' "$*"; } die() { printf 'astelyx: error: %s\n' "$*" >&2; exit 1; } usage() { cat <<'EOF' Install or update the Astelyx launcher on this Mac. curl -fsSL https://astelyx.com/install-mac.sh | bash -s -- [--dir FOLDER] --dir FOLDER the folder Astelyx.app goes in (default: /Applications when you can write there, else ~/Applications). --help this text. EOF } dir="" while [ $# -gt 0 ]; do case "$1" in --dir) [ $# -ge 2 ] && [ -n "$2" ] || die "--dir needs a folder" dir=$2 shift 2 ;; --dir=*) dir=${1#--dir=} [ -n "$dir" ] || die "--dir needs a folder" shift ;; -h | --help) usage exit 0 ;; *) die "unknown option '$1' (try --help)" ;; esac done [ "$(uname -s)" = Darwin ] || die "this installs the macOS launcher; for Windows use $DOWNLOAD_PAGE" url_ok() { local https_re='^https://[A-Za-z0-9.-]+(:[0-9]+)?(/[^[:space:]"\\]*)?$' local local_re='^http://(127\.0\.0\.1|localhost|\[::1\])(:[0-9]+)?(/[^[:space:]"\\]*)?$' [[ $1 =~ $https_re ]] || [[ $1 =~ $local_re ]] } registry=${ASTELYX_REGISTRY_URL:-$REGISTRY_URL_SERVED} case "$registry" in __*) die "this copy of the script does not name a registry; use the command on $DOWNLOAD_PAGE" ;; esac registry=${registry%/} url_ok "$registry" || die "the registry address '$registry' is not an https:// URL" for tool in curl shasum tar ditto codesign mktemp; do command -v "$tool" >/dev/null 2>&1 || die "'$tool' is needed and was not found" done if [ "$(sysctl -n hw.optional.arm64 2>/dev/null || true)" = 1 ]; then arch=arm64; else arch=x86_64; fi # ---- where it goes ------------------------------------------------------- if [ -z "$dir" ]; then [ -n "${HOME:-}" ] || die "HOME is not set; pass --dir" if [ -d "/Applications/$APP_NAME" ] && [ -w /Applications ]; then dir=/Applications elif [ -d "$HOME/Applications/$APP_NAME" ]; then dir="$HOME/Applications" elif [ -w /Applications ]; then dir=/Applications else dir="$HOME/Applications" fi fi mkdir -p "$dir" || die "cannot create $dir" dir=$(cd "$dir" && pwd -P) [ -w "$dir" ] || die "$dir is not writable by you; pass --dir with a folder you own" target="$dir/$APP_NAME" # ---- small helpers ------------------------------------------------------- http_get() { local proto='=https' case "$1" in http://*) proto='=http' ;; esac curl --silent --show-error --location --proto "$proto" --proto-redir "$proto" \ --connect-timeout 20 --retry 2 --output "$2" --write-out '%{http_code}' "$1" } json_str() { local re="\"$2\":\"([^\"\\\\]*)\"" [[ $1 =~ $re ]] && printf '%s' "${BASH_REMATCH[1]}" } json_num() { local re="\"$2\":([0-9]+)" [[ $1 =~ $re ]] && printf '%s' "${BASH_REMATCH[1]}" } registry_error() { local body detail body=$(cat "$2" 2>/dev/null || true) detail=$(json_str "$body" detail || true) [ -n "$detail" ] || detail=$(json_str "$body" error || true) printf '%s' "${detail:-HTTP $1}" } bundle_id_of() { /usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$1/Contents/Info.plist" 2>/dev/null || true } bundle_version_of() { /usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$1/Contents/Info.plist" 2>/dev/null || true } # Staged next to the destination, so the last step is a rename on one volume. work=$(mktemp -d "$dir/.astelyx-install.XXXXXX") || die "cannot create a temporary folder in $dir" mounted="" cleanup() { if [ -n "$mounted" ]; then hdiutil detach -quiet "$mounted" >/dev/null 2>&1 || true; fi rm -rf "$work" } trap cleanup EXIT # ---- what is published --------------------------------------------------- say "asking $registry for the newest macOS launcher" status=$(http_get "$registry/v1/builds/latest?category=launcher&os=macos&arch=$arch" "$work/latest.json") \ || die "could not reach $registry" case "$status" in 200) ;; 404) die "no macOS launcher is published yet (see $DOWNLOAD_PAGE)" ;; *) die "the registry refused the lookup: $(registry_error "$status" "$work/latest.json")" ;; esac latest=$(cat "$work/latest.json") # The row's own file comes first in the answer; its first-install installer, # when it has one, is the "installer" object after it. main=${latest%%\"installer\":*} installer="" case "$latest" in *'"installer":{'*) installer=${latest#*\"installer\":\{} ;; esac id=$(json_str "$main" id || true) version=$(json_str "$main" version || true) got_category=$(json_str "$main" category || true) got_os=$(json_str "$main" os || true) [[ $id =~ ^[0-9a-f-]{36}$ ]] || die "the registry's answer has no build id" [[ $version =~ ^[A-Za-z0-9.+_-]{1,64}$ ]] || die "the registry's answer has no usable version" [ "$got_category" = launcher ] && [ "$got_os" = macos ] \ || die "the registry answered with a $got_category build for $got_os, not the macOS launcher" # Which file: the app bundle in a tarball (the updater's artifact) when the # release has one, else the disk image. artifact_name=$(json_str "$main" filename || true) case "$artifact_name" in *.app.tar.gz) kind=tarball query="" sha=$(json_str "$main" sha256 || true) size=$(json_num "$main" size_bytes || true) ;; *.dmg) kind=dmg query="" sha=$(json_str "$main" sha256 || true) size=$(json_num "$main" size_bytes || true) ;; *) case "$(json_str "$installer" filename || true)" in *.dmg) kind=dmg query="?artifact=installer" sha=$(json_str "$installer" sha256 || true) size=$(json_num "$installer" size_bytes || true) ;; *) die "the newest macOS launcher ($version) has no file this script can install; use $DOWNLOAD_PAGE" ;; esac ;; esac [[ $sha =~ ^[0-9a-f]{64}$ ]] || die "the registry's answer has no sha256" [[ $size =~ ^[0-9]+$ ]] || die "the registry's answer has no size" say "newest: Astelyx $version ($kind, $size bytes, sha256 $sha)" status=$(http_get "$registry/v1/builds/$id/download$query" "$work/descriptor.json") \ || die "could not reach $registry" [ "$status" = 200 ] || die "the registry refused the download: $(registry_error "$status" "$work/descriptor.json")" descriptor=$(cat "$work/descriptor.json") url=$(json_str "$descriptor" url || true) want=$(json_str "$descriptor" sha256 || true) url_ok "$url" || die "the registry's download link is not an https:// URL" [ "$want" = "$sha" ] || die "the download the registry offers is not the build it listed; run the command again" # ---- download and verify -------------------------------------------------- say "downloading" file="$work/download" status=$(http_get "$url" "$file") || die "the download failed" [ "$status" = 200 ] || die "the download failed (HTTP $status)" got_size=$(wc -c <"$file" | tr -d '[:space:]') [ "$got_size" = "$size" ] || die "the download is $got_size bytes, the registry says $size; nothing was installed" got=$(shasum -a 256 "$file" | cut -d' ' -f1) [ "$got" = "$sha" ] || die "the download's sha256 is $got, the registry says $sha; nothing was installed" say "sha256 verified: $got" # ---- unpack --------------------------------------------------------------- mkdir "$work/stage" if [ "$kind" = tarball ]; then # Every entry must sit inside Astelyx.app. entries=$(tar -tzf "$file") || die "the download is not a gzip tarball" while IFS= read -r entry; do [ -n "$entry" ] || continue entry=${entry#./} case "/$entry/" in *'/../'* | '//'*) die "the archive holds an unsafe entry '$entry'; nothing was installed" ;; esac case "$entry" in "$APP_NAME" | "$APP_NAME"/*) ;; *) die "the archive holds '$entry', outside $APP_NAME; nothing was installed" ;; esac done </dev/null 2>&1 || die "'hdiutil' is needed and was not found" mkdir "$work/mnt" hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$work/mnt" "$file" /dev/null \ || die "the disk image could not be opened; nothing was installed" mounted="$work/mnt" [ -d "$work/mnt/$APP_NAME" ] || die "the disk image holds no $APP_NAME; nothing was installed" ditto "$work/mnt/$APP_NAME" "$work/stage/$APP_NAME" hdiutil detach -quiet "$work/mnt" >/dev/null 2>&1 || true mounted="" fi new_app="$work/stage/$APP_NAME" [ -d "$new_app" ] || die "the download holds no $APP_NAME; nothing was installed" [ "$(bundle_id_of "$new_app")" = "$BUNDLE_ID" ] \ || die "the downloaded app is not $BUNDLE_ID; nothing was installed" codesign --verify --deep --strict "$new_app" 2>/dev/null \ || die "the downloaded app's code signature does not verify; nothing was installed" signature=$(codesign -dv "$new_app" 2>&1 | sed -n 's/^Authority=//p' | head -n 1) [ -n "$signature" ] || signature="ad hoc (no Apple Developer ID, not notarized)" # ---- install -------------------------------------------------------------- replaced=no if [ -e "$target" ]; then [ "$(bundle_id_of "$target")" = "$BUNDLE_ID" ] \ || die "$target is not the Astelyx launcher; move it away or pass another --dir" if pgrep -f "$target/Contents/MacOS/" >/dev/null 2>&1; then die "Astelyx is running; quit it, then run this again" fi previous_version=$(bundle_version_of "$target") mv "$target" "$work/previous" || die "could not move the old Astelyx.app aside; nothing was changed" replaced=yes fi if ! mv "$new_app" "$target"; then [ "$replaced" = no ] || mv "$work/previous" "$target" || true die "could not put the new Astelyx.app in place; the old one was kept" fi # ---- what was done -------------------------------------------------------- echo say "installed Astelyx $version" say " app $target" say " sha256 $sha (matches the registry)" say " signed $signature" if [ "$replaced" = yes ]; then say " replaced Astelyx ${previous_version:-(earlier version)}; your sign-in, settings and games are kept" fi say " nothing else was changed, and the app was not opened" echo cat <