#!/bin/sh
# $Header: /cvsroot/sadm/cfm-1/bin/cpdtre,v 1.1 2000/03/31 23:00:01 dmetz Exp $
# $crtd:  by  Derald Metzger  on  08feb93 $
# $cmnt:  "cpdtre" cmd. Cross platform cmd.
#   Copy contents of a dir and all its subdirs into a target 
#   dir and its subdirs. Create dirs as rqd.
#   Preserves ownership, symbolic links, etc.
# $

cmd=`basename $0`
myname=`uname -n`
osv=`uname`-`uname -r | cut -c1`

#   Take care of things that depend on my host type.
case $osv in
  Linux-2)
    echo=/bin/echo;
    rmtsh=ssh
  ;;
  *)
    echo "### $cmd: unrecognized OS!  Exiting."
    exit 1
  ;;
esac

usage="
DESCR:
  Copy contents of a source dir and all its subdirs into a destination
  dir and its subdirs. Creates dirs as rqd. Preserves ownership, 
  symbolic links, etc. This cmd is implemented using tar.
USAGE:
  cpdtre [-p proto]  [src_host:]src_dir  [dst_host:]dst_dir
    -p proto = protocol to use. usually rsh or ssh
    src_host = source host
    src_dir  = path of dir whose contents are to be copied
    dst_host = destination host
    dst_dir  = path of dir to rcv the copied files. (created if rqd)
PBLMS/NOTES:
  The default remote shell protocol is ssh. rsh should work as well.
  Non-interactive login is assumed. Interactive works with some configs.
  Name resolution uses \`ping'. Assumed local hostname is fqdn up to [-.].
  Access is via the transport protocol (rsh, ssh, ?).
  WARNING: do NOT copy into the tree you are copying from.
  Relative paths make sense on the local host.
  Relative names on a remote host are relative to the accounts home dir.
  If dst_dir is created it will belong to the creator.
AUTHOR
               Copyright (c) 1998, 1999 2000  Derald Metzger 
  This file is part of the cfm pkg (GPL).
  $Id: cpdtre,v 1.1 2000/03/31 23:00:01 dmetz Exp $
"

while getopts p: c
do
  case $c in
    p) rmtsh=$OPTARG ;;
    \?) $echo "$usage"; exit 2; ;;
  esac
done
shift `expr $OPTIND - 1`
 
if [ $# -lt 2 ]; then $echo "### $cmd: Insufficient args $usage"; exit; fi


#   Get what's in front of the colon
src_if=`expr "$1" : '\(.*\):.*'`
if [ "$src_if" = "" ]; then src_if=`uname -n`; fi
src_host=`ping -c1 $src_if|grep PING`
[ $? -ne 0 ] && echo "### Can't see src_host! Exiting" && exit
src_host=`expr "$src_host" : 'PING \([^.-]*\).*'`

dst_if=`expr "$2" : '\(.*\):.*'`
if [ "$dst_if" = "" ]; then dst_if=`uname -n`; fi
dst_host=`ping -c1 $dst_if|grep PING`
[ $? -ne 0 ] && echo "### Can't see dst_host! Exiting" && exit
dst_host=`expr "$dst_host" : 'PING \([^.-]*\).*'`

#   Get what's after the colon, or all if there is no colon
src_dir=`expr "$1" : '.*:\(.*\)' \| "$1"`
if [ $src_host = $myname ]; then \
  src_dir=`(cd $src_dir; pwd)`
  if [ $? -ne 0 ]; then echo "### Can't find src dir. Exiting!"; exit; fi
else
  if [ `$rmtsh $src_if "/bin/sh -c \"if [ ! -d $src_dir ]; then echo 1; else echo 0; fi \""` != 0 ]; then echo "### Can't find src dir. Exiting!"; exit; fi  
fi

#	Make the dst_dir if it doesn't exist.
dst_dir=`expr "$2" : '.*:\(.*\)' \| "$2"`
if [ $dst_host = $myname ]; then \
  if [ ! -d $dst_dir ]; then mkdir -p $dst_dir; fi
  dst_dir=`(cd $dst_dir; pwd)`
else
  $rmtsh $dst_if "/bin/sh -c \"if [ ! -d $dst_dir ]; then mkdir -p $dst_dir; fi \""
fi

if [ $src_host = $myname -a  $dst_host = $myname ]; then \
  cd $src_dir; tar cf - . | (cd $dst_dir; tar xvpf -)
  exit
else
  $rmtsh $src_if "cd $src_dir; tar cf - ." | \
    $rmtsh $dst_if "(cd $dst_dir; tar xvpf -)"
fi
