#!/bin/sh

# runsed - automate saving out sed output, making permanent changes to
# the input file.

# Temporary file
temp=/tmp/runsed$$

# For every file on the command line, run 'sedscript' on the input file,
# saving the output in a temporary file.  When the script is done, check
# to make sure that output was produced before copying it over the
# original.  Change 'sedscript' to be the name of the sed script you're 
# using (or rename your sed script to 'sedscript'). 

for x
do
    echo "editing $x: \\c"
    if test "$x" = sedscript; then
       echo "not editing sedscript!" 
    elif test -s $x; then 
       sed -f sedscript $x > $temp
       if test -s $temp; then
           if cmp -s $x $temp; then
             echo "file not changed: \\c"
           else
             cp $temp $x
           fi
           echo "done"
       else 
           echo "Sed produced an empty file - check your sedscript."
       fi
    else
       echo "original file is empty."
    fi
done
echo "all done"
rm -f $temp
