#!/bin/bash

pidfile=/var/run/s3-mirror/pid
logfile="/var/log/s3-mirror/s3sync.log"

trap sighup_handler HUP
trap "rm -f ${pidfile}" QUIT EXIT INT TERM

function sighup_handler()
{
    exec 1>>${logfile} 2>&1
}


function newer()
{
    if [ "$1" -nt "$2" ] ; then
	return 0
    fi
    return 1
}

function repeat()
{
    while :; do
	$1
        /bin/sleep $((${RANDOM} % 300))
    done
}

s3cmd=/usr/bin/s3cmd

S3CMD_ARGS="sync \
  --verbose \
  --preserve \
  --recursive \
  --check-md5 \
  --delay-updates \
  --guess-mime-type --mime-type application/octet-stream \
  --reduced-redundancy \
  --acl-public \
  --exclude-from /usr/local/etc/s3-mirror-excludes.txt \
  "

content="epel"
targets="s3-mirror-us-east-1 s3-mirror-us-west-1 s3-mirror-us-west-2 s3-mirror-eu-west-1 s3-mirror-ap-northeast-1"

function parallel_sync_full()
{
    report=0
    for c in ${content}; do
	if $(newer /pub/${c}/fullfilelist /var/lib/s3-mirror/${c}-fullfilelist) ; then
	    echo "=============================================================="
	    echo -n "Starting at "
	    date
            time $s3cmd $S3CMD_ARGS  \
		       --cache-file /var/lib/s3-mirror/${c}.cache \
                       --delete-removed \
                       --delete-after \
                       --add-destination s3://s3-mirror-us-west-1.fedoraproject.org/pub/${c}/ \
                       --add-destination s3://s3-mirror-us-west-2.fedoraproject.org/pub/${c}/ \
                       --add-destination s3://s3-mirror-eu-west-1.fedoraproject.org/pub/${c}/ \
                       --add-destination s3://s3-mirror-ap-northeast-1.fedoraproject.org/pub/${c}/ \
                              /pub/${c}/ s3://s3-mirror-us-east-1.fedoraproject.org/pub/${c}/
	    report=1
	    cp -a /pub/${c}/fullfilelist /var/lib/s3-mirror/${c}-fullfilelist
	fi
    done

    if [ ${report} -ne 0 ] ; then
	for target in ${targets} ; do
           report_mirror -c /etc/mirrormanager-client/report_mirror_${target}.conf --exclude-from /usr/local/etc/s3-mirror-excludes.txt
	done
        echo -n "Ending at "
        date
    fi
}


# send stdout/stderr to logfile
sighup_handler
mkdir -p $(dirname ${pidfile})
echo "$$" > ${pidfile}
repeat parallel_sync_full
