#!/bin/sh
#	Cp [opt] file... target
#	Lc [opt] file... target
#	Ln [opt] file... target
#	Mv [opt] file... target
# This is like the Unix command with the lower-case names, but it does
# a  recursive  rename  (via  Rm)  on the targets first.  Note that it
# doesn't complain if the target already exists.  It also works if the
# target  is  a  directory,  including the case where the file already
# exists.  Note the "Lc" name, aka "LnCp", which attempts a link,  and
# if that fails, does a Cp instead.
#
# This should probably be rewritten in C.

Cmd=`basename $0`
case "$Cmd" in
	Ln)	cmd=ln;;
	Cp)	cmd=cp;;
	LnCp)	cmd=cn; cmd2=cp;;
	Lc)	cmd=cn; cmd2=cp;;
	lc)	cmd=ln; cmd2=cp;;
	Mv)	cmd=mv;;
	*)	echo 'Unknown command "'$Cmd'"'; exit 1;;
esac
opt=''
case $1 in
	-*) opt=$1;shift;;
esac
if [ $# -lt 2 ];then echo Usage: $0 old new; exit 1;fi
for t do : ; done
#t=`lastfld $*`
while	[ $# -gt 1 ]
do	if [ -d $t ]
	then	b=`basename $1`
		if [ -f $t/$b ];then Rm $t/$b;fi
		$cmd $opt $1 $t/$b 2>/dev/null || {
			if [ -n "$cmd2" ];then $cmd2 $opt $1 $t/$b;fi
		}
	else	if [ -f $t ];then Rm $t;fi
		$cmd $opt $1 $t 2>/dev/null || {
			if [ -n "$cmd2" ];then $cmd2 $opt $1 $t;fi
		}
	fi
	shift
done
exit 0
