already does this, so you might want to save yourself a whole bunch of time and just use it.
If you're using this as a learning experience, or you're stuck in an environment without rsync, there's one other thing to watch out for: What do you do when the source is a file and the target has a directory with the exact same name? Or if the source has a directory and the target has a file? The answers depend on what you really need the functionality for.
Spring: Forces, Coiled Again!