#!/usr/bin/perl
#
#NAME
#  fixnames - modify file names for shell use
#
#SYNOPSIS
#  fixnames file..
#
#DESCRIPTION
#  This is a dumb little program that takes a list of file names and makes  a
#  number  of  changes to alleviate problems when working from a shell.  Unix
#  doesn't much care about what's in file names, of course.  Except for using
#  '/' and null as special chars, any other chars are acceptable.  But shells
#  use a number of chars syntactically, and it's awkward to  deal  with  file
#  names that contain things like spaces or quotes.  So we rename them.
#
#  The substitutions that we can do are:
#
#  Strings of white space are replaced with underscores.
#
#  Quotes are deleted.
#
#  Slashes are replaced with underscores. This effectively moves the files to
#  the current directory, making the the original directory names part of the
#  file name.
#
#  The exit status is the number of renames that failed.
#
#BUGS
#  To turn particular substitutions on and off, you currently  have  to  play
#  with  commented-out  lines.   We might add some command-line options to do
#  this in a more graceful manner.
#
#  Quoting anomalies can turn up in perl.  I haven't seen any, but who knows?
#
#  This program takes more to describe than its actual code.
#
#  The "man perlfunc" description of rename() seems to have the return values
#  reversed.  Is this true on your perl?
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$exitstat = 0;
name:
for $f (@ARGV) {
	$g = $f;
	$g =~ s/\s+/_/g;		# Convert space -> underscore
	$g =~ s/['"]//g;		# Delete apostrophe and double quote
#	$g =~ s'/'_'g;			# Move to current dir
	if ($f ne $g) {
		unless (unlink($f)) {
			print STDERR "$0: Can't unlink \"$f\" ($!)\n";
			++$exitstat;
			next name;
		}
		if (rename($f, $g)) {
			print STDERR "$0: Can't rename \"$f\" as \"$g\" ($!)\n";
			++$exitstat;
			next name;
		}
		print "\"$f\" -> \"$g\"\n";
	}
}
exit $exitstat;
