#!/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=0ff23ac8437c5d15d08063a0b677c47639ab291988ec95eb603f29dc237eb00b
        path=i71d2zyp9c30v98mvqc4dvmnvb1sbx90/nix-2.23.3-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=b417445a9d0091507a38f5321c4711d9f2a862061347596f13ab9dcbafcaf15b
        path=z201kfh4cab9n36hpg3b708bk15aqi55/nix-2.23.3-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=4d75ef8049ed2cef54dc6b6a40115010fc7773544c10904e198248ed9b6c4b4a
        path=gqlsjys012rw36hmljv5f2jav8y0c4kx/nix-2.23.3-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=8e706706cb7c27a3aca33ec82afb3bc383eac633ad99857380008f7be2193849
        path=pv3rz6nqq5sd3r42h1fm58z0jpzy4rwc/nix-2.23.3-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=c0988eb62334b7736f0ef60b767465f681c0f622b4635b4450436d127239b866
        path=1y8i52p9m7v2lqh3gpq1mjllzbnnghfm/nix-2.23.3-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=ca8758fe631781d4b94af5c60f482d5a6d8279c0f20d85213233447ed6916773
        path=g8ywv3b9binpnhxkp5lv0v60v2kx5jdr/nix-2.23.3-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=2bfa2564bc7814336935155d093653ecd4392239df07df46093b0188a2d1acea
        path=chv9j1lfr92jsy5y12icd6arhsil0fd8/nix-2.23.3-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=af5370b63fdc0c8009ad42446dfc46ad347855fc89256b86b5080ac04c89828f
        path=8jlzxrnl7zfk04r0q1hczr3fpylkwah5/nix-2.23.3-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.23.3/nix-2.23.3-$system.tar.xz
fi

tarball=$tmpDir/nix-2.23.3-$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.23.3 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
