#!/usr/bin/env bash
# $Id$
# Author: Meethune Bhowmick <meethune@gmail.com>
# Copy a directory using cpio. It is much faster than tar for these operations.
# Inspired by Example 15-27 of The Advanced Bash Scripting Guide v4.0

ARGS=2

# Basic Usage
if [ ${#} -ne ${ARGS} ]
then
    echo "Usage: $(basename ${0}) source destination"
    exit 1
fi

# Verify Parameters
for each in $(seq 1 ${ARGS})
do
    eval each=\$${each}
    if [ ! -e "${each}" ]
    then
        echo "${each} doesn't exist"
        exit 1
    elif [ ! -d "${each}" ]
    then
        echo "${each} isn't a directory" 
        exit 1
    fi
done

# This is the magic
if find "${1}" -depth | cpio -admp "${2}" >/dev/null 2>&1
then
    echo "Copied ${1} to ${2}"
    exit 0
else
    echo "Copy Failed!"
    exit 1
fi

# vim:syntax=sh
# vim:sw=4:softtabstop=4:expandtab
