#!/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=c349f0559b7849a2cb3b20b8584e532b3fcf5025092cb6180eb0b99240d7b96c
        path=hc1r0wqdz90ici6ss678zfa6vbnbiycx/nix-2.35.5-x86_64-linux.tar.xz
        system=x86_64-linux
        ;;
    Linux.i?86)
        hash=3e7c5b07554af1ad47e2dceb5278dfa4d87415033be25e7acdf039a83decebf4
        path=hc3ypd4i07d1nzaaw2p7q3a5id68sqr2/nix-2.35.5-i686-linux.tar.xz
        system=i686-linux
        ;;
    Linux.aarch64)
        hash=34b8d0c0658321e63143f892086bd2d9623a81bf96e4ffefc1551092d80caedb
        path=z7jlcjl2c3inh2c8vxkcd7xbdb53ki3r/nix-2.35.5-aarch64-linux.tar.xz
        system=aarch64-linux
        ;;
    Linux.armv6l)
        hash=3ba54838746824218096c5751568e209ab0aed1d3d97aad18440f4ad46e60d9a
        path=07jypsvz36x7yyzbdkbmf9lx3gj3m3yb/nix-2.35.5-armv6l-linux.tar.xz
        system=armv6l-linux
        ;;
    Linux.armv7l)
        hash=80b71495a184d5bae588c5aa75c95d7fd4774a3de496beeab012478b25a51e74
        path=fl3x08bmv26b2ifw5x9yd82izb2zqsm4/nix-2.35.5-armv7l-linux.tar.xz
        system=armv7l-linux
        ;;
    Linux.riscv64)
        hash=735851932e658584de034925de835b899427097de35e106d0935f40fcb71aede
        path=72hd3dphaph74jla2zs9m7cgc3c7lnwn/nix-2.35.5-riscv64-linux.tar.xz
        system=riscv64-linux
        ;;
    Darwin.x86_64)
        hash=9211a715eeb2e97b26951205e14c2b9b91f4025c1d06fe109ac0408127e0d26f
        path=gkaxdc6d2ymf5hnq8wlkl6hzwqvpw419/nix-2.35.5-x86_64-darwin.tar.xz
        system=x86_64-darwin
        ;;
    Darwin.arm64|Darwin.aarch64)
        hash=b17549420c04c17bae04be6be2a0473f67a5473a097fc6097150a3a983ec6343
        path=vf1npd2slmr52jrj0wvi7ixgfvjsd3vm/nix-2.35.5-aarch64-darwin.tar.xz
        system=aarch64-darwin
        ;;
    FreeBSD.amd64|FreeBSD.x86_64)
        hash=76d0516adf823899e6e4f7cf5cb2ed9252a7b903da9b510ca2422d1f6f6dabfb
        path=cds0k2pngp9a4rn67cbsn51ijv4a15fx/nix-2.35.5-x86_64-freebsd.tar.xz
        system=x86_64-freebsd
        ;;
    *) 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.35.5/nix-2.35.5-$system.tar.xz
fi

tarball=$tmpDir/nix-2.35.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.35.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
