#!/bin/bash
# upkg-sync
#
# tool to keep Upkg repositories in sync via rsync
#
# Copyright (C) 2004-2007 Raffaele Sandrini, Jürg Billeter
#
# This file is part of Upkg (http://www.upkg.org).
#
# Upkg is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# Upkg 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 Upkg; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Authors:
#   Raffaele Sandrini <rasa at paldo dot org>
#   Jürg Billeter <juerg at paldo dot org>

# Sync two upkg repositories with each other
# uses rsync

[ ! -e /var/lock/upkg-sync ] || exit 0

trap "rm -f /var/lock/upkg-sync" INT TERM

touch /var/lock/upkg-sync

for repo in /etc/upkg-sync.d/*
do
	. $repo

	# Comand execution
	pushd $LOCAL_REPO

	TMPFILE=$(mktemp)
	upkg-repo --needed "$@" . > $TMPFILE

	[ -z "$UPLOAD_LIMIT" ] && BWLIMIT= || BWLIMIT="--bwlimit $UPLOAD_LIMIT"
	RSYNC_PASSWORD=$R_PASSWD rsync -ltzP $BWLIMIT --ignore-existing --files-from=$TMPFILE $LOCAL_REPO $REMOTE_REPO

	[ -z "$DOWNLOAD_LIMIT" ] && BWLIMIT= || BWLIMIT="--bwlimit $DOWNLOAD_LIMIT"
	RSYNC_PASSWORD=$R_PASSWD rsync -ltzP $BWLIMIT --ignore-existing --files-from=$TMPFILE $REMOTE_REPO $LOCAL_REPO

	rm -f $TMPFILE

	popd
done

rm -f /var/lock/upkg-sync

