#!/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=825c695dd611327b50420d9995b7eafe8dd6d5ebb79a8e1086c8441059b3a464
        path=n33p6kyw62b8185gnmrgxplvpfsbkmam/nix-2.29.4-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=33fc66372493977da16cf57ed407d5985ffc6b6f594ddddb81e619dfd2928830
        path=l3rmqrsrbwq39pwgfzdh0lkyaz35fxr5/nix-2.29.4-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=4d0a0f3ef6b635551a4c77f2b2cdd27917c96aa55e4bc960f57a05b128845a63
        path=1xvniyg9w0nsihwbi6k3ad76rcwf8nj4/nix-2.29.4-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=09e92127a91f1032c22ed6d4dc011997f00f5bc89a5af2ab1cd06d4da5b91d51
        path=qk5dadc4p4mjw8sqlybd8kg0nc2p02mg/nix-2.29.4-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=bfe2ba04812f0f1dbf0f4360a736c8d187c9c9b20152296fd8ee7ea68271efc7
        path=xxj71a81nh8wmc864j5lf3wrdwcfya36/nix-2.29.4-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=7bd1a6c8b8735ea01ea9252f6647dec32931d322795baf19dd5b8a4f6e6f5d4e
        path=aqh04b9znf5y3j9w3zkg66xsmks65mxs/nix-2.29.4-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=39901863e1d73ed72f4a27a0da8251d9a6431360f92c90042da46efa9a76463c
        path=p7i0brvkadkdmh64p16k17rkvbx85zkn/nix-2.29.4-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=987d1c57d38c412199f0fe5c18d57597507adc828eedb8e288ee360d27fcaa25
        path=f8vqs0xyj2mp1q5ax8gw09zrhs1wmq4f/nix-2.29.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.29.4/nix-2.29.4-$system.tar.xz
fi

tarball=$tmpDir/nix-2.29.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.29.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
