#!/bin/sh

SCRIPT="$(dirname "$(readlink -f $0)")"
DIRNAME="."

cat > /tmp/arch.txt << EOF
mips,24kc,ar71xx,generic
mips,24kc,ar71xx,tiny
mips,24kc,ath79,generic
mipsel,24kc,lantiq,xrx200
mipsel,24kc,ramips,rt305x
mipsel,24kc,ramips,mt7621
mipsel,mips32,brcm47xx,legacy
mips,mips32,brcm63xx,generic
x86,64,x86,64
EOF

for VERSION in 22.03.5 21.02.5 21.02.3 19.07.10 19.07.8 19.07.7 19.07.6 19.07.5 19.07.4 19.07.3 19.07.2 19.07.1 18.06.7 18.06.4 18.06.2 17.01.7; do
	MAJOR="$(echo "${VERSION}" |cut -f1-2 -d.)"
	echo "+ /${VERSION}/"
	echo "+ /${VERSION}/packages"
	echo "+ /${VERSION}/targets/"
	echo "+ /packages-${MAJOR}/"
	for LINE in $(cat /tmp/arch.txt); do
		P1="$(echo "${LINE}" |cut -f1 -d,)"
		P2="$(echo "${LINE}" |cut -f2 -d,)"
		P3="$(echo "${LINE}" |cut -f3 -d,)"
		P4="$(echo "${LINE}" |cut -f4 -d,)"
		echo "+ /${VERSION}/targets/${P3}/"
		echo "+ /${VERSION}/targets/${P3}/${P4}/"
		echo "+ /${VERSION}/targets/${P3}/${P4}/*.tar.xz"
		echo "+ /${VERSION}/targets/${P3}/${P4}/kmods/"
		echo "+ /${VERSION}/targets/${P3}/${P4}/kmods/**"
		echo "+ /${VERSION}/targets/${P3}/${P4}/packages/"
		echo "+ /${VERSION}/targets/${P3}/${P4}/packages/**"
		echo "+ /${VERSION}/targets/${P3}/${P4}/*sums"
		echo "+ /${VERSION}/targets/${P3}/${P4}/*sums.asc"
		echo "+ /packages-${MAJOR}/${P1}_${P2}/"
		echo "+ /packages-${MAJOR}/${P1}_${P2}/**"
	done
done > /tmp/filter.txt
echo "- *" >> /tmp/filter.txt

#RSYNC_MIRROR="rsync://mirror.0x.sg/lede/"
#rsync -avr --partial --progress --inplace --compress "${RSYNC_MIRROR}/releases/" --filter="merge /tmp/filter.txt" "${DIRNAME}/releases/" --ignore-existing --no-owner --no-group

RSYNC_MIRROR="rsync://downloads.openwrt.org/downloads"
rsync -avr --partial --progress --inplace --compress "${RSYNC_MIRROR}/releases/" --filter="merge /tmp/filter.txt" "${DIRNAME}/releases/" --delete --no-owner --no-group
