#!/usr/bin/perl
use Ogg::Vorbis::Header;
use Pod::Usage;
use Getopt::Long;
use strict;
use utf8;
use open ':std', ':encoding(UTF-8)';


my $pjam_version	= "1.0.1";
my $pjam_desc		= "\noggwrite version $pjam_version for PerlJammer\n";

my %opts;
if (GetOptions(\%opts,
               'title=s',
               'artist=s',
               'disc=s',
               'genre|g=s',
               'gid|i=i',
               'num=i',
               'year=i',
               'help|usage|?',
               'man',
               '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
    {
        while (@ARGV)
        {
            process(shift);
        }
    }
}
else
{
    print "\n";
    pod2usage(-message => $pjam_desc,
              -exitstatus => -1,
              -verbose => 1);
}

exit (0);


sub process
{
    my $file = $_[0];
    if ($file =~ /\.ogg$/i)
    {
        my ($ogg, $length);
        my $write = 0;

        $ogg = Ogg::Vorbis::Header->new($file) || die "Could not get tags from $file";
        $length = $ogg->info('length');

        $opts{tracknumber}  = $opts{num} if (defined $opts{num});
        $opts{album}        = $opts{disc}   if (defined $opts{disc});
        $opts{date}         = $opts{year}   if (defined $opts{year});
        
        foreach my $tag('title', 'album', 'artist', 'tracknumber', 'date', 'genre')
        {
            if (defined $opts{$tag})
            {
                $ogg->delete_comment($tag);
                $ogg->add_comments($tag, $opts{$tag});
                printf("Tag %s rewritten as '%s'\n",
                       $tag,
                       $ogg->comment($tag));
                $write++;
            }
        }
        
        if ($write > 0)
        {
            $ogg->write_vorbis || die "Could not write!";
        }

        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\n\tPlay time:\t%02d:%02d\n",
               $file,
               $ogg->comment('title'),
               $ogg->comment('artist'),
               $ogg->comment('album'),
               $ogg->comment('tracknumber'),
               $ogg->comment('date'),
               $ogg->comment('genre'),
               $length / 60,
               $length % 60);
    }
#           $file, $title, $artist, $disc, $track, $year, $genre, $genreID, $info->{MM}, $info->{SS});
}


__END__

=head1 NAME

B<oggwrite> - Create or update Ogg Vorbis tags

=head1 VERSION

Version 1.0.1

=head1 SYNOPSIS

oggwrite [options] oggfile [...]

=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, -version>

Display version string and exit

=back

All options can be abbreviated to their first letters.

=head1 DESCRIPTION

B<oggwrite> simply allows manually rewriting one or more of the tags in an Ogg Vorbis
audio file.

=head1 REPORTING BUGS

Please send all bug reports to the author.

=head1 LICENSE

B<oggwrite>, part of the B<PerlJammer> package, is copyright (C) 2022 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
