#!/bin/sh { # Prevent execution if this script was only partially downloaded oops() { echo "$0:" "$@" >&2 exit 1 } umask 0022 tmpDir="$(mktemp -d -t xip-binary-tarball-unpack.XXXXXXXXXX || oops "Can't create temporary directory for downloading the Xip binary tarball")" cleanup() { rm -rf "$tmpDir" } trap cleanup EXIT INT QUIT TERM require_util() { command -v "$1" >/dev/null 2>&1 || oops "you do not have '$1' installed, which I need to $2" } case "$(uname -s).$(uname -m)" in Darwin.x86_64) system=darwin-amd64 ;; Darwin.arm64 | Darwin.aarch64) system=darwin-arm64 ;; *) oops "sorry, there is no binary distribution of Nix for your platform" ;; esac url=$(curl -s https://api.github.com/repos/gordian-jklapacz/xip/releases/latest | grep "browser" | cut -d : -f 2,3 | tr -d \" | grep $system | grep -v md5 | awk '{$1=$1};1') path=$(echo "$url" | cut -d / -f 9) # Use this command-line option to fetch the tarballs using nar-serve or Cachix if [ "${1:-}" = "--tarball-url-prefix" ]; then if [ -z "${2:-}" ]; then oops "missing argument for --tarball-url-prefix" fi url=${2}/${path} shift 2 fi tarball=$tmpDir/$path require_util tar "unpack the binary tarball" if [ "$(uname -s)" != "Darwin" ]; then require_util xz "unpack the binary tarball" fi if command -v curl >/dev/null 2>&1; then fetch() { curl --fail -L "$1" -o "$2"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget "$1" -O "$2"; } else oops "you don't have wget or curl installed, which I need to download the binary tarball" fi echo "downloading $path binary tarball for $system from '$url' to '$tmpDir'..." fetch "$url" "$tarball" || oops "failed to download '$url'" unpack=$tmpDir/unpack mkdir -p "$unpack" tar -xJf "$tarball" -C "$unpack" || oops "failed to unpack '$url'" script=$(echo "$unpack"/xip) create_dir() { dest="$1" if ! [ -e "$dest" ]; then cmd="mkdir -m 0755 $dest && chown $USER $dest" echo "directory $dest does not exist; creating it by running '$cmd' using sudo" >&2 if ! sudo sh -c "$cmd"; then echo "$0: please manually run '$cmd' as root to create $dest" >&2 exit 1 fi fi } create_dir /opt create_dir /opt/gordian create_dir /opt/gordian/bin [ -e "/opt/gordian/bin/xip" ] || cp "$unpack"/xip /opt/gordian/bin/xip [ -e "$script" ] || oops "installation script is missing from the binary tarball!" export INVOKED_FROM_INSTALL_IN=1 if [ -n "$@" ]; then "$script" check all "$script" setup else "$script" "$@" fi } # End of wrapping