#!/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=b83f5293e3674980dc0c882f192e65ac9e975ba95e35fad8b8b6bd11a06e8b27
        path=1hac5x9pq42blycbhargppwglh6l262w/nix-2.24.2-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=b6e14bd7b9f67e20035c56abf021220d376758f25886fba8cb7e8bbdcff13429
        path=y9dw24l9ak0zb43jgr14j9dl8s3s8mxd/nix-2.24.2-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=6e879f9c9f63bd5e168f4a5067a7ac5936047773fdd6bc8cf1de55443fee44ce
        path=sn85p657cmfnf6jp7i7gz9f4dlsbvnn1/nix-2.24.2-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=489526a0a0f2d76b64cf3a09a227a37a0a66b99e6ad25440c9b5bcc26ac12778
        path=fcq7dx29kqgjj8l44x0c869nx4a5mrzc/nix-2.24.2-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=1fbeb118d3062d8fc35d4cf787345168252968adf38072336b968273b5ebf626
        path=h8c0np7w7wffdr3ha6g8gmizxfxgybvs/nix-2.24.2-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=9cbf1b94a1bfc99920d2f7def7447b9963f59b5f71b9a214858381e312a6502f
        path=8pp1c7kjfn312vmbq9zrwc1zv4vz01w5/nix-2.24.2-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=a0d2ef2fec8fcc9ff8fa1285a016942134b5e7491925ea2bd3457362faca8229
        path=4qa1ym2wf0qksqjsrkhzg2ly842dkwiz/nix-2.24.2-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=1c0fa88e6c0e2f46172bab584769ea1986d83e76af3edd5a703998ca9b182c57
        path=cwa2fqvi6mhkrmaaww3gxd48akgn1hcd/nix-2.24.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.24.2/nix-2.24.2-$system.tar.xz
fi

tarball=$tmpDir/nix-2.24.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.24.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
