#!/usr/bin/env perl
#
# sqz.pl: script to remove unwanted characters from a filename.
# Such as spaces, parenthesis, or quotes, etc.
#
# Copyright (C) 2009 Prasad J Pandit
#
# The <expression> argument specifies the characters to be removed
# from the filename. It is same as the one used with tr(1) command.
#
# Report bugs to <pj.pandit@yahoo.co.in>
#

use strict;
use warnings;

if ($#ARGV < 1)
{
    printf ("Usage: sqz.pl <expression> file1 [, file2, ...]\n");
    exit -1;
}    

my ($i, $reg, $tmp, $cmd) = (0, quotemeta ($ARGV[0]));

for ($i = 1; $i <= $#ARGV; $i++)
{
    chomp ($ARGV[$i]);
    $tmp = `printf "$ARGV[$i]" | tr -d $reg`;

    $ARGV[$i] =~ s/\'/\\'/g if ($ARGV[$i] =~ /'/);
    $ARGV[$i] =~ s/\"/\\"/g if ($ARGV[$i] =~ /"/);
    $ARGV[$i] =~ s/!/\\!/g  if ($ARGV[$i] =~ /!/);
    $ARGV[$i] =~ s/\(/\\(/g if ($ARGV[$i] =~ /\(/);
    $ARGV[$i] =~ s/\)/\\)/g if ($ARGV[$i] =~ /\)/);
    $ARGV[$i] =~ s/\&/\\&/g if ($ARGV[$i] =~ /\&/);
    $ARGV[$i] =~ s/ /\\ /g  if ($ARGV[$i] =~ / /);

    $cmd = "mv ".$ARGV[$i]." $tmp";
    
    `$cmd`;
}

exit 0;
