#!/usr/bin/env bash

. lib.bash || exit

declare src=$1 dest=$2 rest=("${@:3}")
declare args=()

# note: add -x to jobs instead of here
args+=(nocache
	rsync "$src" "$dest"
	-aHAXvzh
	--info=progress2
	--delete-after
	--delete-excluded)

for arg in "${rest[@]}"; do
	if [[ $last == -f && $arg == @(merge|.)\ * ]]; then
		debug "processing '$arg'"
		if [[ -f ${arg#* } ]]; then
			args+=("$arg")
		else
			debug "merge file not found, replacing with /dev/null"
			args+=("merge /dev/null")
		fi
	else
		args+=("$arg")
	fi
	last=$arg
done

echo "rsyncing $src -> $dest" >&2

"${args[@]}"; r=$?

(( !r )) ||	# success
(( r == 24 ))	# files vanished
