#! /bin/sh
## 
## Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The Regents of the University of California 
## All Rights Reserved 
## 
## Permission to use, copy, modify and distribute any part of this
## CoralReef software package for educational, research and non-profit
## purposes, without fee, and without a written agreement is hereby
## granted, provided that the above copyright notice, this paragraph
## and the following paragraphs appear in all copies.
## 
## Those desiring to incorporate this into commercial products or use
## for commercial purposes should contact the Technology Transfer
## Office, University of California, San Diego, 9500 Gilman Drive, La
## Jolla, CA 92093-0910, Ph: (858) 534-5815, FAX: (858) 534-7345.
## 
## IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
## PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
## DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
## SOFTWARE, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF
## THE POSSIBILITY OF SUCH DAMAGE.
## 
## THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE
## UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
## SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY
## OF CALIFORNIA MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES
## OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED
## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
## PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE
## ANY PATENT, TRADEMARK OR OTHER RIGHTS.
## 
## The CoralReef software package is developed by the CoralReef
## development team at the University of California, San Diego under
## the Cooperative Association for Internet Data Analysis (CAIDA)
## Program. Support for this effort is provided by the CAIDA grant
## NCR-9711092, DARPA NGI Contract N66001-98-2-8922, DARPA NMS Grant
## N66001-01-1-8909, and by CAIDA members.
## 
## Report bugs and suggestions to coral-bugs@caida.org.
## 

usage_exit() {
cat >&2 <<EOF
Usage: $0 [-p<perl_path>] [-D'<var>=<value>']... <file>

Fixperl makes minor alterations to a file containing perl code.  If
<file> ends in ".pl", the output of fixperl is a file with the same
name but without the ".pl"; otherwise, the output of fixperl
is the original file (i.e., it edits <file> in place).  The alterations
made by fixperl are controlled by the options:

-p<perl_path>      Change the "#!" line at the top of the file to contain
                   <perl_path>.  Options, if any, will be left intact.
-D'<var>=<value>'  Replace an assignment of the form
                       \$var = "oldvalue";
                   with
                       \$var = "value";
EOF
    exit 2
}

while getopts p:D: opt; do
    case "$opt" in
	p) PERL="$OPTARG";;
	D) DEF="$DEF $OPTARG";;
	*) usage_exit;;
    esac
done
shift `expr $OPTIND - 1`

if test $# -ne 1; then
    echo "$0: wrong number of arguments" >&2
    usage_exit
fi

if test -n "${PERL}"; then
    CMD="${CMD} -e '1s%^#! *[^ ]*perl[^ ]*%#! ${PERL}%'"
fi

for d in $DEF; do
    var=`echo "$d" | sed 's/=.*//'`
    val=`echo "$d" | sed 's/[^=]*=//'`
    CMD="${CMD} -e 's%\$${var} *= *\"[^\"]*\";%\$${var} = \"${val}\";%'"
done

INPUT="$@"
OUTPUT=`echo ${INPUT} | sed -e's/\.pl$//'`
if test "${INPUT}" = "${OUTPUT}"; then
    # edit in place
    INPUT=/tmp/fixperl.$$
    cp ${OUTPUT} ${INPUT} && eval sed ${CMD} < ${INPUT} > ${OUTPUT}
    RETVAL=$?
    rm -f ${INPUT} 2>&1
else
    # create edited copy
    eval sed ${CMD} < ${INPUT} > ${OUTPUT}
    RETVAL=$?
    chmod ugo+x ${OUTPUT}
fi
exit $RETVAL
