#!/bin/bash

#CENTOSMIRROR=rsync://mirror.cogentco.com/CentOS/
#CENTOSMIRROR=rsync://mirrors.kernel.org/centos/
#CENTOSMIRROR=rsync://lug.mtu.edu/centos/
#CENTOSMIRROR=rsync://mirrors.loosefoot.com/centos/
CENTOSMIRROR=rsync://centos-distro.1gservers.com/files/
EPELMIRROR=rsync://download-ib01.fedoraproject.org/fedora-epel/

#OPTIONS="--copy-dirlinks --progress --verbose -a -z -H"
#OPTIONS="--links --progress --verbose -a -z -H --delete-after --exclude='.~tmp~'"
OPTIONS="--links --verbose --progress -a -z -H --delete-after --exclude='.~tmp~' --delay-updates"
#OPTIONS=${OPTIONS}" --dry-run"

LOCKFILE=/tmp/mirrors.rsync

lockfile-create ${LOCKFILE}
[ $? != 0 ] && exit

lockfile-touch ${LOCKFILE} &
# Save the PID of the lockfile-touch process
LOCKPID="$!"


#CENTOS
rsync ${OPTIONS} --exclude="*.iso" --exclude="*/iso*/" --exclude='*.src.rpm' --exclude='*/SRPMS/' --exclude='*/debug/' --exclude='*/drpms/' --exclude='aarch64/' --exclude='ppc64le/' ${CENTOSMIRROR}/{6*,7*} /mirror/centos/

#EPEL
rsync ${OPTIONS} --exclude="*/SRPMS/" --exclude="*/ppc/" --exclude="*/ppc64/" --exclude='*/debug/' --exclude='*/drpms/' ${EPELMIRROR}/{6,7} /mirror/epel/

#Fedora
#rsync ${OPTIONS} --exclude=debug/ --exclude=drpms/ rsync://mirrors.kernel.org/fedora/updates/28/Everything/x86_64/ /mirror/fedora/updates/28/Everything/x86_64/
rsync ${OPTIONS} --exclude=debug/ --exclude=drpms/ rsync://mirrors.kernel.org/fedora/updates/29/Everything/x86_64/ /mirror/fedora/updates/29/Everything/x86_64/
rsync ${OPTIONS} --exclude=debug/ --exclude=drpms/ rsync://mirrors.kernel.org/fedora/updates/30/Everything/x86_64/ /mirror/fedora/updates/30/Everything/x86_64/

kill "${LOCKPID}"
lockfile-remove ${LOCKFILE}
