#!/bin/sh # Installs Gigahog, the disk space analyzer for macOS (https://gigahog.com). # # curl -fsSL https://gigahog.com/install.sh | sh # curl -fsSL https://gigahog.com/install.sh | sh -s -- [--dir DIR] [--cli] [--no-open] # # --dir DIR install Gigahog.app into DIR (default /Applications, or # ~/Applications when /Applications isn't writable; never sudo) # --cli also link the gigahog command-line tool (gigahog install-cli) # --no-open don't open Gigahog afterwards # # What it does: reads the latest release from gigahog.com/releases/appcast.json, # downloads its disk image into a temporary folder, checks the image's SHA-256 # against the appcast, copies Gigahog.app out of it, and cleans up. It touches # nothing but the app's own path and the temporary folder. It never replaces a # newer copy, and if anything fails half way the old copy is put back. # # Trust: the appcast and the image both come over HTTPS from gigahog.com, and # the image must match the appcast's SHA-256. The appcast entry is also signed # (Ed25519, checked by the app before every update), but macOS has no tool to # check that signature from a shell script, so here HTTPS and the SHA-256 are # what you are trusting. # # Files downloaded with curl aren't quarantined, so macOS doesn't ask about # Gigahog on first launch. If Gigahog is running from where it would install, # it asks you to quit it and changes nothing. For testing: GIGAHOG_INSTALL_BASE # (http only on 127.0.0.1/localhost) and GIGAHOG_INSTALL_DIR (= --dir). # # Everything runs from main, called inside braces on the last line, so a # download of this script cut short anywhere does nothing at all. set -eu say() { printf 'gigahog: %s\n' "$*"; } die() { printf 'gigahog: %s\n' "$*" >&2; exit 1; } TMP='' MNT='' STAGE='' OLD='' APP='' cleanup() { if [ -n "$MNT" ] && [ -d "$MNT" ]; then hdiutil detach "$MNT" -quiet -force 2>/dev/null || true; fi if [ -n "$OLD" ] && [ -e "$OLD" ] && [ ! -e "$APP" ]; then mv "$OLD" "$APP" && say "put the previous copy back"; fi if [ -n "$STAGE" ]; then rm -rf "$STAGE"; fi case "$TMP" in */gigahog-install.*) rm -rf "$TMP" ;; esac } main() { BASE=${GIGAHOG_INSTALL_BASE:-https://gigahog.com} DIR=${GIGAHOG_INSTALL_DIR:-} CLI=0 OPEN=1 while [ $# -gt 0 ]; do case "$1" in --dir) [ $# -ge 2 ] || die "--dir needs a folder"; DIR=$2; shift ;; --cli) CLI=1 ;; --no-open) OPEN=0 ;; -h | --help) say "options: --dir DIR, --cli, --no-open (see the comments at https://gigahog.com/install.sh)"; exit 0 ;; *) die "unknown option $1 (try --help)" ;; esac shift done case "$BASE" in https://*) PROTO='=https' ;; http://127.0.0.1[:/]* | http://localhost[:/]*) PROTO='=http' ;; *) die "GIGAHOG_INSTALL_BASE must be https (or http on 127.0.0.1 for testing)" ;; esac [ "$(uname -s)" = Darwin ] || die "Gigahog runs on macOS only" [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ] || die "Gigahog needs a Mac with Apple silicon (M1 or later)" trap cleanup EXIT trap 'exit 130' INT TERM T=$(mktemp -d "${TMPDIR:-/tmp}/gigahog-install.XXXXXX") || die "couldn't make a temporary folder" TMP=$(cd "$T" && pwd -P) || die "couldn't make a temporary folder" # resolved, as mount(8) prints it # 1. The latest release. say "checking $BASE for the latest release" curl -fsSL --proto "$PROTO" --proto-redir "$PROTO" "$BASE/releases/appcast.json" -o "$TMP/appcast.json" || die "couldn't read $BASE/releases/appcast.json" VERSION=$(field version) BUILD=$(field build) URL=$(field url) SHA=$(field sha256) MIN=$(field min_macos) case "$BUILD" in '' | *[!0-9]*) die "the appcast's build isn't a number" ;; esac OS=$(sw_vers -productVersion) [ "${OS%%.*}" -ge "${MIN%%.*}" ] || die "Gigahog $VERSION needs macOS $MIN or later (this Mac has $OS)" case "$URL" in *://*) die "the appcast's download URL must be relative to it" ;; esac URL=$BASE/releases/$URL # 2. Where it goes: nothing running from there, and nothing newer. if [ -z "$DIR" ]; then DIR=/Applications [ -w "$DIR" ] || DIR=$HOME/Applications fi mkdir -p "$DIR" [ -w "$DIR" ] || die "can't write to $DIR (try --dir ~/Applications)" DIR=$(cd "$DIR" && pwd -P) APP=$DIR/Gigahog.app # Nothing to install: say so, and still do what was asked (--cli, open). HAVE=$(plutil -extract CFBundleVersion raw -o - "$APP/Contents/Info.plist" 2>/dev/null || echo 0) case "$HAVE" in '' | *[!0-9]*) HAVE=0 ;; esac if [ "$HAVE" -eq "$BUILD" ]; then say "Gigahog $VERSION is already installed in $APP; it's up to date" finish exit 0 fi if [ "$HAVE" -gt "$BUILD" ]; then say "$APP is newer (build $HAVE) than the latest release ($VERSION, build $BUILD); nothing changed" finish exit 0 fi if pgrep -f "^$APP/Contents/MacOS/Gigahog" >/dev/null 2>&1; then die "Gigahog is running from $APP; quit it (menu bar pig > Quit Gigahog) and run this again" fi # 3. Download and check it. say "downloading Gigahog $VERSION" curl -fSL --proto "$PROTO" --proto-redir "$PROTO" --progress-bar "$URL" -o "$TMP/Gigahog.dmg" || die "download failed: $URL" GOT=$(shasum -a 256 "$TMP/Gigahog.dmg" | cut -d' ' -f1) [ "$GOT" = "$SHA" ] || die "the download's SHA-256 is $GOT, but the appcast says $SHA; nothing was installed" # 4. Copy it out of the image, beside the old copy, then swap. MNT=$TMP/mnt mkdir "$MNT" hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$MNT" "$TMP/Gigahog.dmg" >/dev/null || die "couldn't open the disk image" [ -d "$MNT/Gigahog.app" ] || die "the disk image has no Gigahog.app; nothing was installed" STAGE=$DIR/.Gigahog.app.installing rm -rf "$STAGE" ditto "$MNT/Gigahog.app" "$STAGE" || die "couldn't copy Gigahog.app into $DIR (is it full?); nothing was installed" hdiutil detach "$MNT" -quiet || true codesign --verify --strict "$STAGE" || die "the copied app's code signature doesn't verify; nothing was installed" [ "$(plutil -extract CFBundleVersion raw -o - "$STAGE/Contents/Info.plist")" = "$BUILD" ] || die "the disk image isn't build $BUILD; nothing was installed" if [ -e "$APP" ]; then OLD=$DIR/.Gigahog.app.old rm -rf "$OLD" mv "$APP" "$OLD" fi mv "$STAGE" "$APP" || die "couldn't move the new copy into place" STAGE='' if [ -n "$OLD" ]; then rm -rf "$OLD"; OLD=''; fi say "installed Gigahog $VERSION in $APP" finish } # 5. Optional command-line tool, then open it. finish() { if [ "$CLI" = 1 ]; then # Only ever run a verified copy's helper (the one already there, too). codesign --verify --strict "$APP" 2>/dev/null || die "$APP doesn't pass codesign --verify, so its command-line tool wasn't linked; delete it and run this again to reinstall" "$APP/Contents/Helpers/gigahog" install-cli fi if [ "$OPEN" = 1 ]; then open "$APP"; fi say "to uninstall: quit Gigahog, run \"$APP/Contents/Helpers/gigahog\" agent uninstall, then delete $APP" } field() { plutil -extract "latest.$1" raw -o - "$TMP/appcast.json" 2>/dev/null || die "the appcast has no latest.$1"; } # In braces: a download cut anywhere in this line is a syntax error, not a run. { main "$@"; }