#!/bin/bash

# copy all files below <from> ($1), that match <filepattern> ($3)
# into target <to> ($2)

function fcp
{
	rel_path=${3#$1}
	
	# create target dir, if necessary
	rel_dir=${rel_path%/*}
	mkdir -p $2/$rel_dir
	cp -a $1/$rel_path $2/$rel_dir
}
	
usage="usage: treecopy <from> <to> <filepattern>|-e"

if [ $# -ne 3 ]
then
	echo $usage
	exit 1
fi
if [ $3 == "-e" ]
then
	for file in $(find $1 \( -type f -o -type l \) -executable)
	do
		fcp $1 $2 $file
	done
else	
	for file in $(find $1 \( -type f -o -type l \) -name "$3")
	do
		fcp $1 $2 $file
	done
fi
