# Sourced by _fake when it is invoked as `curl`. Stands in for every remote the
# installer talks to, and for nothing else: a URL this file does not recognise
# is a failed download, not a silent success, so a typo'd host or a moved path
# shows up as the failure it would really be.
#
# Knobs, all set by the caller:
#   FAKE_BOOT_BLOB   file whose contents the bootstrap endpoint returns on 200
#   FAKE_BOOT_PW     the password that gets a 200; anything else gets a 401
#   FAKE_BOOT_CODE   force a code (404, 000, 500 ...) whatever the password
#   FAKE_BWS_SUMS    match (default) | mismatch | absent
#   FAKE_FAIL=curl:deb | curl:chezmoi | curl:fzf | curl:docker | curl:claude
#             | curl:rustup | curl:font | curl:claude-desktop
#                    fail just that one download

_fb=$(dirname "$0")                       # the fakebin directory
url=; out=; prev=; readcfg=0
for a in "$@"; do
	case $prev in -o) out=$a ;; -K) [ "$a" = - ] && readcfg=1 ;; esac
	case $a in http://*|https://*|get.chezmoi.io|*.io|*.sh) [ -z "$url" ] && url=$a ;; esac
	prev=$a
done
cfg=
[ "$readcfg" = 1 ] && cfg=$(cat)          # always drain: the writer is a pipe

# Where a download lands is itself under test (DU-H2: the tarballs used to go to
# /tmp/nvim.tgz and be unpacked by `sudo tar`). The directory is private to the
# run and removed before it exits, so its mode can only be observed from in
# here. Recorded beside the argv line _fake has already written.
[ -n "$out" ] && printf 'curl-outdir %s %s\n' "$(dirname "$out")" \
	"$(stat -c %a "$(dirname "$out")" 2>/dev/null || echo unknown)" \
	>> "${DOTUP_TEST_LOG:?DOTUP_TEST_LOG unset}"

emit() { if [ -n "$out" ]; then cat > "$out"; else cat; fi; }

# A tiny installer script, printed the way get.chezmoi.io and astral.sh print
# theirs. It installs the FAKE of the same name, so whatever it drops behaves
# like every other fake in this directory.
installer() { # $1 tool, $2 default dir
	printf '%s\n' \
		"b=$2" \
		'while [ $# -gt 0 ]; do case $1 in -b) b=$2; shift 2 ;; *) shift ;; esac; done' \
		"mkdir -p \"\$b\" && ln -sf $_fb/_fake \"\$b/$1\" && echo \"installed $1 to \$b\""
}

case $url in
*get.chezmoi.io*)
	[ "$mode" = chezmoi ] && exit 22
	installer chezmoi "\$HOME/.local/bin"; exit 0 ;;
*astral.sh/uv/install.sh*)
	installer uv "\$HOME/.local/bin"; exit 0 ;;
*get.docker.com*)
	# The vendor's convenience script. It installs the engine and both plugins
	# in one pass, so the stand-in drops a single `docker` fake -- `docker
	# buildx` and `docker compose` are subcommands of that binary, not separate
	# ones. Through emit, because the caller downloads this to a file and runs
	# the file rather than feeding it to a shell through a pipe: POSIX sh has no
	# pipefail, so the pipe form cannot tell a failed download from a good one.
	# FAKE_FAIL=curl:docker is that download failing.
	[ "$mode" = docker ] && exit 22
	installer docker "\$HOME/.local/bin" | emit; exit 0 ;;
# Claude Code's native installer and rustup-init, both downloaded to a file
# and run from it, the docker shape. Each stand-in drops the fake of the same
# name where the real one would put it, so the post-install `have` is real.
https://claude.ai/install.sh)
	[ "$mode" = claude ] && exit 22
	installer claude "\$HOME/.local/bin" | emit; exit 0 ;;
https://sh.rustup.rs)
	[ "$mode" = rustup ] && exit 22
	installer rustup "\${CARGO_HOME:-\$HOME/.cargo}/bin" | emit; exit 0 ;;
