#!/usr/bin/perl
### ataglist takes only one argument, a filename. *.ogg, *.mp3
### or *.flac. It outputs a number of lines, that can be easily used
### to create a hash of tag->tag_value pairs in zsh. This program's
### main use is to help implement a functionality in atag, that makes
### it possible to export tag values into zsh shell variables.
### Useful for scripting.
### Calling this program by hand is probably not that useful...

# Copyright 2007, 2008, 2009
# Frank Terbeck <ft@bewatermyfriend.org>, All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS OF THE
#  PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
#  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use warnings;
use strict;
use ARename;

my ( $NAME, $VERSION ) = qw( ataglist v0.7 );

sub list {
    my ($datref, $ext) = @_;

    foreach my $tag (sort keys %{ $datref }) {
        if (defined $datref->{$tag} && $datref->{$tag} ne q{}) {
            print "$tag\n" . $datref->{$tag} . "\n";
        }
    }

    return 1;
}

if ($#ARGV < 0) {
    print "$NAME $VERSION\n";
    print "From arename version:\n";
    ## no critic
    print '            3.1' . "\n";
    ## use critic
    ARename::show_version();
    print "\n  usage: $NAME FILE\n";
    exit 0;
}

my $file = $ARGV[0];

if (! -r $file) {
    die "Can't read \"$file\": $!\n";
}

ARename::set_nameversion($NAME, $VERSION);
ARename::set_postproc(\&main::list);
ARename::set_default_methods();
ARename::disable_hooks();
ARename::set_opt("oprefix", q{});
ARename::set_file($file);
ARename::apply_methods(1);

die "No method for handling \"$file\".\n";
