#! /bin/sh
set -e

# Called from update-versions (inside the lock) to process all *.versions
# files in the queue.

SOURCE="$1"

cd "/org/bugs.debian.org/versions/queue/$SOURCE"

find . -maxdepth 1 -name \*.versions -printf '%P\n' | while read x; do
    perl -ne '
	if (/(\w[-+0-9a-z.]+) \(([^\(\) \t]+)\)/) {
	    push @v, "$1/$2";
	}
	END { print join(" ", @v), "\n"; }
    ' "$x" \
	| /org/bugs.debian.org/versions/bin/merge-one-version "$x" \
	|| continue
    pkg="${x%%_*}"
    pkghash="$(echo "$pkg" | cut -b 1)"
    mkdir -p "/org/bugs.debian.org/versions/archive/$SOURCE/$pkghash/$pkg"
    mv "$x" "/org/bugs.debian.org/versions/archive/$SOURCE/$pkghash/$pkg/$x"
done
