#!/bin/bash

# Source: https://git.server-speed.net/users/flo/bin/plain/syncrepo.sh
#  as recommended in our request to become Tier 1:
#		https://bugs.archlinux.org/task/52853

if [ $# -lt 1 ]; then
  echo "Usage: $0 local_dir"
  exit 1
fi

umask 022
ADDRESS=$(cat ~/config/ADDRESS)

# This is a simple mirroring script. To save bandwidth it first checks a
# timestamp via HTTP and only runs rsync when the timestamp differs from the
# local copy. As of 2016, a single rsync run without changes transfers roughly
# 6MiB of data which adds up to roughly 250GiB of traffic per month when rsync
# is run every minute. Performing a simple check via HTTP first can thus save a
# lot of traffic.

home="/home/mirror"
target=/mirror/root/$1
tmp="${home}/tmp"
lock="${tmp}/mirrorsync-$1.lck"
# NOTE: You'll probably want to change this or remove the --bwlimit setting in
# the rsync call below
bwlimit=4096
# NOTE: most people reading this very likely need to change this since
# rsync.archlinux.org requires you to be a tier 1 mirror
source='rsync://rsync.archlinux.org/ftp_tier1'
lastupdate_url="http://rsync.archlinux.org/lastupdate"

[ ! -d "${target}" ] && mkdir -p "${target}"
[ ! -d "${tmp}" ] && mkdir -p "${tmp}"

exec 9>"${lock}"
flock -n 9 || exit

# if we are called without a tty (cronjob) only run when there are changes
if ! tty -s && diff -b <(curl --interface $ADDRESS -s "$lastupdate_url") "$target/lastupdate" >/dev/null; then
   date +'%s' > "$target/lastsync"
	exit 0
fi

if ! stty &>/dev/null; then
    QUIET="-q"
fi

# this can be added into the rsync: --bwlimit=$bwlimit \
# --exclude='/iso' \
#	--exclude='*.links.tar.gz*' \
#	--exclude='/other' \
#	--exclude='/sources' \

rsync -rtlvH --safe-links --delete-after --progress -h ${QUIET} --timeout=600 --contimeout=60 -p \
	--delay-updates --no-motd \
	--temp-dir="${tmp}" \
	--stats \
	--log-file=$home/merlin/logs/$1.log \
	--address=$ADDRESS \
	${source} \
	"${target}"

date +'%s' > "$target/lastsync"
#echo "Last sync was $(date -d @$(cat ${target}/lastsync))"
