#! /bin/sh -e
#	@(#)backup_copy 96-05-27
#
#	ԡƱ̾Υե뤬СХååפƤ饳ԡ
#	(C) Copyright 1994-2000 by Personal Media Corporation
#
usage='usage: backup_copy [-p] [-t] [-c] [-d old_dir] src... dst_dir'

cp_opt=''
pfx=''
old_dir=''
src=''
dst=''
cmp_test=''

while test $# -gt 1
do
	case $1 in
	  -p)	cp_opt="$cp_opt $1" ;;
	  -t)	pfx='#' ;;
	  -c)	cmp_test='t' ;;
	  -d)	old_dir="/$2" ; shift ;;
	  -*)	echo "$usage" 1>&2 ; exit 1 ;;

	  *)	if test ! -f $1
		then
			echo "$usage" 1>&2
			exit 1
		fi
		src="$src $1"
		;;
	esac
	shift
done

case $# in
  1)	dst=$1 ;;
  *)	echo "$usage" 1>&2 ; exit 1 ;;
esac

case $src in
  '')	echo "$usage" 1>&2 ; exit 1 ;;
esac

if test ! -d $dst
then
	echo "$usage" 1>&2 ; exit 1
fi

if test ! -d $dst$old_dir
then
	mkdir $dst$old_dir
fi

# Хååץեդ륵եå
tm1=`date +%y%m%d`
tm2=`date +%H%M%S`

for i in $src
do
	fname=`basename $i`

	if test -f $dst/$fname
	then

		if test $cmp_test && cmp -s $i $dst/$fname
		then
			continue
		fi

		# Хååפɬ

		tm=$tm1
		if test -f $dst$old_dir/$pfx$fname.$tm
		then # Хååץե뤬ʣ

			tm=$tm1.$tm2
			if test -f $dst$old_dir/$pfx$fname.$tm
			then # Хååץե뤬ʣ
				echo "ХååפǤޤ : $dst/$fname"
				exit 1
			fi
		fi

		# Хåå
		mv $dst/$fname $dst$old_dir/$pfx$fname.$tm
		# ХååפեΥ򸽺߻ꤹ
		file $dst$old_dir/$pfx$fname.$tm > /dev/null
	fi

	# ԡ
	echo "$i -> $dst/$fname"
	cp $cp_opt $i $dst/$fname
done

exit 0
