#!/bin/sh

usage()
{
  cat 1>&2 <<EOF
usage: dpkruby-dcp [OPTIONS] SRCDIR DESTDIR
OPTIONS:
  -v: Verbose mode.
  -n: dry-run mode. (Do not anything.)
  -X, --cvs-exclude:
    Excludes such files:
      RCS, CVS, *~, *.bak, *.BAK, core, *.core, #*, .#*, *.orig,
      *.rej, *.old
EOF
  exit 1
}

eval `dpkruby-getopts nvX cvs-exclude :: "$@"`
$OPT_USAGE && usage
shift $OPT_SHIFT

SRCDIR="$1"
DESTDIR="$2"
[ -n "$DESTDIR" ] || usage

if ! $OPT_n; then
  if ! [ -d "$DESTDIR" ]; then
    mkdir "$DESTDIR" || exit $?
  fi
fi

SRC_TAROPTS="-c"
if $OPT_n; then
  DEST_TAROPTS="-t"
else
  DEST_TAROPTS="-x"
fi

$OPT_v && DEST_TAROPTS="-v $DEST_TAROPTS"

runsync()
{
  tar -C "$SRCDIR" $SRC_TAROPTS .|tar -C "$DESTDIR" $DEST_TAROPTS
}

if $OPT_X || $OPT_cvs_exclude; then
  SRC_TAROPTS="-X - $SRC_TAROPTS"
  dpkruby-array -r dpklib/file Dpklib::CvsIgnore::PATTERNS | runsync
else
  runsync
fi
