# $Id: M3u.pm,v 1.4 2003/12/14 09:43:56 ianb Exp $
package MP3::Archive::Lint::Tools::M3u;

use strict;
use warnings;

use Cwd;

use vars qw(@ISA $VERSION);
@ISA = qw(MP3::Archive::Lint::Tool);
$VERSION = '0.01';

=head1 m3u - Tool to check .m3u playlists 

Checks m3u playlists, although will also work if they are just lists
of files.

=head2 Tests

=over 4

=cut

sub new
{
	my $proto=shift;
	my $class=ref($proto) || $proto;
	my $opts=shift;
	my $self=$class->SUPER::new($opts);;
	bless($self,$class);


	$self->settests("playlist");
	
	return $self;
}


sub initscan
{
	my $self=shift;

	return 0 if (-d $self->{file});
	return 0 unless $self->{filename}=~/\.m3u$/i;

	return 1;
}

=item B<playlist>

Checks all the files mentioned in the m3u file exists. Attempts to be
(slightly) smart about relative pathnames and backslashes for pathname
delimiters. Complains if it finds drive letters in the filenames
mentioned.

=cut

sub playlist
{
	my $self=shift;
	my $path=$self->{path};

	my $warneddrive=0;
	unless(open(M,$self->{file}))
	{
		$self->say("cannot open file");
		return;
	}
	while(<M>)
	{
		chomp;
		my $orig=$_;
		# skip comments, dont currently do anything with EXTINF
		next if /^#/; 
		s/\\/\//g; # swap path delims '\' => '/'
		s/\r//g; # get rid of ^M

		if(/^[A-Za-z]\:/)
		{
			unless($warneddrive)
			{
				$self->say("playlist entries have drive letters:$orig");
			}
			$warneddrive++;
		}
		
		unless (-e)
		{
			unless(-e ($path."/".$_))
			{
				$self->say("entry not found:$orig");
			}
		}
	}
	close(M);
}

=back

=head2 Bugs

Currently doesn't do anything with EXTINF comments.

=cut

1;
