#!/bin/bash

##
## This script is used to sync data from main download servers to
## secondary server at ibiblio.
##

RSYNC='/usr/bin/rsync'
RS_OPT="-avSHP  --numeric-ids "
RS_DEADLY="--delete --delete-excluded --delete-delay --delay-updates"
ALT_EXCLUDES=""
EPL_EXCLUDES=""
FED_EXCLUDES=""

DATE_EPEL='/root/last-epel-sync'
DATE_FED='/root/last-fed-sync'
DATE_ARCHIVE='/root/last-archive-sync'
DATE_ALT='/root/last-alt-sync'
DATE_SECOND='/root/last-second-sync'

for i in ${DATE_EPEL} ${DATE_FED} ${DATE_ARCHIVE} ${DATE_ALT} ${DATE_SECOND}; do
  touch ${i}
done

LAST_SYNC='/usr/local/bin/last-sync'

SERVER=dl.fedoraproject.org

function sync_stuff() {
    if [[ $# -ne 5 ]]; then 
	echo "Illegal number of arguments to sync_stuff: " $#
	exit 1
    fi
    DATE_FILE=$1
    LOGGER_NAME=$2
    RSYNC_FROM=$3
    RSYNC_TO=$4
    FLAG="$5"

    CURDATE=$( date +%s )
    if [[ -s ${DATE_FILE} ]]; then
	LASTRUN=$( cat ${DATE_FILE} | awk '{print int($NF)}' )
    else
	LASTRUN=$( date +%s --date="Jan 1 00:00:00 UTC 2007" )
    fi
    DELTA=`echo ${CURDATE}-${LASTRUN} | bc`

    ${LAST_SYNC} -d ${DELTA} -q ${FLAG}
    
    if [ "$?" -eq "0" ]; then
	${RSYNC} ${RS_OPT} ${RS_DEADLY} ${ALT_EXCLUDES} ${SERVER}::${RSYNC_FROM}  ${RSYNC_TO}  | tail -n2 | logger -p local0.notice -t ${LOGGER_NAME}
	echo ${CURDATE} > ${DATE_FILE}
    else
	logger -p local0.notice -t ${LOGGER_NAME} "No change found. Not syncing"
    fi
}


sync_stuff ${DATE_EPEL} rsync_epel fedora-epel0 /srv/pub/epel/ "-e"
sync_stuff ${DATE_FED} rsync_fedora fedora-enchilada0 /srv/pub/fedora/ "-f"
sync_stuff ${DATE_ARCHIVE} rsync_archive fedora-archive0 /srv/pub/archive/ "-f"
sync_stuff ${DATE_ALT} rsync_alt fedora-alt0 /srv/pub/alt/ "-f"
sync_stuff ${DATE_SECOND} rsync_second fedora-secondary0 /srv/pub/fedora-secondary/ "-f"

# Let MM know I'm all up to date
#/usr/bin/report_mirror
