#!/bin/sh
# overwrite: copy standard input to output
# after an EOF (from Kernighan and Pike)
opath=$PATH
PATH=/bin:/usr/bin
case $# in
1) set $1 cat ; test -p /dev/stdin || exit 2 ;;
0) echo 'Usage: over file [cmd [args]]' 1>&2 ;exit 2 ;;
esac

file=$1; shift
new=/tmp/over.$$

trap 'rm -f $new;exit 1' 1 2 15 #clean up files

if PATH=$opath "$@" > $new # collect input
then
	trap '' 1 2 15	#ignore signals
	cp $new $file
else
	echo "over: $1 failed, $file unchanged" 1>&2
	exit 1
fi

rm -f $new
exit 0
