#! /bin/csh -f

# This script changes perl scripts to reference perl in an accurate location

if ($#argv < 2) then
    echo "Usage: fix_perl location-of-perl-executable script-to-update"
    exit -1
    endif

set PERL = $1
set SCRIPT = $2
set TEMP = /tmp/fixperl.$USER


# figure out the script name and the args...

set orig = `head -1 $2` 

set discr = `echo $orig | grep -c '#! '`

if ( $discr == 1) then
set pgm = `echo $orig | awk -F! '{print $2}' | awk '{print $1}'`
set args = `echo $orig | awk '{print $3 " " $4 " " $5 " " $6 }'`
else
set pgm = `echo $orig | awk '{print $1}' | sed -e 's/#\!//'`
set args = `echo $orig | awk '{print $2 " " $3 " " $4 " " $5}'`
endif

# echo pgm = $pgm
# echo args = $args

# See if script is correct already

if ( "$pgm" == "$1" ) then
#	echo ok
	exit 0
endif

# Make sure this is likely to work

set pcount = `echo $pgm | grep -c perl`
if ( "$pcount" != 1 ) then
	echo "fix_perl won't work, this is not a perl script"
	exit -1
endif

# Fix it

# echo fixing it

/bin/rm -f $TEMP
echo "#! $1 $args" > $TEMP
tail +2l $2 >> $TEMP
/bin/rm -f $2.orig
mv $2 $2.orig
cp $TEMP $2
chmod +x $2
/bin/rm -f $TEMP

