#!/usr/bin/perl -w
use strict;

# reads tags from STDIN and applies them to the MP3 file given as the first argument

use MP3::Tag;

my $audio_file = shift or die "Usage: $0 <mp3-file>\n";
my $mp3 = MP3::Tag->new($audio_file);

my %method_for = (
    ALBUM       => sub { $_[0]->album_set($_[1]) },
    ARTIST      => sub { $_[0]->artist_set($_[1]) },
    DATE        => sub { $_[0]->year_set($_[1]) },
    TITLE       => sub { $_[0]->title_set($_[1]) },
    TRACKNUMBER => sub { $_[0]->track_set($_[1]) },
);

while (<>) {
    chomp;
    next if /\s*#/;
    next unless /\S/;
    my ($key, $value) = split(/=/, $_, 2);
    if (exists $method_for{$key}) {
        $method_for{$key}->($mp3, $value);
    }
}

$mp3->update_tags;
