#!/bin/sh
#
#  Copy a file with pipemeter
# 
#  Copyright Clint Byrum 2003, All Rights Reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
#  ---
#  $Id$
#
echo "Note: pipecp is *alpha* quality software. You have been warned."

SRC=$1
DST=$2
SRCBASE=`basename $SRC`

if [ -d $SRC ] ; then
  # TODO: don't use tar for this
  echo -n "Finding size of source dir.."
  # TODO: maybe pipedu ?
  # XXX: requires gnu du
  SRCSIZE=`du -sk ${SRC} | awk {'print $1'}`
  echo "${SRCSIZE}k"
  if [ -d $DST ] ; then
    if [ ! -e $DST/$SRCBASE ] ; then
      mkdir $DST/$SRCBASE
      DST=$DST/$SRCBASE
    fi 
    tar cf - -C $SRC . | pipemeter -s ${SRCSIZE}k | tar xf - -C $DST
  elif [ -f $DST ] ; then
    # NO! ;)
    echo "$0: cannot overwrite non-directory with directory"
    exit 1
  elif [ ! -e $DST ] ; then
    # doesn't exist, lets create it
    mkdir $DST 
    cd $DST && tar cf - -C $SRC . | pipemeter -s ${SRCSIZE}k | tar xf - -C $DST
  fi
elif [ -f $SRC ] ; then
  if [ -d $DST ] ; then
    OUTFILE=$DST/$SRCBASE
  else
    OUTFILE=$DST
  fi
  echo pipemeter -f $SRC \> $OUTFILE
  pipemeter -f $SRC > $OUTFILE
fi
[ $? ] && exit 1
echo "SHOULD be removing $SRC ... but I'm chicken"
