#!/usr/bin/perl
#
#NAME
#  unblank - modify blanks in file names
#
#SYNOPSIS
#  unblank file..
#
#DESCRIPTION
#  This is a dumb little program that takes a list of file names  and
#  changes  any white space to underscores.  This can be a bit tricky
#  to do with the usual unix tools, and blanks in file  names  are  a
#  bit of a bother if you're using a shell.
#
#  The exit status is the number of renames that failed.
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$exitstat = 0;
name:
for $f (@ARGV) {
	$g = $f;
	$g =~ s/\s+/_/g;		# Convert space -> underscore
	if ($f ne $g) {
		if (-f $g) {		# Does target exist?
			unless (unlink($g)) {
				print STDERR "$0: Can't unlink \"$g\" ($!)\n";
				++$exitstat;
				next name;
			}
		}
		if (rename($f, $g)) {
			print "\"$f\" -> \"$g\"\n";
		} else {
			print STDERR "$0: Can't rename \"$f\" as \"$g\" ($!)\n";
			++$exitstat;	# Should we exit here instead?
			next name;
		}
	}
}
exit $exitstat;
