#!/bin/bash
# Try to disambiguate rsync "absolute" path syntax
# (C) 2023 Felix Hauri - testRsync@f-hauri.ch
# Licensed under terms of GPL v3. www.gnu.org
# This script will create 250 differents arbitray files, in
# 25 dirs and subdir who contain a ".cache" directory
# Rsync excluding some subdirs and ALL .cache.
#
# This is a test script. It open an Interactive bash before
# deleting everything.

tempDir=$(mktemp -d)
cd $tempDir || exit 1

filesDirsCacheStats() {
    local -i stats=([0]=0 [1]=0 [2]=0) knd
    local fLne
    while read knd; do
	  stats[knd]+=1
    done < <(
	find "$@" -type f -printf \\n -o -type d -printf 1\\n -name .cache -printf 2\\n
    )
    printf 'Found %3d files, %3d dirs and %3d dotCache dirs, in "%s".\n'\
	   "${stats[@]}" "$*"
}

mkdir -p path/source/{dir{a..d}/,}{subdir{1..4}/,}.cache dest
for file in path/source/{dir{a..d}/,}{subdir{1..4}/,}{,.cache/}file{V..Z}; do
    dd count=3 2>/dev/null |
	xargs echo 2>/dev/null |
	fold -sw 68 >$file
done < <(man -Len -Pcol\ -b bash )

exec {BASH_XTRACEFD}> >(sed -ue \
    '/set +x/d;s/^+ \(.*\)/\o33[7m\1\o33[0m/' >/dev/tty);set -x
#  source/dir{a,c,d} will be expanded by bash *before* running rsync
rsync -a --exclude=/dirc/subdir[13] --exclude=.cache \
      path/source/dir{a,c,d} dest
ls -Altr dest/dir{a,c}/subdir1/
ls -Altr dest/dirc/

set +x;exec {BASH_XTRACEFD}>&-;kill $!

filesDirsCacheStats path/source
filesDirsCacheStats dest
export -f filesDirsCacheStats

bash --rcfile <(echo "PS1='$USER@ExploreRsync [${tempDir##*/}]$ '") -i

cd || exit 1
rm -fR "$tempDir"