#!/bin/bash -e
# Precondition: Have ``apt-cacher-ng`` installed, default config

# Phaenomena description: Running this (on a fairly fast and parallel host) yields to ``apt-get update`` to stall
#
# If that happens, ``ps aux | grep method`` will then show existing/standing ``/usr/lib/apt/methods/http``, ``/usr/lib/apt/methods/gpgv`` processes
# Killing any of these will make apt continue (with an error)

# Note: Acquire::http::Pipeline-Depth and Acquire::QueueMode don't seem to change anything
# Note: Using ``parallel --jobs=1`` remedies the bug

read -p "Will create chroots in $(pwd) and do other dirty sudo thing (Ctl-C to abort)"

# At least two working apt lines are needed to trigger bug
APT_SOURCES_LIST="\
deb [] http://localhost:3142/debian sid main contrib non-free non-free-firmware
deb [] http://localhost:3142/debian experimental main contrib non-free non-free-firmware
"

CHROOTS="sid0 sid1"

for C in ${CHROOTS}; do
	[ -e "${C}" ] || sudo debootstrap sid "${C}" http://localhost:3142/debian/

	# Configure sources.list for local apt-cacher-ng with at least two workng lines
	printf "%s" "${APT_SOURCES_LIST}" | sudo tee ./${C}/etc/apt/sources.list

	# Remove any already download source.list files, so proper HTTP download is triggered
	sudo rm -rf "./${C}/var/lib/apt/lists/"
done

# Run apt-get in parallel
seq 0 1 | parallel sudo chroot ./sid{}/ timeout --verbose 10s apt-get update
