:
##########################################################################
# Title      :	modify - modify file using arbitrary command
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1994-04-26
# Requires   :	
# Category   :	File Utilities, Text Utilities
# SCCS-Id.   :	@(#) modify	1.3 06/02/22
##########################################################################
# Description
#
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.3'

usage () {
    echo >&2 "$PN - modify files using arbitrary command, $VER (stv '94)
usage: $PN [-f] command file [...]
   -f: force execution, no interactive prompts

This program basically does the following:
	(command) < file > tmp && mv tmp file

In other words: the file will be replaced with the standard
output of the command."
    exit 1
}

echon () {
    if [ X"$ECHON" = X ]
    then
	# Determine how to "echo" without newline: "echo -n" or "echo ...\c"
	if [ X`echo -n` = X-n ]
	then ECHON=echo; E_N=; NNL="\c"
	else ECHON=echo; E_N=-n; NNL=
	fi
    fi
    $ECHON $E_N "$*$NNL"
}

interactive=true			# Ask for confirmation?

tmp=${TMPDIR:=/tmp}/mo$$
trap 'rm -f "$tmp"' 0
trap "exit 2" 1 2 3 15

if [ $# -gt 0 ] && [ X"$1" = X"-f" ]
then
    interactive=no
    shift
fi

[ $# -lt 1 ] && usage

Command="$1"
shift

for file
do
    if [ $interactive = true ]
    then
	echon >&2 "replace $file with \`($Command) < $file\` (Y/n, ^D)? "
	read OK || { echo >&2; exit 1; }
	case "$OK" in
	    "" | [yYjJ]*)	;;
	    *)	continue ;;		# No action
	esac
    fi
    cp -p "$file" "$tmp" || exit
    eval "$Command" < "$file" > "$tmp" && mv "$tmp" "$file"
done
