#!/bin/sh

# cpif [ -eq -ne ] file...
# copy standard input to each of the named files
# if new * old is true or old doesn't exist;
# * defaults to -ne

PATH=/bin:/usr/bin

# set -x
op=-ne
case "$1" in
-eq|-ne)	op=$1; shift ;;
-*)		echo 'Usage: '`basename $0`' [ -eq -ne ] file...' 1>&2; exit 2
esac
case $# in
0)		echo 'Usage: '`basename $0`' [ -eq -ne ] file...' 1>&2; exit 2
esac

# to avoid /tmp attacks we write the temporary file to the
# same directory as the first argument (user surely has write 
# permission there)

new=`dirname $1`/cpif-tmp.$$
if [ -f $new ] ; then
        echo "Error: can't create temporary file: $new"
	exit 3
fi
trap 'rm -f $new; exit 1' 1 2 15	# clean up files

cat >$new
for i
do
	cmp -s $new $i
	case $op$? in
	-eq0|-ne1|*2)	cp $new $i
	esac
done
rm -f $new
