#!/usr/local/bin/perl -w
# $Id: mp3lintskip,v 1.9 2003/12/10 00:01:16 ianb Exp $

use strict;

use MP3::Archive;
use MP3::Archive::Lint::Skip;

my $me=($0=~/(?:.*\/)?(.*)/)[0];

my %opts=();

$opts{overwrite}     =0;
$opts{verbose}       =0;
$opts{deletelocal}   =0;
$opts{deleteglobal}  =0;
$opts{local}         =1;

my @delete=();

while( ($#ARGV>-1) && ($ARGV[0]=~/^-.+/) && ($_=shift))
{
	   if (/-o/) { $opts{overwrite}    = 1; }
	elsif (/-m/) { $opts{overwrite}    = 0; }
	elsif (/-g/) { $opts{local}        = 0; }
	elsif (/-l/) { $opts{local}        = 1; }
	elsif (/-v/) { $opts{verbose}      = 1; }
	elsif (/-D/) { $opts{deleteglobal} = 1; }
	elsif (/-d/) {
		while(defined($ARGV[0]) && ($ARGV[0]!~/^-.+/))
		{
			push @delete,shift;
		}
		$opts{deletelocal}  = 1;
	}
	elsif (/-h/) { usage(); }
	else { usage(); }
}


my $archive=new MP3::Archive;

my $rcskip;
if($rcskip=$archive->config->get("lint_skip"))
{
	$opts{skip}=$rcskip;
}

if($opts{deleteglobal})
{
	my $gskip=$ENV{HOME}."/.mp3lintskip-global";
	if (-e $gskip)
	{
		unless(unlink($gskip))
		{
			print "$me:cannot delete $gskip:$!\n";
		}
	}
}

if($opts{deletelocal})
{
	my $filename=".mp3lintskip";
	foreach my $dir (@delete)
	{
		my $qdir=$dir;
		#replace ' with '\''
		$qdir=~s/\'/\'\\\'\'/g;
		# enclose in quotes
		$qdir=~s/^/\'/;
		$qdir=~s/$/\'/;
		
		my $vopt="";
		if($opts{verbose})
		{
			$vopt=" -v";
			print "$me: removing .mp3lintskip files...\n";
		}
		unless(system("find $qdir -name $filename -print0 |xargs -0 -r  rm $vopt") == 0)
		{
			print "error running find: $?\n";
		}
	}
}

# if delete options but not specified filename, quit
if((($opts{deletelocal}) || ($opts{deleteglobal})) && ($#ARGV < 0))
{
	exit;
}

my $skip=new MP3::Archive::Lint::Skip(\%opts);
while(<>)
{
	chomp;
	if(/^mp3lint:/ || /^debug:/)
	{
		print "ignoring: $_\n" if($opts{verbose});
		next;
	}
	my @count=split(/:/,$_);
	if(scalar(@count) < 3)
	{
		print "malformed line, ignoring: $_\n" if($opts{verbose});
		next;
	}
	print "adding:$_\n" if $opts{verbose};
	if($opts{local})
	{
		$skip->addlocal($_);
	}
	else
	{
		$skip->addglobal($_);
	}
}

$skip->save;

sub usage
{
	die( "Usage: $me [-o] [-m] [-g] [-l] [-v] [-d path...] [-D] [-h] [-|file]\n" .
		 " -o\t\toverwrite .mp3lintskip files\n" .
		 " -m\t\tmerge .mp3lintskip files with new data (default)\n" .
		 " -g\t\twrite global .mp3lintskip-global file\n" .
		 " -l\t\twrite local .mp3lintskip files per dir (default)\n" .
		 " -v\t\tverbose\n" .
		 " -d path...\tdelete local .mp3lintskip files under path\n" .
		 " -D\t\tdelete global .mp3lintskip-global file\n" .
		 " -h\t\tthis help\n".
		 "if no filename given and -d or -D not specified, reads STDIN \n".
		 "example:  mp3lint music/ | mp3lintskip -v\n"
		 );
}

__END__


=head1 NAME

mp3lintskip - writes .mp3lintskip files from mp3lint output

=head1 SYNOPSIS

B<mp3lintskip> [I<-o>] [I<-m>] [I<-g>] [I<-l>] [I<-v>] [I<-d path>] [I<-D>] [I<-h>] (I<file>|I<->)...

=head1 DESCRIPTION

B<mp3lintskip> is used with L<mp3lint(1)> to write F<.mp3lintskip>
files to suppress warnings generated by mp3lint. For instance, if you
have an album that you only have a low bitrate copy of, and cannot
find a higher bitrate version, or you have a directory of radio plays
that you don't care are in mono, you can use this to suppress those
warnings on future runs of mp3lint, so you only get warned about new
problems.

Depending on the options set (B<-g> or B<-l>), mp3lintskip either
writes to per-directory .mp3lintskip files, or writes a global file
called .mp3lintskip-global in your $HOME directory. The latter can be
used to permanently ignore certain warnings in all directories, and
can be used for tests you don't care about or don't agree with.

Default messages to ignore can also be set in your .mp3lintrc file,
see L<mp3lintrc(5)>.

B<WARNING:> mp3lintskip works by filtering the output of mp3lint,
so it has no effect on the B<-f> (fix) option to L<mp3lint(1)>.

=head1 OPTIONS

=over 4

=item B<-o>

Overwrite .mp3lintskip files.

=item B<-m>

Merge .mp3lintskip files. This is the default.

=item B<-g> 

Write global $HOME/.mp3lintskip-global file.

=item B<-l>

Write local .mp3lintskip files per directory (default)

=item B<-v>

Verbose

=item B<-d path> 

Delete local .mp3lintskip files under B<path>.

=item B<-D>

Delete global $HOME/.mp3lintskip-global file.

=item B<-h>

Help.

=item (I<file>|I<->)

Where to read mp3lint data from. If B<-> or not specified, uses STDIN.

=back

=head1 EXAMPLES

=over 4

=item B<mp3lint music/ | mp3lintskip -v ->

Probably the most common usage pattern. Run mp3lint on your music
directory, fixing all the warnings you care about, then run the above
command to suppress all the other warnings on future runs of mp3lint,
so you only get warned about new problems.

=item B<mp3lint music |grep -v ':channels:mono:' |mp3lintskip -v>

As above, but filters out messages about mono tracks, so they
won't get skipped.

=item B<mp3lint -D -d music/>

Deletes both the global $HOME/.mp3lintskip-global file, and
any .mp3lintskip files in B<music/> and any subdirectories.

=back

=head1 FILES

=over 4	

=item B<.mp3lintrc>, B</etc/mp3lintrc>

Configuration files, see L<mp3lintrc(5)> for details.	

=item B<.mp3lintskip>, B<$HOME/.mp3lintskip-global>

Files containing messages to skip.

=back	

=head1 ENVIRONMENT

=over 4

=item  B<$HOME>

Used to find .mp3lintrc and .mp3lintskip-global	

=back

=head1 BUGS

None known. Please report any found to ianb@nessie.mcc.ac.uk	

=head1 SEE ALSO    

L<mp3lint(1)>, L<mp3lint-tools(3)>, L<mp3lintsum(1)>, L<mp3lintrc(5)>,
L<mp3-archive-tools(1)>

=head1 AUTHOR

Ian Beckwith <ianb@nessie.mcc.ac.uk>

=head1 COPYRIGHT

Copyright 2003 Ian Beckwith <ianb@nessie.mcc.ac.uk>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

=cut


