#!/bin/sh

# MIT No Attribution
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## make sure we never run 2 rsync at the same time
#lockfile="/tmp/alpine-mirror.lock"
#if [ -z "$flock" ] ; then
#  exec env flock=1 flock -n $lockfile "$0" "$@"
#fi

src=rsync://rsync.alpinelinux.org/alpine/ 
dest=/srv/alpine/mirror

exclude="
--exclude v2.*
--exclude v3.0
--exclude v3.1
--exclude v3.2
--exclude v3.3
--exclude v3.4
--exclude v3.5
--exclude v3.6
--exclude v3.7
--exclude v3.8
--exclude v3.9
--exclude v3.10
--exclude v3.11
--exclude v3.12
--exclude v3.13
--exclude v3.14
--exclude v3.15
--exclude v3.16
--exclude v3.17
--exclude v3.18
--exclude v3.19
--exclude v3.20
--exclude v3.21
--exclude v3.22
--exclude v3.23
--exclude latest-stable
--exclude */*/aarch64
--exclude */*/arm*
--exclude */*/loongarch64
--exclude */*/ppc64le
--exclude */*/riscv64
--exclude */*/s390x
--exclude */*/x86
--exclude */testing
--exclude */releases
--exclude MIRRORS.txt
"

mkdir -p "$dest"
/usr/bin/rsync \
        --archive \
        --update \
        --hard-links \
	--delete \
	--delete-after \
        --delay-updates \
        --timeout=600 \
        $exclude \
        "$src" "$dest"
