#!/bin/sh

if [ -r /etc/sisyphus-mirror/sisyphus-mirror.conf ]; then
		. /etc/sisyphus-mirror/sisyphus-mirror.conf
fi

if [ -d "$RSHOME" ]; then
	continue
else
	echo ""
	echo "Directory \"$RSHOME\" does not exists, creating..."
	echo ""
	echo "You may want edit now personal config file \"$RSHOME\"/config"
	echo "(if don't, please re-run this script)"
	echo ""
	mkdir -p "$RSHOME"; exit 1
fi

if [ -r "$RSHOME"/config ]; then
	. "$RSHOME"/config
fi

RSYNC_RSH=rsh
RSYNCARGS="$ARGS --timeout=$TMOUT1 --exclude-from=$EXCLUDE_FILE \
		--bwlimit=$SPEED --partial-dir=$PARTIAL"

for mirror in "$LIST"; do
	OK=""
	LOG="$RSHOME"/rsync-$mirror.log
	LOCK="$RSHOME"/lock.$mirror
	FLAG="$DESTROOT"/$mirror/__SYNCING__
	date >> $LOG
	[ -f $LOCK ] && {
		ps auxww | grep rsync | mail -s "sync: $mirror: lockfile \
		exists" "$MAINTAINER"
		echo "Lockfile for $mirror exists, exiting" >> $LOG
		continue
	}
	trap "rm -f $LOCK; exit 1" INT
	touch $LOCK $FLAG
	for attempt in `seq 1 "$MAXATTEMPTS"`; do
		rsync $RSYNCARGS $* "$SRCROOT"/$mirror/ "$DESTROOT"/$mirror \
		>> $LOG 2>&1 && {
			OK="1"
			break
		}
		sleep "$TMOUT2"
	done
	[ -z "$OK" ] && {
		tail $LOG | mail -s "sync: $mirror trouble" "$MAINTAINER"
	} || {
		rm -f $FLAG
	}
	rm -f $LOCK
	date >> $LOG
done

nice symlinks -dr "$DESTROOT" >/dev/null
