#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Bryan Joshua Pedini

# fake apt-get (unit tests). Scenario: FAKE_APT_SIM=fixture|held|real|none|purge (data/apt/sim-*.txt).
# Purge simulation: installed = the "ii" rows of data/apt/dpkg-status.tsv; reverse dependencies from
# data/apt/purge-rdeps.txt; pre-existing orphans from data/apt/autoremovable.txt (added by
# --auto-remove, which is system-wide like the real one); FAKE_APT_AUTO_ORPHANS="pkg:dep ..." are
# the auto-installed dependencies a purge of pkg orphans; FAKE_APT_SIM_EXTRA="pkg:other ..." makes a
# simulated purge of pkg also remove other (a dependent apt-cache rdepends did not report).
# shellcheck source=../fakelib.bash
. "${BASH_SOURCE[0]%/*}/../fakelib.bash"
fake_log "$@"
args=" $* "
D=$FAKE_DATA/apt

installed() { grep -q "^.i *"$'\t'"$1"$'\t' "$D/dpkg-status.tsv" && ! fake_purged "$1"; }
orphans() { while IFS= read -r o; do installed "$o" && printf '%s\n' "$o"; done <"$D/autoremovable.txt"; }

# purge_set [--auto-remove] NAME...: the packages a purge removes, sorted
purge_set() {
	local auto=0 n r line
	if [[ $1 == --auto-remove ]]; then
		auto=1
		shift
	fi
	{
		for n in "$@"; do
			installed "$n" || continue
			echo "$n"
			while IFS= read -r line; do
				[[ $line == "$n: "* ]] || continue
				for r in ${line#*: }; do installed "$r" && echo "$r"; done
			done <"$D/purge-rdeps.txt"
			if ((auto)); then
				for r in ${FAKE_APT_AUTO_ORPHANS:-}; do [[ ${r%%:*} == "$n" ]] && installed "${r#*:}" && echo "${r#*:}"; done
			fi
			for r in ${FAKE_APT_SIM_EXTRA:-}; do [[ ${r%%:*} == "$n" ]] && installed "${r#*:}" && echo "${r#*:}"; done
		done
		if ((auto)); then orphans; fi
	} | sort -u
}

case $args in
*' update '*)
	rc=$(fake_rc FAKE_APT_UPDATE_RC 0)
	if ((rc)); then
		echo "E: Failed to fetch http://deb.debian.org/debian/dists/bookworm/InRelease  Temporary failure resolving 'deb.debian.org'" >&2
		echo "E: Some index files failed to download. They have been ignored, or old ones used instead." >&2
	fi
	exit "$rc"
	;;
*' -s '*autoremove*)
	while IFS= read -r o; do [[ -n $o ]] && echo "Remv $o [1.0]"; done < <(orphans)
	exit 0
	;;
*' -s '*purge*)
	names=()
	auto=()
	for a in "$@"; do
		case $a in --auto-remove) auto=(--auto-remove) ;; -* | purge) ;; *) names+=("$a") ;; esac
	done
	rc=$(fake_rc FAKE_APT_SIM_PURGE_RC 0)
	if ((rc)); then
		echo "E: Unable to correct problems, you have held broken packages." >&2
		exit "$rc"
	fi
	for n in "${names[@]}"; do
		if ! grep -q $'\t'"$n"$'\t' "$D/dpkg-status.tsv" && [[ $n != su-dummy-newdep ]]; then
			echo "E: Unable to locate package $n" >&2
			exit 100
		fi
	done
	while IFS= read -r p; do [[ -n $p ]] && echo "Purg $p [1.0]"; done < <(purge_set "${auto[@]}" "${names[@]}")
	exit 0
	;;
*' -s '*' upgrade '*)
	f=$D/sim-${FAKE_APT_SIM:-fixture}.txt
	while IFS= read -r line; do
		name=
		case $line in Inst\ * | Conf\ *) name=${line#* }; name=${name%% *} ;; esac
		if [[ -n $name ]] && fake_purged "$name"; then continue; fi
		if [[ $line == '  '* && -f $FAKE_DIR/purged ]]; then
			while IFS= read -r p; do line=${line//$p/}; done <"$FAKE_DIR/purged"
		fi
		printf '%s\n' "$line"
	done <"$f"
	exit 0
	;;
*' upgrade '*)
	echo "holds during upgrade: $(sort "$FAKE_DIR/holds" 2>/dev/null | tr '\n' ' ')" >>"$FAKE_LOG"
	# which of our lock files does the package manager have open (it must have none)?
	if [[ -d /proc/$$/fd ]]; then
		n=0
		for fd in "/proc/$$/fd"/*; do
			[[ $(readlink "$fd" 2>/dev/null) == *.lock ]] && n=$((n + 1))
		done
		echo "lock fds in the upgrade: $n" >>"$FAKE_LOG"
	fi
	if [[ -n ${FAKE_APT_UPGRADE_KILL:-} ]]; then
		kill -s "$FAKE_APT_UPGRADE_KILL" "$PPID"
		sleep 0.2
	fi
	if [[ -n ${FAKE_KILL_READER:-} ]]; then
		# close the applet's stdout: kill the process reading it, then keep writing
		pid=$(cat "$FAKE_DIR/reader.pid")
		kill -9 "$pid"
		while kill -0 "$pid" 2>/dev/null; do sleep 0.05; done
	fi
	echo "Reading package lists..." 2>/dev/null
	echo "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded." 2>/dev/null
	exit "$(fake_rc FAKE_APT_UPGRADE_RC 0)"
	;;
*' purge '*)
	rc=$(fake_rc FAKE_APT_PURGE_RC 0)
	if ((rc == 0)); then
		for a in "$@"; do case $a in -* | purge) ;; *) fake_mark_purged "$a" ;; esac; done
		# FAKE_APT_PURGE_ALSO: packages that disappear although the plan did not name them
		read -r -a also <<<"${FAKE_APT_PURGE_ALSO:-}"
		((${#also[@]} == 0)) || fake_mark_purged "${also[@]}"
	else
		echo "E: Held packages were changed and -y was used without --allow-change-held-packages." >&2
	fi
	exit "$rc"
	;;
esac
echo "fake apt-get: unhandled: $*" >&2
exit 99
