#!/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=5f3d720283c8c0c0e924a7f4dd44c029f2823806d8293d65e6dc610d4c57b5dc
        path=ap8lnwxqg31c1aip01as6fq5w68zjr9l/nix-2.25.5-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=194351c457c5362048f344777a1e41d8746ee4758e45c0120aadc4e523cc1c47
        path=745n7s4rzk9ppl397k03hap29kx754r0/nix-2.25.5-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=725f9fa1a5fd717eedb4f2283a6bd004edd9001b9af53443d8beb9f790d23f91
        path=clqs86bl8zx5x18p6cmamfardx4cns89/nix-2.25.5-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=9b309636310a252d76fee3e19b19a589ef916c2c154c88c3f9cc876d7878093c
        path=0aj1257nl5kdml6lq8bw5rh67615fi0j/nix-2.25.5-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=2f5eacb8436353c3c206ca4ad4f5dccb7968cac978242c7631f3c37c3303a51f
        path=hpaxvs38mwqhviwrj1qmjbx0mz8ld64m/nix-2.25.5-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=cd26987a55e6a295c98c300f3251f20114e3cb37f09ae0f8063b3ebd58e27c7b
        path=jwa8pq87v122vc50a4ciy2qzfv3k5mcz/nix-2.25.5-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=3727e831bb300fc355f86ed5f83f8890d8fd93b9c0176887310d0e6162b76d04
        path=x0swidpc4n6x5xh1dwmgdrmfnsaihkq8/nix-2.25.5-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=447c54bbbed51fc4950f636070d4b873a323e0714adfed0132eb48f7a8cdf342
        path=yrya1pb658jxx2i1ml4ifq29afj8zfbn/nix-2.25.5-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.25.5/nix-2.25.5-$system.tar.xz
fi

tarball=$tmpDir/nix-2.25.5-$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.25.5 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
