#! /bin/sh

# cpr
#
# Implements recursive copy for machines which do not have the -R option on cp
#
# Andrew.Findlay@brunel.ac.uk
#
# Derived from:
#
# lndir - create shadow link tree
#
# Time stamp <89/11/28 18:56:54 gildea>
# By Stephen Gildea <gildea@bbn.com> based on
#  XConsortium: lndir.sh,v 1.1 88/10/20 17:37:16 jim Exp


USAGE="Usage: $0 fromdir [todir]"

if [ $# -lt 1 -o $# -gt 2 ]
then
	echo "$USAGE"
	exit 1
fi

PATH=`pwd`:$PATH

DIRFROM=$1

if [ $# -eq 2 ];
then
	DIRTO=$2
else
	DIRTO=.
fi

if [ ! -d $DIRTO ]
then
	echo `basename $0`": $DIRTO is not a directory"
	echo "$USAGE"
	exit 2
fi

case "$DIRFROM" in
	/*) ;;
	*)  DIRFROM=`pwd`/$DIRFROM ;;
esac

cd $DIRTO

if [ ! -d $DIRFROM ]
then
	echo `basename $0`": $DIRFROM is not a directory"
	echo "$USAGE"
	exit 2
fi

pwd=`pwd`

if [ `(cd $DIRFROM; pwd)` = $pwd ]
then
	echo "$pwd: FROM and TO are identical!"
	exit 1
fi

for file in `ls -a $DIRFROM`
do
	if [ ! -d $DIRFROM/$file ]
	then
		#ln -s $DIRFROM/$file .
		cp $DIRFROM/$file .
	else
		if [ \( $file != RCS \) \
		     -a \( $file != . \) \
		     -a \( $file != .. \) ]
		then
			#echo $file:
			if [ ! -d $file ]
			then
				mkdir $file
			fi
			(cd $file
			 pwd=`pwd`
			 case "$DIRFROM" in
				 /*) ;;
				 *)  DIRFROM=../$DIRFROM ;;
			 esac
			 if [ `(cd $DIRFROM/$file; pwd)` = $pwd ]
			 then
				echo "$pwd: FROM and TO are identical!"
				exit 1
			 fi
			 `basename $0` $DIRFROM/$file
			)
		fi
	fi
done
