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


my $pjam_version	= "1.0.3";
my $pjam_desc		= "\nid3write version $pjam_version for PerlJammer\n";
my %opts;

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

if (GetOptions(\%opts,
               'title=s',
               'artist=s',
               'disc=s',
               'genre|g=s',
               'gid|i=i',
               'num=i',
               'year=i',
               '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 process
{
    my $file = $_[0];
    my ($mp3, $title, $track, $artist, $disc, $comment, $year, $genreID, $genre, $info);

    $mp3 = MP3::Tag->new($file) || die "Could not get tags from $file";
    $mp3->get_tags;

    $info = get_mp3info($file) || die "Could not retrieve MP3 metadata";
    ($title, $track, $artist, $disc, $comment, $year, $genre) = $mp3->autoinfo();

    print "READ title $title, track $track, artist $artist, disc $disc, comment $comment, year $year, genre $genre FROM FILE\n" if ($opts{'verbose'});
    
    $genreID = $mp3_genres{$genre};

    $title  = $opts{title}  if (defined $opts{title});
    $track  = $opts{num}    if (defined $opts{num});
    $artist = $opts{artist} if (defined $opts{artist});
    $disc   = $opts{disc}   if (defined $opts{disc});
    $year   = $opts{year}   if (defined $opts{year});

    if (defined $opts{genre})
    {
        print "Searching for GID by name:\n";
        $genre  = $opts{genre};
        $genreID = $mp3_genres{$genre};
    }
    elsif (defined $opts{gid})
    {
        print "Searching for genre by ID:\n";
        $genreID = $opts{gid};
        
        foreach my $name (sort keys %mp3_genres)
        {
            if ($mp3_genres{$name} == $opts{gid})
            {
                $genre = $name;
                last;
            }
        }
    }
    
    $mp3->{ID3v1}->remove_tag if (exists $mp3->{ID3v1});
    $mp3->{ID3v2}->remove_tag if (exists $mp3->{ID3v2});

    $mp3->new_tag("ID3v1");
    $mp3->{ID3v1}->all($title,
                       $artist,
                       $disc,
                       $year,
                       'Created by Grip',
                       $track,
                       $genreID);
    $mp3->{ID3v1}->write_tag;

    $mp3->new_tag("ID3v2");
    $mp3->{ID3v2}->add_frame('TIT2', $title);
    $mp3->{ID3v2}->add_frame('TPE1', $artist);
    $mp3->{ID3v2}->add_frame('TALB', $disc);
    $mp3->{ID3v2}->add_frame('TYER', $year);
    $mp3->{ID3v2}->add_frame('TCON', $genre);
    $mp3->{ID3v2}->add_frame('TRCK', $track);
    $mp3->{ID3v2}->write_tag;

    $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});
}


__END__

=head1 NAME

B<id3write> - Create or update ID3 tags

=head1 VERSION

Version 1.0.3

=head1 SYNOPSIS

id3write [options] mp3file [...]

=head1 OPTIONS

=over 4

=item B<-title>  "new title"

=item B<-artist> "new artist"

=item B<-disc>   "new discname"

=item B<-num>    tracknumber

=item B<-year>   year

=item B<-genre>  "new genre"

Rewrite the specified tag using the data supplied.

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

List command-line options and usage, then exit

=item B<-man>

Display full documentation and exit

=item B<-quiet>

Suppress post-write tag listing

=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<id3write> simply allows manually rewriting one or more of the id3 tags listed
above.

=head1 REPORTING BUGS

Please send all bug reports to the author.

=head1 LICENSE

B<id3write>, 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
