#!/bin/bash
set -e

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
	echo "bad args" >&2
	exit 1
fi 

DEST=$(realpath "$2")
cd "$1"

#shellcheck disable=SC2016
find . \
	-type f \
	-not -path '*/.*' \
	-path "$3" | \
		xargs -0 -d \\n -n 1 sh -c "mkdir -p $DEST/\$(dirname \"\$1\")" sh

#shellcheck disable=SC2016
find . \
	-type f \
	-not -path '*/.*' \
	-path "$3" \
	-printf "$(pwd)/%P\n$DEST/%P\n" | \
		xargs -0 -d \\n -n 2 sh -c "$4" sh


#for rel in $(find . -type f -name *.gpg); do
#	DESTFILE=$(echo $DEST/$rel | sed 's/\.[^.]*$//')
 #      	[ -f "$DESTFILE" ] && continue	
#	mkdir -p "$(dirname "$DESTFILE")"
#	gpg --output "$DESTFILE" --decrypt "$rel"
#done