#!/bin/bash

aptdir="/var/univention/buildsystem2/apt/ucs_4.3-0"

cleanup() {
	rm -rf "${wd:?Workdir missing}"
}
trap cleanup EXIT
wd=$(mktemp -d)

wget -P "$wd" http://ftp.de.debian.org/debian/dists/stretch/main/source/Sources.gz || exit 1
gunzip "$wd/Sources.gz"

while read -d $'\0' rec; do
	section=$(sed -n 's/^Section: //p' <<<"$rec")
	if [ "$section" = "games" ]; then
		continue
	fi
	
	srcpkg=$(sed -n 's/^Package: //p' <<<"$rec")

	ucsrec=$(grep-dctrl -r -F Package -s "Package,Binary,Version" "^$srcpkg$" "$aptdir/source/Sources")
	if [ -z "$ucsrec" ]; then
		continue
	fi
	ucsver=($(sed -n 's/^Version: //p' <<<"$ucsrec" | sort -r))
	debver=($(sed -n 's/^Version: //p' <<<"$rec" | sort -r))
	echo -e "$srcpkg:\t${ucsver[0]}\t${debver[0]}" >> sources.log
	if dpkg --compare-versions "${ucsver[0]}" lt "${debver[0]}"; then
		echo -e "$srcpkg:\t${ucsver[0]}\t${debver[0]}"
	fi
	continue

	binpkg=$(sed -n 's/^Binary: //p' <<<"$rec")
	if [ -n "$binpkg" ]; then
		IFS=", " binpkg=($binpkg)
	else
		binpkg="$srcpkg"
	fi
done < <(grep-dctrl -r -F Package -s "Package,Binary,Version,Section" "^.*" "$wd/Sources" | sed -e 's/^$/\x00/')

