#!/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=c4d1aed088f62aa729823cffa3269e880de6ce6102fecb8909f2821eedb2f2eb
        path=bb3f9xxx21g0s15wcm0fpi2lll507g3p/nix-2.36.0-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=e291dd75537d1004d435c732fbdebf2046ac742d16adb4a51f71fdc6ddefc887
        path=vcxiwqxv9zbmgs8jqy9p16847wxc7pj4/nix-2.36.0-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=ee696971de946d311699f6dd639cccc8c6734aec9a061b8e205de0ce6515e5ae
        path=r76nb4cwm5hsycf9dq8kcv63svbcxbfn/nix-2.36.0-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=4cfae57844f849bc11faa4b75c1a28beb4490a11e0ca05294b61bd414dff27eb
        path=c78n3xgmjp8blvj9ainlii98467r3xk3/nix-2.36.0-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=d88fa6614b2ead5b32a1da7e245cf4b717a1982e5974aa4921fcfd1cc7a21421
        path=flf387vg3b17f7vwd1mi4p452f35pk20/nix-2.36.0-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.ppc64)
        hash=96ecf095b5333cfb53313ec75fa4f7a6d5905ee95a1c53a00505dcdb85f30fb6
        path=8cigmzbkyxrjd4zqa43iff9k7n8k1lp5/nix-2.36.0-powerpc64-linux.tar.xz
        system=powerpc64-linux
        ;;
    Linux.ppc64le)
        hash=2484588db6ecd302313f6c3585466420c5916be0aa0ee43ad091a95de6d1e501
        path=lz8pqrk5rw3pmsg5ynqnx4ck788q5d92/nix-2.36.0-powerpc64le-linux.tar.xz
        system=powerpc64le-linux
        ;;
    Linux.riscv64)
        hash=5fd6601193e47d538a1256149859fe7d862998c39b59a4cd698bd3e0b014fd0a
        path=akvbca46lsfp7qpkn6l0rygn06mxrg8c/nix-2.36.0-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=0e1cadd91de1580f1c5c34d9004b65c6465f0eba055a33c203c305bf68c1e765
        path=j8q99wa59akdgdfhwnmkih3ci2pvr347/nix-2.36.0-aarch64-darwin.tar.xz
        system=aarch64-darwin
        ;;
    FreeBSD.amd64|FreeBSD.x86_64)
        hash=d999d3c8603c51afd3917ad49df882c6c367c71b42be67d00634c36b473f3970
        path=6a922fqdwm9l3qa5p6aryzi75p94qv4k/nix-2.36.0-x86_64-freebsd.tar.xz
        system=x86_64-freebsd
        ;;
    *) 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.36.0/nix-2.36.0-$system.tar.xz
fi

tarball=$tmpDir/nix-2.36.0-$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.36.0 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
