#!/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=e12ffe2ce7d858d1ffccdfaa909723d0f9b2de28958a3088a7b238d6c68f17d4
        path=nlngh34w594fw329rfimjms5npnaav3d/nix-2.26.2-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=c992fd19c59fb784e53637d9b41dbd0f8665f9ea9c8a7a510f937e78962386db
        path=9ylv1ldkckr4c4paqhhr3n1kdfg4cspz/nix-2.26.2-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=54d2b8783aded136d160440946ed3e4296a913da091543852b41a4e7c447ebde
        path=4myiq5dny5k51z6kxf3ay0apj6p4ibm6/nix-2.26.2-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=4ad45c059a5c11d51d01376a3a51aa05b042441c7b0cd044477ec7558886bcf3
        path=khjdfb31x3m6ljz3ah8z0xrxrjxwm6ka/nix-2.26.2-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=ddc7a19333c2593bd8db6c02921688f43aa1d9b7f029d165d8f20d4d8e168c5b
        path=j2bvkqmkbabkr21ih052y7k4h06df233/nix-2.26.2-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=610b3307dac45a8b045afca4eb3c69987e4bc55c29d05487355ea7ffb70429a0
        path=ibzvgrjx13risb9c1n11525p0cfqjsw5/nix-2.26.2-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=5e20f3c48a060e88ed4ab9bb923d7030762d742c60fa22ab6dbb13d6352624d2
        path=0avfp5cnwkh9dv33qwibr3sz7nmfwrsp/nix-2.26.2-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=e0f9e1775fa2acf0bdd894f0bc8e1e926dce42823c19662a1e5d4144ba5e5ac0
        path=1q9hil0gipfxndz3pq6jg4ggmn0syia8/nix-2.26.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.26.2/nix-2.26.2-$system.tar.xz
fi

tarball=$tmpDir/nix-2.26.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.26.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
