#!/bin/sh
#< Duplicate a directory tree using cpio pass through
# KW 08/10/04

# usage: cpcpio /source/dir/root /output/dir/root
# e.g. to copy /home/kevin/bin to /home/kevin/tmp/bin
#      use "cpcpio /home/kevin/bin /home/kevin/tmp/bin"
# * creates subdirectories as required
# * full path must be given for output dir

prog=$(basename $0)

if [ "$#" -ne "2" ]; then
   echo "Usage: ${prog} /source/tree /output/dir" >&2
   exit 1
fi

if [ ! -d "$1" ]; then
   echo "Error: ${prog}: ${1} - Not a valid source directory" >&2
   exit 1
fi

echo "$2" | grep "^/" > /dev/null 2>&1

if [ "$?" -ne "0" ]; then
   echo "Error: ${prog}: ${2} - Doesn't look absolute to me!" >&2
   exit 1
fi

if [ ! -d "$2" ]; then
   echo "Warning: ${prog}: ${2} - Directory does not exist"
   echo "Would you like to create it? [y|n]"
   read response
   case $response in
      y|Y)  mkdir -p ${2}
            if [ ! -d "${2}" ]; then
               echo "Error: ${prog}: Couldn't create directory ${2}" >&2
			   exit 1
            fi
			;;
      *)    echo "Error: ${prog}: Directory creation cancelled" >&2
            exit 1
            ;;
   esac
fi

tput clear

cat << EnDoFhErE
================================================
${prog} CONFIRMATION
================================================
You are copying directory tree
  ${1}
To location
  ${2}
If this information is correct, and you are
certain that the target is specified fully
and absolutely, press return to continue. 
Otherwise, ^C to abort!
================================================
EnDoFhErE

read dummy

cd ${1}

find . -depth -print | cpio -pvd $2

echo "Pass through copy complete!"

exit 0