#!/bin/bash
#
# Copyright 2002,2003,2004,2005,2008,2010 Bernie Innocenti <bernie@codewiz.org>
#

# Space separated list of architectures (i386, x86_64, ppc...)
#ARCHS="i386 x86_64"
ARCHS="i386"

# Space separated list of Fedora release versions (8, 9, development...)
RELEASES="11 12"

# Wether to mirror the updates-testing too
MIRROR_UPDATES_TESTING=1

RSYNC_OPTS="--delete --delete-after --partial -l -p -t -r --timeout=60"

# Verbose...
RSYNC_OPTS="$RSYNC_OPTS -v --stats --progress"

# ...or quiet
#RSYNC_OPTS="$RSYNC_OPTS -q"

# Limit bandwidth
RSYNC_OPTS="$RSYNC_OPTS --bwlimit=30"

REMOTE="rsync://mirrors.kernel.org/fedora"
#REMOTE="rsync://fr.rpmfind.net/linux/fedora"
#REMOTE="rsync://na.mirror.garr.it/fedora/linux"

#LOCAL="/pub/linux/distro/fedora"
LOCAL="/var/www/fedora"

FILTERS='
	--exclude debug
	--exclude drpms
	--exclude repodata/repoview
	--exclude kernel-PAEdebug-*
	--exclude kde-l10n-*
	--exclude openoffice.org-langpack-*-*.rpm
'

LOCKFILE="/tmp/`basename $0`.lock"

# Prevent re-entrancy
if [ -s $LOCKFILE ]; then
        if kill -0 `cat $LOCKFILE`; then
                echo >&2 "Uh-oh, another instance appears to be running! Bailing out..."
                exit 1
        fi
        echo >&2 "Overwriting stale lockfile $LOCKFILE."
fi
echo $$ >$LOCKFILE
trap "/bin/rm -f $LOCKFILE; exit 0" EXIT SIGINT SIGTERM SIGQUIT


# sync_path <DIR>
sync_path() {
	mkdir -p "$LOCAL/$1"
	rsync $RSYNC_OPTS $FILTERS "$REMOTE/$1" "$LOCAL/$1"
}

for rel in $RELEASES; do
	for arch in $ARCHS; do
		if [ "$rel" != "development" ]; then
			sync_path "updates/$rel/$arch/"
			if [ "$MIRROR_UPDATES_TESTING" -ge 1 ]; then
				sync_path "updates/testing/$rel/$arch/"
			fi
		fi # $rel != "development"

		sync_path "releases/$rel/Everything/$arch/"
	done

	# Kernel sources
	mkdir -p "$LOCAL/updates/$rel/SRPMS/"
	rsync $RSYNC_OPTS \
			"$REMOTE/updates/$rel/SRPMS/kernel-*src.rpm" "$LOCAL/updates/$rel/SRPMS/"
done
