#!/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=ed9db592e53faeae9f0a8e879c8c52999d84a045a15814e918f9e72f9dffbf29
        path=lk386bdssqb3w8lbz4jlzhsa2gaslykg/nix-2.27.1-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=78b0089058a68529cb0285273c6f112fc5c6763278cb3278f81852d1c948d6c2
        path=ic12yyircmjhmf11p6p2sxfqx5kgxwsc/nix-2.27.1-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=9150d394609e06b353c386e14da22ea92aecb898a15544549170e0799c64526b
        path=fq9j7834zv70kph30m70fxbc2l8x0hpq/nix-2.27.1-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=3da9e794d738d13bfc99aff767f33f35fb6955ef37fe799e981526968e13e394
        path=5fz0fp5d8x5c88vw2c5kmzmdi724gvsc/nix-2.27.1-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=b824f510f94da9b84af263a2af80a0edaf612c937dd55cbf9226e98c14a47ba8
        path=jsyri48ckv4rf0kb3h1ckxj6ypwq8npw/nix-2.27.1-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=ac3a3191b1d97b5bed06ff38a41fb5aff6cadb220c2382ba5384cffe25795df3
        path=g5kazpvz1zz4g4xygj32is7z6dszpq1i/nix-2.27.1-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=eceaaf585800b5e73a3f57fa9667cd5686fee3395c01a4bdd66321a446885934
        path=j5h9zm9bc79w0i6fprs54rkk1cgj4x4l/nix-2.27.1-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=224404af9b08ebfd25ecdc49a4c702c39f92af68a847a7e3df5ebd7333120d1a
        path=l465xlmqwg3r0mvk24pcknn0izzpl8yb/nix-2.27.1-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.27.1/nix-2.27.1-$system.tar.xz
fi

tarball=$tmpDir/nix-2.27.1-$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.27.1 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
