#!/usr/bin/perl
#   rename perlexpr [files]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# My favorite example is the 'rename' script from  the  distribution; #
# it's the fastest way to change all .o files to .o.bak, etc.:        #
#   rename 's/$/.bak/' *.o                                            #
# Note  that this script will read file names from stdin if there are #
# none on the command line.                                           #
#                                                                     #
# Bug fix: On some systems, the rename won't occur if  the  new  name #
# exists;  on  others, the old file with the new name will disappear. #
# I've fixed this by using the Backup.pm module  to  change  the  old #
# name  to  whatever the default backup scheme does (appending '-' by #
# default).                                                           #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Here's where I link my .pm files:
	$HOME = $ENV{'HOME'} || '.';
	push @INC, "$HOME/lib", "$HOME/sh";
	require "Backup.pm";	# Rename files for backup

($op = shift) || die "Usage: rename perlexpr [filenames]\n";
if (!@ARGV) {
	@ARGV = <STDIN>;
	chop(@ARGV);
}
for $old (@ARGV) {
	$_ = $old;
	eval $op;
#	eval("$new =~ $op");	# Why doesn't this work?
	die $@ if $@;			# Die if invalid pattern
	$new = $_;				# Is this necessary?
	if ($new ne $old) {
		Backup($new) if -f $new;
		rename($old,$new);
#	} else {
#		print "$0: $old not changed.\n";	# Debug info.
	}
}
