#!/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=1f836536715aae53330ff160adfe0e2247db6499f87c68ae38548b457ae3cdf7
        path=6m060hlm1nxfvcq9j6jnaf598gyhpj14/nix-2.20.9-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=23a7798c63619f961eda41bf132168d310889fb2ceaacc128a1055d4305235ce
        path=c8j5bc722riljixj4w0xmip635mv9m1m/nix-2.20.9-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=1d7cdf4a069824c68b1a670198a53dfe6adc9e44e49b0aa3509fe47b5726b059
        path=w5dr6nl540s9nr4a3bqh1rpsk61rsd40/nix-2.20.9-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=8759c25f488c40ea3b9cdda92ea33da2497bddd153f4d48b5c39264f91372bd8
        path=kmrnsi3gqkb06p5z69r95zdmnkbfjyqx/nix-2.20.9-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=dbe0ee10775503706f5ab49989c81ecf04a199887770568b3cdcb13d0c79a530
        path=k5wiszaffs7zydd77ja5w8bpc3imy6da/nix-2.20.9-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Darwin.x86_64)
        hash=ab8f7e4457d3bc97fdfa5e35228d371e401004c5c73bb0225ebebbdf08cd9740
        path=86vnfg991qv23dq90n5k9kd1f6kb3q9k/nix-2.20.9-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=6e06aeaf5464512955dcf71ce091f19577aa7299f73aaabfee4d4361f9b439f6
        path=fhnq8cnfq3a64g4hjnwj0rcb53ngyji7/nix-2.20.9-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.20.9/nix-2.20.9-$system.tar.xz
fi

tarball=$tmpDir/nix-2.20.9-$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.20.9 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
