#!/bin/sh

# Update the copy of the debtags information that is shipped with the package

set -ue

OK=true

# Commit or rollback on exit
commit_or_rollback() {
	if [ $OK = true ]
	then
		mv tags-current.gz.tmp tags-current.gz
		cat vocabulary1.tmp vocabulary2.tmp > vocabulary
		rm vocabulary1.tmp vocabulary2.tmp
	else
		rm -f tags-current.gz.tmp vocabulary1.tmp vocabulary2.tmp
	fi
}

trap commit_or_rollback EXIT

cat_tags() {
	if [ $USER = enrico ] && [ -f ../tagdb/tags ]
	then
		cat ../tagdb/tags
	else
		svn cat svn://anonscm.debian.org/debtags/tagdb/tags
	fi
}

cat_voc() {
	if [ $USER = enrico ] && [ -f ../vocabulary/debian-packages ]
	then
		cat ../vocabulary/debian-packages
	else
		svn cat svn://anonscm.debian.org/debtags/vocabulary/trunk/debian-packages
	fi
}

echo -n "Exporting a new version of the reviewed tags from SVN... "
if cat_tags | tagcoll copy | gzip -9 > tags-current.gz.tmp
then
	echo "ok."
else
	echo "failed."
	OK=false
	exit 1
fi

echo -n "Exporting a new version of the vocabulary data from SVN... "
if cat_voc > vocabulary1.tmp
then
	echo "ok."
else
	echo "failed."
	OK=false
	exit 1
fi

echo -n "Exporting a new version of the security team tag vocabulary data from SVN... "
if svn cat svn://anonscm.debian.org/debtags/vocabulary/trunk/security-team > vocabulary2.tmp
then
	echo "ok."
else
	echo "failed."
	OK=false
	exit 1
fi

exit 0
