# $Id: Dir.pm,v 1.4 2003/12/09 21:43:03 ianb Exp $
package MP3::Archive::Lint::Tools::Dir;

use strict;
use warnings;

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

=head1 dir - Tool to check directories

Directory related tests. All tests performed by this tool are disabled if
the B<-n> option is given to mp3lint.

=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);

	return $self;
}

sub initscan
{
	my $self=shift;

	return 0 if $self->config->skipnametests();

	if(-d $self->{file})
	{
		return $self->initdirscan;
	}
	else
	{
		return $self->initfilescan;
	}
}

sub initdirscan
{
	my $self=shift;

	$self->settests("tracknums");

	return 1;
}

sub initfilescan
{
	my $self=shift;

	my @tests=();
	if($self->{dirartist}=$self->config->archive->dirartist($self->{path}))
	{
		push(@tests,"artist");
	}
	else
	{
#		$self->say("initscan:cannot get artist from directory name");
	}

	if($self->{diralbum}=$self->config->archive->diralbum($self->{path}))
	{
		push(@tests,"album");
	}
	else
	{
#		$self->say("initscan:cannot get album from directory name");
	}

	$self->settests(@tests);

	return 1;
}


=item B<tracknums>

Tests for non-consecutively numbered tracks.

=cut

sub tracknums
{
	my $self=shift;

	return unless($self->isalbum());
	return unless(-d $self->{file});
	return if($self->config->skipnametests);

	my $dir=$self->{file};

	unless(opendir(D,$dir))
	{
		$self->say("cannot open directory:$!");
		return;
	}

	my @files;
	@files=sort readdir(D);
	closedir(D);

	my $expected=0;
	for (@files)
	{
		next if /^\./;
		my $tracknum;
		my $thisfile=$dir."/".$_;
		$thisfile=~s,/\./,/,;
		if($tracknum=$self->config->archive->tracknum($thisfile))
		{
			$expected++;
			if($tracknum != $expected)
			{
				$self->{printfile}=$thisfile;
				$self->say("wrong track number:expected ".sprintf("%.2d",$expected).", got $tracknum");
				delete($self->{printfile});
				return;
			}
		}
	}
}


=item B<artist>

Checks the filename and directory versions of the artist name match.

To allow for multi-artist compilations, if the directory matches
the regular expression $lint_various no warning is issued.

Variables:

=over 4

=item $lint_various

(string (regular expression), default "\/Various\/")

=back

=cut


sub artist
{
	my $self=shift;
	my $artist;

	if($artist=$self->config->archive->artist($self->{pathfilename}))
	{
		my $various=$self->config->get("lint_various");
		if(($self->{dirartist} ne $artist) &&
		   ($self->{pathfilename} !~ /$various/))
		{
			$self->say("directory/filename artist mismatch");
		}
	}
}

=item B<album>

Checks the filename and directory versions of the album match.

=cut	

sub album
{
	my $self=shift;

	return unless($self->isalbum());
	
	my $album;
	if($album=$self->config->archive->album($self->{pathfilename}))
	{
		if($self->{diralbum} ne $album) 
		{
			$self->say("directory/filename album mismatch");
		}
	}
}

=back

=cut

1;
