#!/bin/sh # Rind installer: installs the `peel` command-line tool (and optionally libpeel + peel.h). # # curl -fsSL https://filesunwrapped.com/developers/install.sh | sh # curl -fsSL https://filesunwrapped.com/developers/install.sh | sh -s -- --prefix ~/.local --with-lib # # Options: # --prefix DIR install under DIR (bin/, and lib/ + include/ with --with-lib). # Default: /usr/local if writable, otherwise ~/.local. RIND_INSTALL_DIR is the same. # --version X.Y.Z install that release instead of the latest. # --with-lib also install libpeel (static + dynamic) and peel.h. # -h, --help show this help. # # Environment: # RIND_INSTALL_DIR same as --prefix # RIND_VERSION same as --version # RIND_REPO GitHub repository holding the releases. Default: SnackStudio/peel-releases # RIND_BASE_URL where tarballs are served. Default: https://github.com/$RIND_REPO/releases # Tarballs come from $RIND_BASE_URL/download/rind-v/. A file:// URL # works too (with RIND_VERSION set), which is how the installer is tested. # RIND_API_URL release list used to find the latest Rind version. # Default: https://api.github.com/repos/$RIND_REPO/releases?per_page=50 # # The tarball's .sha256 is checked before anything is extracted or installed; a mismatch aborts. # sudo is never invoked. If the prefix is not writable, pick another one. set -eu REPO="${RIND_REPO:-SnackStudio/peel-releases}" BASE_URL="${RIND_BASE_URL:-https://github.com/$REPO/releases}" API_URL="${RIND_API_URL:-https://api.github.com/repos/$REPO/releases?per_page=50}" PREFIX="${RIND_INSTALL_DIR:-}" VERSION="${RIND_VERSION:-}" WITH_LIB=0 say() { printf '%s\n' "$*"; } die() { printf 'rind-install: %s\n' "$*" >&2; exit 1; } usage() { sed -n '2,/^set -eu/p' "$0" 2>/dev/null | sed '$d' | sed 's/^# \{0,1\}//' ; } while [ $# -gt 0 ]; do case "$1" in --prefix) [ $# -ge 2 ] || die "--prefix needs a directory"; PREFIX="$2"; shift 2 ;; --prefix=*) PREFIX="${1#--prefix=}"; shift ;; --version) [ $# -ge 2 ] || die "--version needs a value"; VERSION="$2"; shift 2 ;; --version=*) VERSION="${1#--version=}"; shift ;; --with-lib) WITH_LIB=1; shift ;; -h|--help) usage; exit 0 ;; *) die "unknown option: $1 (try --help)" ;; esac done # --- platform -> tarball target ------------------------------------------------------------- os="$(uname -s)" arch="$(uname -m)" case "$os" in Darwin) target="universal-apple-darwin" ;; Linux) case "$arch" in x86_64|amd64) target="x86_64-unknown-linux-gnu" ;; aarch64|arm64) target="aarch64-unknown-linux-gnu" ;; *) die "unsupported CPU: $arch (Rind ships x86_64 and aarch64 on Linux)" ;; esac ;; *) die "unsupported OS: $os (Rind ships for macOS and Linux)" ;; esac # --- tools ---------------------------------------------------------------------------------- if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL --retry 3 -o "$2" "$1"; } fetch_stdout() { curl -fsSL --retry 3 "$1"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -q -O "$2" "$1"; } fetch_stdout() { wget -q -O - "$1"; } else die "need curl or wget" fi if command -v shasum >/dev/null 2>&1; then sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; } elif command -v sha256sum >/dev/null 2>&1; then sha256_of() { sha256sum "$1" | awk '{print $1}'; } else die "need shasum or sha256sum to verify the download" fi # --- which release -------------------------------------------------------------------------- # peel-releases also hosts the Peel app's releases (tags v*), so GitHub's own "latest" release is # usually the app, not Rind. Rind releases are tagged rind-v*; take the newest of those. case "$BASE_URL" in file://*) fetch() { cp "${1#file://}" "$2"; } ;; esac APP_VERSION="" if [ -z "$VERSION" ]; then releases="$(fetch_stdout "$API_URL")" || die "could not look up the latest Rind release ($API_URL)" VERSION="$(printf '%s\n' "$releases" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"rind-v\([^"]*\)".*/\1/p' | head -n1)" if [ -z "$VERSION" ]; then # Before the first rind-v* release, macOS falls back to the notarized `peel` inside the # newest Peel app release (tags v*). Linux has no such fallback. [ "$os" = Darwin ] || die "no Rind release found at $API_URL yet; build from source: https://filesunwrapped.com/developers/install#source" APP_VERSION="$(printf '%s\n' "$releases" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"v\([0-9][^"]*\)".*/\1/p' | head -n1)" [ -n "$APP_VERSION" ] || die "no Rind or Peel release found at $API_URL" VERSION="$APP_VERSION" fi fi VERSION="${VERSION#v}" case "$VERSION" in *[!0-9A-Za-z.+-]*|"") die "odd version string: $VERSION" ;; esac tmp="$(mktemp -d "${TMPDIR:-/tmp}/rind-install.XXXXXX")" trap 'rm -rf "$tmp"' EXIT INT HUP TERM if [ -n "$APP_VERSION" ]; then dl="$BASE_URL/download/v$VERSION" tarball="Peel-$VERSION.zip" say "No Rind release yet; installing the peel tool from Peel $VERSION" fetch "$dl/$tarball" "$tmp/$tarball" || die "download failed: $dl/$tarball" fetch "$dl/Peel-$VERSION.sha256" "$tmp/app.sha256" || die "download failed: $dl/Peel-$VERSION.sha256" # The app's .sha256 lists several release files; take the zip's line. expected="$(awk -v f="$tarball" '$2 == f { print $1 }' "$tmp/app.sha256" | head -n1)" else dl="$BASE_URL/download/rind-v$VERSION" name="rind-$VERSION-$target" tarball="$name.tar.gz" say "Installing Rind $VERSION ($target)" fetch "$dl/$tarball" "$tmp/$tarball" || die "download failed: $dl/$tarball" fetch "$dl/$tarball.sha256" "$tmp/$tarball.sha256" || die "download failed: $dl/$tarball.sha256" expected="$(awk '{print $1}' "$tmp/$tarball.sha256" | head -n1)" fi # --- verify BEFORE extracting or installing ------------------------------------------------- actual="$(sha256_of "$tmp/$tarball")" case "$expected" in [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]*) ;; *) die "the checksum file is not a SHA-256 digest; refusing to install" ;; esac if [ "$expected" != "$actual" ]; then die "checksum mismatch for $tarball (expected $expected, got $actual); refusing to install" fi say "Checksum OK" # --- prefix --------------------------------------------------------------------------------- if [ -z "$PREFIX" ]; then if [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then PREFIX=/usr/local elif [ ! -e /usr/local/bin ] && [ -d /usr/local ] && [ -w /usr/local ]; then PREFIX=/usr/local else PREFIX="$HOME/.local" fi fi mkdir -p "$PREFIX/bin" 2>/dev/null || die "cannot create $PREFIX/bin; choose another prefix with --prefix" [ -w "$PREFIX/bin" ] || die "$PREFIX/bin is not writable; choose another prefix with --prefix (sudo is not used)" # --- extract + install ---------------------------------------------------------------------- install_file() { # mode src dst cp "$2" "$3.new.$$" && chmod "$1" "$3.new.$$" && mv -f "$3.new.$$" "$3" } if [ -n "$APP_VERSION" ]; then mkdir "$tmp/app" || die "could not create $tmp/app" ditto -x -k "$tmp/$tarball" "$tmp/app" || die "could not unzip $tarball" cli="$tmp/app/Peel.app/Contents/Helpers/peel" [ -x "$cli" ] || die "the peel binary was not found in $tarball" "$cli" --version >/dev/null 2>&1 || die "the downloaded peel binary does not run on this Mac" install_file 755 "$cli" "$PREFIX/bin/peel" say "Installed $PREFIX/bin/peel" if [ "$WITH_LIB" = 1 ]; then say "libpeel and peel.h come with the first Rind release; until then build them from source:" say " https://filesunwrapped.com/developers/install#source" WITH_LIB=skipped fi else tar -xzf "$tmp/$tarball" -C "$tmp" || die "could not extract $tarball" src="$tmp/$name" [ -x "$src/bin/peel" ] || die "the tarball does not contain bin/peel" install_file 755 "$src/bin/peel" "$PREFIX/bin/peel" say "Installed $PREFIX/bin/peel" fi if [ "$WITH_LIB" = 1 ]; then mkdir -p "$PREFIX/lib" "$PREFIX/include" || die "cannot create $PREFIX/lib or $PREFIX/include" for f in "$src"/lib/*; do install_file 644 "$f" "$PREFIX/lib/$(basename "$f")"; done install_file 644 "$src/include/peel.h" "$PREFIX/include/peel.h" say "Installed libpeel into $PREFIX/lib and peel.h into $PREFIX/include" fi # --- next steps ----------------------------------------------------------------------------- case ":$PATH:" in *":$PREFIX/bin:"*) ;; *) say ""; say "Add $PREFIX/bin to your PATH:"; say " export PATH=\"$PREFIX/bin:\$PATH\"" ;; esac say "" "$PREFIX/bin/peel" --version 2>/dev/null || true say "Next: run 'peel --help', or try 'peel list some-archive.zip'." if [ "$WITH_LIB" = 0 ]; then say "To link against the engine, re-run with --with-lib." fi