#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   copy a directory using tar and scp
# author
#   larg 30 sep 02
# description
#   Transfer directory and its contents to remote host.
#   Preserve permissions and ownership.
#
#   If you run this as root you won't have permissions problems.
#
#   If you are not using key pairs with no passphrases to log in, this
#   is a pain in the arse to run. You will type in your password a dozen times.
#
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------

# command line options
   usage_message="
   usage:
      $0 <dir> <remote host>
   example:
      $0 /export/home/larg/scripts ics01
   description
      Transfer directory and its contents to remote host.
      Preserve permissions and ownership.
   "
   # wrong options provided? tell the punter how to use this utility
   if [ $# -ne 2 ]; then
      echo "This script takes 2 options."
      echo "$usage_message" 1>&2
      exit 1
   fi
#---------#---------#---------#---------#---------#---------#---------#---------
# function:
#    transfer_dir
# description:
#
#
transfer_dir() {

    tar_dir=/tmp
    crapola=`basename $dir`
    tarball=$crapola.tar
    if [ ! -d $dir ]
    then
        echo "FAIL: local directory $dir is missing."
    else
		echo "archiving $dir  ..."
		tar -cf $tar_dir/$tarball $dir
		echo "copying to $host  ..."
		scp $tar_dir/$tarball $host:$tar_dir
		echo "extracting $host:$tar_dir/$tarball ..."
		ssh $host tar -xpf $tar_dir/$tarball
		echo "removing tarfiles ..."
		rm $tar_dir/$tarball
		ssh $host rm $tar_dir/$tarball
    fi
}
#---------#---------#---------#---------#---------#---------#---------#---------
#
# setup
    # vars
    dir=$1
    host=$2
#
# main
# copy
    transfer_dir
#
#---------#---------#---------#---------#---------#---------#---------#---------
