#!/bin/sh

if [ "$1" == "" ]; then
    echo "Usage: ${0##*/} PATH_TO_mirror.conf_ROOT"
    exit 0
fi

cfgroot=$1
conf=${cfgroot}mkmirror.conf
exc=${cfgroot}excludes
inc=${cfgroot}includes
logfile=${cfgroot}mkmirror.log

opt_incexc=
target=

if [ ! -d "$cfgroot" ]; then
    echo "ERROR: '$cfgroot' is not a directory"
    exit 1
fi

if [ ! -f "$conf" ]; then
    echo "ERROR: can't find 'mirror.conf' file under '$cfgroot'"
    exit 1
fi

if [ -f "$exc" ] && [ -f "$inc" ]; then
    opt_incexc="--exclude-from=$exc --files-from=$inc"
fi

# source conf file
. "$conf"

if [ "$target" == "" ] || [ ! -d "$target" ]; then
    echo "ERROR: target directory not specified or do not exist"
    exit 1
fi

curdate=`date "+%Y-%m-%d %H:%M:%S"`
echo "############## $curdate Starting mkmirror... ##############" | tee -a $logfile
echo "####    processing: $cfgroot"
echo "####    target:     $target"
echo

for src in "${sources[@]}"
do
    if [ -n "$src" ]; then
	echo "source:     $src"
	#list=$(rsync --list-only --exclude-from="$exc" --include-from="$inc" "$source")
	#list=$(rsync --dry-run --stats -W -v -a --delete-after --include-from="$inc" --exclude-from="$exc" --log-file="$logfile" "$source" "$tgtdir")
#	list=$(rsync --dry-run --stats -W -v -a -r --delete-after $opt_incexc --log-file="$logfile" "$src" "$target")
#	echo "$list" >$cfgroot/mkmirror.lst
	#rsync --list-only --dry-run --stats -W -v -a -r --delete-after $opt_incexc --log-file="$logfile" "$src" "$target"
#	rsync --dry-run -v $opt_incexc --log-file=$logfile $src $target
	#rsync --list-only -r $opt_incexc --log-file=$logfile $src $target
#	rsync --list-only -r $opt_incexc --log-file=$logfile $src $target

	# this is used to check if any mirror hide files when updating itself
	num=$(rsync --list-only -r $opt_incexc $src $target | wc -l)
	if [ $? -eq 0 ] && [ $num -gt 10 ]; then
	    #rsync --dry-run -v --stats --delete-after -a -H $opt_incexc --log-file=$logfile $src $target
	    rsync -v --stats --delete-after -a -H -r $opt_incexc --log-file=$logfile $src $target
	    break
	fi
    fi
done

## good cmd
# rsync -v --stats --delete-after -a -H $opt_incexc --log-file=$logfile $src $target
##

exit

exit

exit


# get filelist from first source
src=$source1
retval=0
list=$(rsync --list-only --exclude=debug/ --exclude=repodata/ $src | wc -l)

# if it hides files (ex: num < 500), use source2
if [ $list -gt 500 ]; then
    echo "Using yandex mirror..."
else
    echo "Using chg.ru mirror, cos' yandex hide files again :("
    src=$source2
fi


# do the rsync
rsync	--stats --size-only -W -v -a --delete --max-delete=200 \
	--exclude=debug/ \
	--exclude=repodata/ --include=repodata/comps-f7.xml --include=repodata/updateinfo.xml.gz\ \
	--log-file=$logfile \
	$src $target
retval=$?

#sample perl search
#$ perl -ne 'print "$1\n" if ((/Error: Missing Dependency:.*is needed by package (.*)$/) || (/Error: Package (.*?) needs.*, this is not available./))' /tmp/yum_upgrade | sort | uniq
#
# it seems to be 13 lines to the end contains rsync's '--stats' option + 2 lines of totals
transferred_files=$(cat $logfile | tail -n 13 | perl -ne 'print "$1" if (/Number of files transferred: (.*?)$/)')

# TODO: 20070929 winex: check checksums in repodata/, if fails - recreate repodata/
if [ $retval -eq 0 ]; then
    if [ $transferred_files -gt 0 ]; then
	echo "refreshing repo metadata..."
	createrepo -q -d --update -g repodata/comps-f7.xml -o "$target" "$target"
    fi
else
    echo "ERROR: Something bad has happened, rsync returned:" $retval >&2
fi
