#!/bin/sh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# $Id: update-sourcedeps 162 2005-11-26 20:53:41Z rleigh $
#

set -e

set +e	# don't die if .sbuildrc is missing
dbfull=$(cat /etc/sbuild.conf /etc/sbuild.conf.local ~/.sbuildrc 2>/dev/null | \
	sed -n '/^\$source_dependencies\>/s/.*"\(.*\)".*/\1/p' | tail -n 1)
set -e

dbdir=${dbfull%/*}
if [ -z "$dbdir" ]; then
    dbdir=/var/lib/sbuild
fi
file=source-dependencies

tmp=/tmp/update-sourcedep.$$
arch=$(dpkg --print-installation-architecture)
host=buildd.debian.org
dir=andrea/$arch

mkdir $tmp
cd $tmp

trap 'rm -rf $tmp ; exit 1' 1 2 3 13 15
trap 'rm -rf $tmp' 0

done=""
for distribution in stable testing unstable; do

	name=$file-$distribution
    if wget --tries=3 -q http://$host/$dir/$name.gz; then
		gunzip $name.gz
		if ! cmp -s $name $dbdir/$name; then
			done="$done $distribution"
			cp $name $dbdir/
			chmod 664 $dbdir/$name
			if [ -x /usr/bin/sourcedeps-makerev ]; then
				/usr/bin/sourcedeps-makerev $dbdir/$name >$dbdir/$name.rev
			fi
		fi
	fi
done

if [ -n "$done" ]; then
	echo "Updated source-dependencies for:$done"
fi

exit 0
