#!/bin/sh

if test "$2" = ""; then 
  echo "usage $0: SM_SRC SM_DEST [PATCH_SRC]" ; exit 1
fi

CURRDIR=`pwd`
SM_SRC=$1
SM_DEST=$2
shift; shift

if ! test -d $SM_SRC; then
  echo "$0: SM_SRC $SM_SRC does not exist"
  exit 2
fi

if ! test -d $SM_DEST; then
  mkdir $SM_DEST
fi

echo "Copying $SM_SRC -> $SM_DEST"
(cd $SM_SRC ; tar cf - .) | (cd $SM_DEST ; tar xf -)

for PATCH_SRC in $*; do
  if test -f $PATCH_SRC; then
    echo "Applying patch $PATCH_SRC to $SM_DEST tree"
    (cd $SM_DEST ; patch -p1 ) < $PATCH_SRC
  else
    echo "$0: PATCH_SRC $PATCH_SRC does not exist"
  fi
done
