#!/bin/sh

if [ $# != 2 ]; then
  echo usage: $0 source target
  echo creates in target any files or directories that exist in source
  exit 1
fi

CDPATH=:.
: ${LN=ln}
sed_quote_subst='s/\([\\"\\`$\\\\]\)/\\\1/g'

# Make sure source exists
test -d $1 || exit 1 &&
# Create target
test -d $2 || mkdir $2 &&
# Create missing directories
(cd $1 && find . -type d -print) |
  sed "$sed_quote_subst;s,.*,test -e \"$2/&\" || mkdir \"$2/&\"," |
  bash &&
# Create missing files
(cd $1 && find . ! -type d ! -type l -print) |
  sed "$sed_quote_subst;s,.*,test -e \"$2/&\" || $LN \"$1/&\" \"$2/&\"," |
  bash &&
# Create missing links
(cd $1 && find . -type l -print) |
  sed "$sed_quote_subst;s,.*,test -e \"$2/&\" || echo \"&\"," |
  bash |
  (cd $1 && tar -cf - -T-) |
  (cd $2 && tar -xkf -) &&
exit 0
exit 1
