#!/bin/sh
set -e
if [ "$(hostname)" != buxtehude ] ; then
	echo 'This script must be run on bugs-master.debian.org only' > /dev/stderr
	exit 1
fi
mkdir -p /home/pabs/usertags
cd /home/pabs/usertags
mkdir -p $(seq 0 9)
src=/srv/bugs.debian.org/spool/user/
find ! -wholename '*/.git/*' -type f -delete
mkdir -p debian/
find "$src" -type f ! -empty ! -wholename "*/*.tmp/*" | while read -r inp ; do
	out="./${inp##$src}"
	out="${out%\%40*}@${out#*%40}"
	cp "$inp" debian/control
	sed -i 's/^Category:/Package: Category:/' debian/control
	sed -i 's/^Tag:/Package:/' debian/control
	sed -i 's/^Bugs:/Depends:/' debian/control
	wrap-and-sort --short-indent --wrap-always --sort-binary-packages --trailing-comma
	sed -i 's/^Depends:/Bugs:/' debian/control
	sed -i 's/^Package: Category:/Category:/' debian/control
	sed -i 's/^Package:/Tag:/' debian/control
	sort-dctrl -k Tag,Bugs,Category debian/control > "$out"
done
rm debian/control
rmdir debian/
mv */* .
rmdir */
sed -i 's/ $//' *
find ! -wholename '*/.git/*' -empty -type f -delete
test -d .git || git init
git add .
if ! git diff --quiet --cached ; then
        echo Usertags:
        echo
        git diff --cached
        echo
        echo End of usertags
fi
git commit -q -m "Import $(date --rfc-3339=seconds)" > /dev/null || true

