#!/usr/bin/perl
use Ogg::Vorbis::Header;
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.1";
my $pjam_desc		= "\noggread version $pjam_version for PerlJammer\n";

my %opts;

if (GetOptions(\%opts,
               'quiet|q',
               '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 read_tags
{
    my ($file) = @_;

    if ($file =~ /\.ogg$/i)
    {
        my ($ogg, $title, $track, $artist, $disc, $comment, $year, $genre, $length);

        $ogg = Ogg::Vorbis::Header->new($file);
            
        $length = $ogg->info('length');
        printf("File:\t%s\n\t%s :: %s :: %s [%02d] (%02d:%02d, %s, %d)\n",
               $file,
               $ogg->comment('artist'),
               $ogg->comment('album'),
               $ogg->comment('title'),
               $ogg->comment('tracknumber'),
               $length / 60,
               $length % 60,
               $ogg->comment('genre'),
               $ogg->comment('date'));
    }
    else
    {
        print "Skipping $file : Not an Ogg Vorbis 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<oggread> - Read and display Ogg Vorbis tags

=head1 VERSION

Version 1.0.1

=head1 SYNOPSIS

oggread [options] oggfile [...]

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

Display version string and exit

=back

All options can be abbreviated to their first letters.

=head1 DESCRIPTION

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

=head1 REPORTING BUGS

Please send all bug reports to the author.

=head1 LICENSE

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