# The Claude Desktop apt repository's signing key. Its fingerprint is what the
# fake gpg reports, so the key's content here does not matter -- only that it
# arrived.
https://downloads.claude.ai/claude-desktop/key.asc)
	[ "$mode" = claude-desktop ] && exit 22
	printf -- '-----BEGIN PGP PUBLIC KEY BLOCK-----\nfake\n' | emit; exit 0 ;;
# A Nerd Fonts release: a real .tar.xz holding one .ttf named for the family,
# so the extract and the "already installed" probe both have something to see.
https://github.com/ryanoasis/nerd-fonts/releases/latest/download/*.tar.xz)
	[ "$mode" = font ] && exit 22
	f=${url##*/}; f=${f%.tar.xz}
	d=$(mktemp -d); : > "$d/${f}NerdFont-Regular.ttf"
	tar -cJf - -C "$d" . | emit; rm -rf "$d"; exit 0 ;;
*go.dev/VERSION*)
	echo go1.99.0; exit 0 ;;
*api.github.com*)
	echo '  "browser_download_url": "https://example.invalid/fake_amd64.deb"'; exit 0 ;;

# ------------------------------------------------------------------- fzf ----
# Only the real release path answers. The tarball is built here from the
# version in the URL, so `dotup preflight` reporting "0.74.2" is evidence that
# the pinned version travelled through the URL and into the binary -- not that
# a fixture happened to say so.
https://github.com/junegunn/fzf/releases/download/*)
	[ "$mode" = fzf ] && exit 22
	f=${url##*/}                                   # fzf-0.74.2-linux_amd64.tar.gz
	v=${f#fzf-}; v=${v%%-*}
	d=$(mktemp -d); printf '#!/bin/sh\ncase ${1:-} in --version) echo "%s (fake)" ;; esac\nexit 0\n' "$v" > "$d/fzf"
	chmod 755 "$d/fzf"; tar -czf - -C "$d" fzf; rm -rf "$d"; exit 0 ;;

# ------------------------------------------------------------------- bws ----
*/sdk-sm/releases/download/*bws-sha256-checksums-*)
	case ${FAKE_BWS_SUMS:-match} in
	absent) exit 22 ;;
	mismatch) s=0000000000000000000000000000000000000000000000000000000000000000 ;;
	*) s=$(printf 'PK-fake-bws-zip\n' | sha256sum | cut -d' ' -f1) ;;
	esac
	v=${url##*bws-sha256-checksums-}; v=${v%.txt}
	for t in x86_64-unknown-linux-musl aarch64-unknown-linux-musl macos-universal; do
		printf '%s  bws-%s-%s.zip\n' "$s" "$t" "$v"
	done | emit
	exit 0 ;;
*/sdk-sm/releases/download/*.zip)
	printf 'PK-fake-bws-zip\n' | emit; exit 0 ;;

# -------------------------------------------------------------- bootstrap ---
*/bootstrap.env)
	pw=$(printf '%s\n' "$cfg" | sed -n 's/^user = "[^:]*:\(.*\)"$/\1/p' | head -1)
	code=${FAKE_BOOT_CODE:-}
	if [ -z "$code" ]; then
		if [ "$pw" = "${FAKE_BOOT_PW:-}" ]; then code=200; else code=401; fi
	fi
	if [ "$code" = 200 ]; then cat "${FAKE_BOOT_BLOB:?FAKE_BOOT_BLOB unset}"; fi
	printf '\n%s' "$code"
	[ "$code" = 000 ] && exit 7
	exit 0 ;;
esac

# --------------------------------------------------------------- anything ---
# A download to a file still has to produce the file; a download of something
# this stand-in has never heard of is a failure, which is the honest answer.
case $url in
*.deb|*.tar.gz|*.tgz)
	[ "$mode" = deb ] && exit 22
	[ -n "$out" ] && { : > "$out"; exit 0; }
	exit 0 ;;
esac
[ -n "$out" ] && { : > "$out"; exit 0; }
exit 22
