#!/usr/bin/perl
use MP3::Tag;
use MP3::Info qw(:all);
use Pod::Usage;
use Getopt::Long;
use Cwd qw(realpath);
use strict;
use utf8;
use open ':std', ':encoding(UTF-8)';


my $pjam_version	= "1.0.4";
my $pjam_desc		= "\nid3read version $pjam_version for PerlJammer\n";
my %opts;

Getopt::Long::Configure ("bundling_override");

if (GetOptions(\%opts,
               'quiet|q',
               'help|usage|?',
               'man',
               'verbose|v',
               'version|V'))
{
    if ($opts{version})
    {
        pod2usage(-message => $pjam_desc,
                  -exitstatus => 0,
                  -verbose => 0);
    }
    elsif ($opts{'help'})
    {
        pod2usage(-message => $pjam_desc,
                  -exitstatus => 0,
                  -verbose => 1);
    }
    elsif ($opts{'man'})
    {
        pod2usage(-message => $pjam_desc,
                  -exitstatus => 0,
                  -verbose => 2);
    }
    else
    {
        MP3::Tag->config(write_v24 => 1, prohibit_v24 => 0);
        use_winamp_genres();
        
        while (@ARGV)
        {
            process(shift);
        }
    }
}
else
{
    print "\n";
    pod2usage(-message => $pjam_desc,
              -exitstatus => -1,
              -verbose => 1);
}

exit (0);


sub read_tags
{
    my ($file) = @_;
    my (@terms, $mp3, $tags);
    
    if ($file =~ /\.mp3$/i)
    {
        $mp3 = MP3::Tag->new($file);
        $mp3->get_tags;
        print "ID3v1 tags found\n" if (exists $mp3->{ID3v1} && $opts{'verbose'});
        print "ID3v2 tags found\n" if (exists $mp3->{ID3v2} && $opts{'verbose'});

        my ($title, $track, $artist, $disc, $comment, $year, $genre) = $mp3->autoinfo;
        print "Detected title $title, track $track, artist $artist, disc $disc, comment $comment, year $year, genre $genre\n" if ($opts{'verbose'});
    
        my $info = get_mp3info($file) || die "Could not retrieve MP3 metadata";
        my $genreID = $mp3_genres{$genre};
        
        $mp3->close;

        printf("File:\t%s\n\tTitle:\t\t%s\n\tArtist:\t\t%s\n\tDisc:\t\t%s\n\tTrack:\t\t%02d\n\tYear:\t\t%4d\n\tGenre:\t\t%s (%d)\n\tPlay time:\t%02d:%02d\n",
               $file, $title, $artist, $disc, $track, $year, $genre, $genreID, $info->{MM}, $info->{SS});
    }
    else
    {
        print "Skipping $file : Not an MP3 file\n" if ($opts{'verbose'});
    }
}


sub process
{
    my $pathname = $_[0];

    $pathname = realpath($pathname);
    if (-f $pathname)
    {
        read_tags($pathname);
    }
    elsif (-d $pathname)
    {
        recurse_into($pathname);
    }
    else
    {
        print "Skipping $pathname: Not a plain file or directory\n" unless ($opts{'quiet'});
    }
}


sub recurse_into
{
    my $path = $_[0];

    print "Entering $path\n" unless ($opts{'quiet'});

    chdir($path);

    opendir(DIR, '.');
    my @files = grep (!/^\./, readdir(DIR));
    closedir(DIR);

    foreach my $file (sort @files)
    {
        process($file);
    }
    print "Leaving $path\n" unless ($opts{'quiet'});

    chdir('..');
}
__END__

=head1 NAME

B<id3read> - Read and display ID3 tags

=head1 VERSION

Version 1.0.4

=head1 SYNOPSIS

id3read [options] mp3file [...]

=head1 OPTIONS

=over 4

=item B<-quiet>

Skip informational status messages

=item B<-help, -usage, -?>

List command-line options and usage, then exit

=item B<-man>

Display full documentation and exit

=item B<-v, -verbose>

Display additional diagnostic information

=item B<-V, -version>

Display version string and exit
All options can be abbreviated to their first letters.

=back

All options can be abbreviated to their first letters.

=head1 DESCRIPTION

B<id3read> reads and displays the principal ID3 tags and metadata (title, artist,
disc, track, year, genre, play time) from a specified MP3 file or files.  Files
which are not MP3 files are automatically skipped.

=head1 REPORTING BUGS

Please send all bug reports to the author.

=head1 LICENSE

B<id3read>, part of the B<PerlJammer> package, is copyright (C) 2003 Phil Stracchino.

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 3 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, see <http://www.gnu.org/licenses/>.

=head1 OBTAINING PerlJammer

B<PerlJammer> can be downloaded from http://co.ordinate.org/perljammer/.

=head1 AUTHOR

B<PerlJammer> and its supporting tools are written and maintained by Phil Stracchino
(phil@co.ordinate.org).

=cut
