#!/bin/bash
# Forwarded to Debian: https://bugs.debian.org/824904
set -e

print_bts_usertags_cmdline () {
	bad="$(basename "$2")"
	good="$1"
	echo -n "bts user $bad , "
	for tag in $(sed -n "s/Tag: //p" "$2") ; do
		for bug in $(sed -n "/^Tag: $tag/,/^$/{/^Tag: /d;p}" "$2" | grep -o '[0-9]*') ; do
			echo -n "usertags $bug - $tag , "
		done
	done
	echo -n "user $good , "
	for tag in $(sed -n "s/Tag: //p" "$2") ; do
		for bug in $(sed -n "/^Tag: $tag/,/^$/{/^Tag:/d;p}" "$2" | grep -o '[0-9]*') ; do
			echo -n "usertags $bug + $tag , "
		done
	done
	echo
}

find_and_correct () {
	local f
	test -d "$1" || return 0
	find "$1/" -type f | while read f ; do
		print_bts_usertags_cmdline "$(basename "$f" | sed "$2")" "$f"
	done
}

download () {
	rsync --timeout=60 rsync://bugs-mirror.debian.org/bts-spool-index/user/*/"$1" "$2/" 2>  >(grep -vE 'No such file or directory|see previous errors' 1>&2) || true
}

dir="$(mktemp --tmpdir --directory debian-usertags-tests-XXXXXXXX)"
cleanup () { rm --recursive --force "$dir"; }
trap cleanup EXIT
cd "$dir"
#download "*" all
# tagsregex=$(ssh buxtehude.debian.org "sed '/^1;$/i print join \"|\", @gTags;' < /srv/bugs.debian.org/etc/config | perl")
#tagsregex="patch|wontfix|moreinfo|unreproducible|fixed|potato|woody|sid|help|security|upstream|pending|sarge|sarge-ignore|experimental|d-i|confirmed|ipv6|lfs|fixed-in-experimental|fixed-upstream|l10n|newcomer|etch|etch-ignore|lenny|lenny-ignore|squeeze|squeeze-ignore|wheezy|wheezy-ignore|jessie|jessie-ignore|stretch|stretch-ignore|buster|buster-ignore|bullseye|bullseye-ignore"
#grep -rE '^Tag: ($tagsregex)$' all || true
download '*.or' or
download '*.ort' ort
download '*.ogr' ogr
download '*.rog' rog
download '*.orge' orge
download '*.orrg' orrg
download '*.d.o' d.o
download '*.d.n' d.n
download '*debia[.-]*' debia
download '*debiarn*' debiarn
download '*dbeian*' dbeian
download '*debain*' debain
download '*deban*' deban
download '*.debian' debian
download '*.debian-org' debian-org
download '*.debian-net' debian-net
download '*.debian-com' debian-com
#download '*.debian.net' debian.net
download '*.debian.com' debian.com
download '*packages.qa.debian.org' packages.qa
download '*packge*.debian.org' packge
download '*package.debian.org' package
download '*list.debian.org' list
download '*%40alioth.debian.org' alioth
download '*.debian.org*bugs.debian.org' qa
download 'debian-m68k%40lists.debian.org' 68k
download 'debian-s390x%40lists.debian.org' s390
download 'debian-power%40lists.debian.org' power
download 'debian-ppc%40lists.debian.org' ppc
download '*mulitarch*' mulitarch
find -type f -print0 | xargs -0 rename 's/%40/@/'
find -empty -delete
find_and_correct or 's/\.or$/.org/'
find_and_correct ort 's/\.ort$/.org/'
find_and_correct ogr 's/\.ogr$/.org/'
find_and_correct rog 's/\.rog$/.org/'
find_and_correct orge 's/\.orge$/.org/'
find_and_correct orrg 's/\.orrg$/.org/'
find_and_correct d.o 's/\.d\.o$/.debian.org/'
find_and_correct d.n 's/\.d\.n$/.debian.net/'
find_and_correct debiarn 's/debiarn/debian/'
find_and_correct dbeian 's/dbeian/debian/'
find_and_correct debain 's/debain/debian/'
find_and_correct debia 's/debia\(-.\)/debian\1/'
find_and_correct deban 's/deban/debian/'
find_and_correct debian 's/\.debian$/&.org/'
find_and_correct debian-org 's/debian-org/debian.org/'
find_and_correct debian-net 's/debian-net/debian.net/'
find_and_correct debian-com 's/debian-com/debian.com/'
find_and_correct debian.com 's/\.debian\.com$/.debian.org/'
#find_and_correct debian.net 's/\.debian\.net$/.debian.org/'
find_and_correct packages.qa 's/packages\.qa\.debian\.org$/packages.debian.org/'
find_and_correct package 's/package\.debian\.org$/packages.debian.org/'
find_and_correct packge 's/packges\?\.debian\.org$/packages.debian.org/'
find_and_correct list 's/list\.debian\.org$/lists.debian.org/'
find_and_correct qa 's/\.debian\.org@bugs\.debian\.org$/.debian.org@packages.debian.org/'
find_and_correct alioth 's/@alioth\.debian\.org$/@lists.alioth.debian.org/'
find_and_correct 68k 's/m68k/68k/'
find_and_correct s390 's/s390x/s390/'
find_and_correct power 's/power@/powerpc@/'
find_and_correct ppc 's/ppc/powerpc/'
find_and_correct mulitarch 's/mulitarch/multiarch/'
