#!/usr/bin/perl
#
# Vorbiscomment from Filename
#   usage: vcfromfilename [options] <files>
#   options:
#     -n:  Only set the track number
#
# Adds Vorbiscomment data to ogg vorbis and flac files based on the filename.
# Files should be in darkside's arbitrary file name standard, which is:
# album/artist-tracknum-title.(ogg|flac)
#
# Requires vorbiscomment and metaflac
#
# Changelog:
# 0.2.2: added flac support
# 0.2.1: fixed version in help message
#        fixed handling of dashes in artist name and title
# 0.2:   aligned version with ds-audiotools
# 0.1.1: added version variable
# 0.1:   initial implementation

$VERSION = "0.2.2";
$HELP_MSG = "Vorbiscomment from Filename $VERSION\n\tusage: vcfromfilename [options] <files>\n";

chomp($COMMENTFILENAME = '/tmp/vcfromfilename-' . `whoami`);

# Look for options
foreach (@ARGV)
{
    if (/^(-h)|(--help)$/) { die $HELP_MSG; }
    if (/^-n$/) { $tracknumonly = true; s/.*//g; }
}

# Set the album name
$album = `pwd`; # We WANT the newline here
$album =~ s/^.*\///; # Remove everything up to the last /
$album = formatName($album, 'album=');

# Parse the file list, set vorbiscomments
foreach (@ARGV)
{
    next if (/^$/);
    # check filename format
    next unless (/^\w+-\d{1,2}-(\w|-|\d)+\.(ogg|flac)$/);

    $isflac = true if (/\.flac$/);

    open COMMENTFILE, '>' . $COMMENTFILENAME;

    # Remove old flac comments that match
    if ($isflac)
    {
	system("metaflac --remove-tag=tracknumber $_");
	if (!$tracknumonly)
	{
	    system("metaflac --remove-tag=title --remove-tag=artist --remove-tag=album $_");
	}
    }
    # Preserve existing comments, except the ones we'll replace
    else
    {
	foreach (`vorbiscomment -l $_`)
	{
	    if ($tracknumonly) { print COMMENTFILE $_ unless (/^tracknumber/); }
	    else { print COMMENTFILE $_ unless (/^(title)|(artist)|(tracknumber)|(album)/); }
	}
    }

    # Create new comments from the filename
    if (!$tracknumonly)
    {
	# Add the artist
	my $artist = $_;
	$artist =~ s/^(.*?)-\d{2}-.*/\1/; # grab everything to the track number
	$artist = formatName($artist, 'artist=');
	print COMMENTFILE $artist;

	# Add the title
	my $title = $_;
	$title =~ s/.*-\d{2}-(.*)\.(ogg|flac)$/\1/; # grab everything after the track number
	$title = formatName($title, 'title=');
	print COMMENTFILE $title;

	# And the album
	print COMMENTFILE $album;
    }

    # Now get the track number
    my $tracknum = $_;
    $tracknum =~ s/.*-(\d{2})-.*/\1/;
    $tracknum =~ s/\b0*//; # removing leading zeros
    $tracknum = formatName($tracknum, 'tracknumber=');
    print COMMENTFILE $tracknum;
    
    close COMMENTFILE;

    if ($isflac) { system("metaflac --import-tags-from=$COMMENTFILENAME $_"); }
    else { system("vorbiscomment -c $COMMENTFILENAME -w $_"); }
    system('rm', $COMMENTFILENAME);
}

# Formats the first argument, adds the second argument to it
sub formatName
{
    my $name = shift;
    my $prepend = shift;
    chomp $name; # we add a newline ourselves
    $name =~ s/_/ /g; # underscore - space
    $name =~ s/^\s*//; # Remove leading space
    $name =~ s/\s*$//; # Remove trailing space
    $name =~ s/\b(\w)/uc($1)/eg; # Cap the words
    $name = $prepend . $name . "\n";
}


# Copyright (c) 2005 - 2008 John Wiggins
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA