#!/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=e0e65ccb162f603ce5aa23abf956bc71fca51232c2775a2ea5e4033e069d2e4a
        path=iqcsz6p8j8l4zh5965nq7slj3av7a0k5/nix-2.17.2-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=8bf65031d8114daeb8c112db4cc87b03e93a013596b079326bab63f9740185b6
        path=p6zg0l311v1fr4mgy8yh0wv9rxgihkqr/nix-2.17.2-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=1db407577f92dc49c56572bcdb0cc01d245e4eb72d02f72bbd9096010005ce6f
        path=9f2lrljn670p1s3nq283z7i6w5dsawn6/nix-2.17.2-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=18c165ad576ee7f4e4fdf6c7882b7752892eb8a8189c771d83440d8609a966bd
        path=vqjksxqy0b4i9kz8v9gai94ziw4cjhpz/nix-2.17.2-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=5fb7891f2dfab37a34918ac1cd932afdaaf85cc326fa3cfeb685bdd6abdb43c7
        path=wsw8x2r3vnm9n9zqp0k6pp0l70ar1q8b/nix-2.17.2-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Darwin.x86_64)
        hash=ac3915c9b4c193c81a613e5edda9e3ac8f7eee38fb8a342c0a1a6d2f93b67eec
        path=n5w9mc25cplsbpxnwask99jdwcxh74ya/nix-2.17.2-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=8100d4fd9adb87a4990a7d6b56e17b26414a04f9138f7c2fe38f2f23ed423a31
        path=j91crdgncpmkpazizvsrjiy3afjah0d9/nix-2.17.2-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.17.2/nix-2.17.2-$system.tar.xz
fi

tarball=$tmpDir/nix-2.17.2-$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.17.2 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
