#!/bin/zsh

d=${0:A:h}
INCOMING=$d/incoming # incoming relative to THIS script

REPREPRO=reprepro

for f ($INCOMING/*.changes) {

	< $f perl -ne 'if(/Distribution: *([a-z]+)/m) { print $1; exit;}' | read distro
	test -z "$distro" && { echo "Couldn't parse distro"; exit 1 }

	test -d $d/$distro || { echo "Distro directory $d/$distro doesn't exist"; exit 1}

	$REPREPRO -T deb --ignore=surprisingbinary --ignore=missingfile --ignore=wrongdistribution -V -b $d/$distro/public include $distro $f || \
	    { echo "$REPREPRO failed; exit 1" }
	rm $f
} |& ts | tee -a $d/import.log
