#!/bin/sh

owner=$1
mode=$2
files=""

shift 2

while [ X$2 != X ]; do
    case $1 in
	*CVS*|*RCS*)
	    shift
	    continue
	    ;;
    esac
    if [ -f scratch/$1 ]; then
	files="$files scratch/$1"
    else
	files="$files $1"
    fi
    shift
done
dest=$1

if [ "$files" = " -d" ]; then
    case $dest in
	*CVS*|*RCS*)
	    true
	    ;;
	*)
	    if [ $dest != CVS -a $dest != RCS -a ! -d $dest ]; then
		mkdir -p $dest || exit 1
		chown $owner $dest 2>/dev/null
		chmod $mode $dest
		chmod u+w $dest
	    fi
	    ;;
    esac
    exit 0
fi

chf=""

for f in $files; do
    cp -p $f $dest
    if [ $? -ne 0 ]; then
       exit 1
    elif [ -d $dest ]; then
            file=`basename $f`
	    chf="$chf $dest/$file"
    else
	    chf="$chf $dest"
    fi
done

if [ "$chf" != "" ]; then
    chown $owner $chf 2>/dev/null
    chmod $mode $chf
fi
