#!/bin/sh

# This script installs the Nix package manager on your system by
# downloading a binary distribution and running its installer script
# (which in turn creates and populates /nix).

{ # Prevent execution if this script was only partially downloaded
oops() {
    echo "$0:" "$@" >&2
    exit 1
}

umask 0022

tmpDir="$(mktemp -d -t nix-binary-tarball-unpack.XXXXXXXXXX || \
          oops "Can't create temporary directory for downloading the Nix 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
    Linux.x86_64)
        hash=c544da740a879f9fa0cd88e16c1e3542aa0a04dfbf330f3c081e5cdbb9cc8dcc
        path=qnhl2nc4riv42qzbzk1qlv85rn33gbs8/nix-2.30.4-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=e92095b1d409aed206c7000d88b299464126bb8e72fbb87c78f7978e32e74927
        path=yx08z2pa0v8as8mrcbnkjrz957adxmyz/nix-2.30.4-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=0bf999287a8c0c2686c48303193582f02b85677dd8d13eb9c92fafc0f14746d0
        path=7c5pz30c8zhkvhqy6y1cx40wf3v7bwqv/nix-2.30.4-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=1024809c2724c430fbafa21e6e70741797938a87690e43a53e92e3d89c439622
        path=rr6058a7sz1l14mjnp2glvp6ih41pi67/nix-2.30.4-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=a21b55ed78b9a44ff75696c0c35b501ad36de30fb918e8fa9a04dece6db80769
        path=1f7cjgg1v8jv0bcq4z2nn86jnwplc20m/nix-2.30.4-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=48272f229052586327f23fbf88297c7341c2cb2e805de73d70d2ee8156176c26
        path=1wmkvhl517h9hzamyfbkxls62jnabbbl/nix-2.30.4-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=adcea6dc6c2d36a917fd51eb408fee908d16aa1308f51b8925dcda776ae701d1
        path=5fp6h9ja7041fa36mmzyijla0n59wi69/nix-2.30.4-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=0cef9677e985cbcba947f146c022cb948e737fbecce8c797b2c8a1fb13b4b1f9
        path=8p2cpckzaqz8lpxlk8dw5yfa7jyf4ikx/nix-2.30.4-aarch64-darwin.tar.xz
        system=aarch64-darwin
        ;;
    *) oops "sorry, there is no binary distribution of Nix for your platform";;
esac

# 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
else
    url=https://mirrors.tuna.tsinghua.edu.cn/nix//nix-2.30.4/nix-2.30.4-$system.tar.xz
fi

tarball=$tmpDir/nix-2.30.4-$system.tar.xz

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 Nix 2.30.4 binary tarball for $system from '$url' to '$tmpDir'..."
fetch "$url" "$tarball" || oops "failed to download '$url'"

if command -v sha256sum > /dev/null 2>&1; then
    hash2="$(sha256sum -b "$tarball" | cut -c1-64)"
elif command -v shasum > /dev/null 2>&1; then
    hash2="$(shasum -a 256 -b "$tarball" | cut -c1-64)"
elif command -v openssl > /dev/null 2>&1; then
    hash2="$(openssl dgst -r -sha256 "$tarball" | cut -c1-64)"
else
    oops "cannot verify the SHA-256 hash of '$url'; you need one of 'shasum', 'sha256sum', or 'openssl'"
fi

if [ "$hash" != "$hash2" ]; then
    oops "SHA-256 hash mismatch in '$url'; expected $hash, got $hash2"
fi

unpack=$tmpDir/unpack
mkdir -p "$unpack"
tar -xJf "$tarball" -C "$unpack" || oops "failed to unpack '$url'"

script=$(echo "$unpack"/*/install)

[ -e "$script" ] || oops "installation script is missing from the binary tarball!"
export INVOKED_FROM_INSTALL_IN=1
"$script" "$@"

} # End of wrapping
