#!/bin/sh
set -e
release="$1"
arch="$2"

dir="$(dirname $0)/../$1"
if [ "$arch" ]; then
	dir="$dir.$arch"
else
	arch=amd64
fi
[ -d "$dir" ] || mkdir"$dir" 
cd "$dir" 

[ -e root ] || mkdir -p root/etc/apt/ root/var/lib/apt/lists root/etc/apt/preferences.d root/var/lib/dpkg
[ -e root/var/lib/dpkg/status ] || touch root/var/lib/dpkg/status

cat > root/etc/apt/sources.list << EOF

deb http://snapshot.ubuntu.com/ubuntu ${release} main universe restricted multiverse
deb http://snapshot.ubuntu.com/ubuntu ${release}-updates main universe restricted multiverse
deb http://snapshot.ubuntu.com/ubuntu ${release}-proposed main universe restricted multiverse
# temporary workaround for pre-t64 migration packages getting lost between proposed and release
#deb http://snapshot.ubuntu.com/ubuntu/20240327T235959Z ${release}-proposed main universe restricted multiverse
deb-src http://snapshot.ubuntu.com/ubuntu ${release} main universe restricted multiverse
deb-src http://snapshot.ubuntu.com/ubuntu ${release}-proposed main universe restricted multiverse
#deb-src http://snapshot.ubuntu.com/ubuntu/20240327T235959Z noble-proposed main universe restricted multiverse
EOF


apt-get -qq -o Dir=$PWD/root -o APT::Architecture=$arch -o Dir::Etc::trustedparts=/etc/apt/trusted.gpg.d update

if [ -e global-ben.py ]; then
	python3 global-ben.py > global-ben.txt
fi
if [ -e removal-candidates.py ]; then
	python3 removal-candidates.py --log removal-candidates.log > removal-candidates.txt
	python3 removal-candidates.py --log removal-candidates.with-proposed.log --proposed > removal-candidates.with-proposed.txt                                                 
	python3 removal-candidates.py --log removal-candidates.with-proposed-missing.log --proposed --proposed-missing > removal-candidates.with-proposed-missing.txt
fi

if [ -e missing-rebuilds.py ]; then
	python3 missing-rebuilds.py  > missing-rebuilds.txt
fi

bg() { for i in "$@"; do if echo $i | grep -q 202403; then continue; fi; echo --bg deb://$i; done }
fg() { for i in "$@"; do if echo $i | grep -q 202403; then continue; fi; echo --fg debsrc://$i; done }
fgb() { for i in "$@"; do if echo $i | grep -q 202403; then continue; fi; echo --fg deb://$i; done }

mk() {
	file=$1
	shift

	grep Date root/var/lib/apt/lists/*_${release}_InRelease | sed s#Date#date#g > $file.new        
        grep Date root/var/lib/apt/lists/*_ubuntu_dists_${release}-proposed_InRelease | sed s#Date#date-proposed#g >> $file.new

	echo "command: $@" >> $file.new
	"$@" >> $file.new || true
	mv $file.new $file
}


mk proposed-against-release-binaries.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages) $(fgb root/var/lib/apt/lists/*_${release}_*Packages root/var/lib/apt/lists/*_${release}-proposed_*Packages)
python3 ../bin/human-report.py proposed-against-release-binaries.yml "release + proposed  binaries against release binaries"  > proposed-against-release-binaries.txt

mk release-binaries.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages) $(fgb root/var/lib/apt/lists/*_${release}_*${arch}_Packages) 
python3 ../bin/human-report.py release-binaries.yml "release binaries against release binaries"  > release-binaries.txt

#mk updates-binaries.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages)  $(bg root/var/lib/apt/lists/*_${release}-updates_*${arch}_Packages) $(fgb root/var/lib/apt/lists/*_${release}_*${arch}_Packages)  $(fgb root/var/lib/apt/lists/*_${release}-updates_*${arch}_Packages)
#python3 ../bin/human-report.py updates-binaries.yml "release+updates binaries against release+updates binaries"  > updates-binaries.txt

mk release.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages) $(fg root/var/lib/apt/lists/*_${release}_*Sources)
python3 ../bin/human-report.py release.yml "release sources against release binaries"  > release.txt
python3 ../bin/human-report.py release.yml "release sources against release binaries" --only-regressions  > release.only-regressions.txt

mk proposed-against-release.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages) $(fg root/var/lib/apt/lists/*_${release}_*Sources root/var/lib/apt/lists/*_${release}-proposed_*Sources)
python3 ../bin/human-report.py proposed-against-release.yml "release + proposed sources against release binaries"  > proposed-against-release.txt
python3 ../bin/human-report.py proposed-against-release.yml "release + proposed sources against release binaries" --only-regressions  > proposed-against-release.only-regressions.txt

mk proposed.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages root/var/lib/apt/lists/*_${release}-proposed_*${arch}_Packages) $(fg root/var/lib/apt/lists/*_${release}_*Sources root/var/lib/apt/lists/*_${release}-proposed_*Sources)
python3 ../bin/human-report.py proposed.yml "release + proposed sources against release+proposed binaries"  > proposed.txt
python3 ../bin/human-report.py proposed.yml "release + proposed sources against release+proposed binaries" --only-regressions > proposed.only-regressions.txt


if [ "$arch" = "armhf" ]; then
	mk proposed-binaries.yml dose-distcheck  --deb-native-arch ${arch} -fe --latest 1 $(fgb root/var/lib/apt/lists/*_${release}_*${arch}_Packages root/var/lib/apt/lists/*_${release}-proposed_*${arch}_Packages) $(bg root/var/lib/apt/lists/*_${release}_*${arch}_Packages root/var/lib/apt/lists/*_${release}-proposed_*${arch}_Packages)
	python3 ../bin/human-report.py proposed-binaries.yml "release + proposed binaries against release+proposed binaries"  > proposed-binaries.txt
	python3 ../bin/human-report.py proposed-binaries.yml "release + proposed binaries against release+proposed binaries"  --only-regressions > proposed-binaries.only-regressions.txt
fi

