#!/bin/sh
# Installs f3b, the command-line client for Fast Formal Functorial Build.
#
#   curl -fsSL https://build.functorial.inc/f3b.sh | sh
#
# It downloads one binary for this machine (macOS or Linux, arm64 or x86-64), checks it
# against the SHA-256 published beside it, and puts it in ~/.local/bin (or $F3B_INSTALL_DIR).
# Nothing runs as root and nothing else on the system changes. The rest of this file is a
# single function, called on the last line, so a download cut short runs nothing.
#
# Settings, all optional:
#   F3B_INSTALL_DIR   where the binary goes (default ~/.local/bin)
#   F3B_DOWNLOAD      where binaries are fetched from (default https://build.functorial.inc/dl)

set -eu

main() {
	base="${F3B_DOWNLOAD:-https://build.functorial.inc/dl}"
	dir="${F3B_INSTALL_DIR:-$HOME/.local/bin}"

	say() { printf 'f3b: %s\n' "$*" >&2; }
	die() { say "$*"; exit 1; }
	need() { command -v "$1" >/dev/null 2>&1 || die "this needs \`$1\`, which isn't installed"; }

	need uname
	need tar
	need mktemp

	os="$(uname -s)"
	arch="$(uname -m)"
	case "$os" in
		Darwin)
			# A shell under Rosetta reports x86_64 on Apple silicon: install the native one.
			if [ "$arch" = x86_64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = 1 ]; then
				arch=arm64
			fi
			case "$arch" in
				arm64 | aarch64) target=aarch64-apple-darwin ;;
				x86_64) target=x86_64-apple-darwin ;;
				*) die "no f3b for macOS on $arch yet" ;;
			esac
			;;
		Linux)
			case "$arch" in
				x86_64 | amd64) target=x86_64-unknown-linux-musl ;;
				aarch64 | arm64) target=aarch64-unknown-linux-musl ;;
				*) die "no f3b for Linux on $arch yet" ;;
			esac
			;;
		*) die "no f3b for $os yet: macOS and Linux only" ;;
	esac

	if command -v curl >/dev/null 2>&1; then
		fetch() { curl --proto '=https' --tlsv1.2 -fsSL "$1" -o "$2"; }
	elif command -v wget >/dev/null 2>&1; then
		fetch() { wget --https-only -q "$1" -O "$2"; }
	else
		die "this needs curl or wget"
	fi

	if command -v sha256sum >/dev/null 2>&1; then
		sha() { sha256sum "$1" | cut -d' ' -f1; }
	elif command -v shasum >/dev/null 2>&1; then
		sha() { shasum -a 256 "$1" | cut -d' ' -f1; }
	else
		die "this needs sha256sum or shasum to check the download"
	fi

	tmp="$(mktemp -d)"
	trap 'rm -rf "$tmp"' EXIT INT TERM

	archive="f3b-$target.tar.gz"
	say "downloading $base/$archive"
	fetch "$base/$archive" "$tmp/$archive" || die "couldn't download $base/$archive"
	fetch "$base/SHA256SUMS" "$tmp/SHA256SUMS" || die "couldn't download $base/SHA256SUMS"

	want="$(awk -v f="$archive" '$2 == f || $2 == "*" f { print $1 }' "$tmp/SHA256SUMS")"
	[ -n "$want" ] || die "SHA256SUMS has no line for $archive"
	got="$(sha "$tmp/$archive")"
	[ "$got" = "$want" ] || die "the download's SHA-256 is $got, not $want: not installing it"

	tar -xzf "$tmp/$archive" -C "$tmp"
	[ -f "$tmp/f3b" ] || die "the archive has no f3b in it"

	mkdir -p "$dir"
	# Into place in one step, so a running f3b is never half overwritten.
	cp "$tmp/f3b" "$dir/.f3b.new"
	chmod 755 "$dir/.f3b.new"
	mv -f "$dir/.f3b.new" "$dir/f3b"

	say "installed $("$dir/f3b" version 2>/dev/null || echo f3b) in $dir/f3b"

	case ":$PATH:" in
		*":$dir:"*) ;;
		*)
			say "$dir is not on your PATH. Add it, for example:"
			case "${SHELL:-}" in
				*/fish) say "    fish_add_path $dir" ;;
				*/zsh) say "    echo 'export PATH=\"$dir:\$PATH\"' >> ~/.zshrc" ;;
				*) say "    echo 'export PATH=\"$dir:\$PATH\"' >> ~/.profile" ;;
			esac
			;;
	esac

	say "next: \`f3b topup\` to add credits, then \`f3b build YourFile.lean\`"
}

main "$@"
