#!/usr/bin/perl

# ----------------------------------------------------------------
#
# fixMusicNames.pl
#
# A tool from the MusicBank system
#
# Copyright (C) 2005 Josef C. Grosch (jgrosch@mooseriver.com)
#
# 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.
# 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
#
# ----------------------------------------------------------------

#
# $Id: fmn.html,v 1.1 2008/07/23 01:18:45 jgrosch Exp $
#

use English;
use Getopt::Std;
use File::Basename;
use MusicBank;
use strict;

my (@files, $file, @lines, $line, $new_line, @bits, $bit);
my (@parts, $part, $uc_bit, $part_save, $dir, @dirs, $fqp);
my ($i, $o_loc, $str, $stage_base, $alpha_base);

use vars qw($opt_h $opt_b $opt_f $opt_d);

getopts('hb:f:d:');
&Usage if $opt_h ne "";
$music_base = $opt_b if $opt_b ne "";
$dir = $opt_d if $opt_d ne "";
$file = $opt_f if $opt_f ne "";

$stage_base = "$music_base/Rip/Stage";
$alpha_stage = "$music_base/Rip/AlphaStage/";

if ($dir ne "") {
    chdir $dir;
} else {
    chdir $stage_base;
}

if ($file ne "") {
    push @files, $file;
} else {
    @files = `find . -print | grep m3u`;
}

$i = 0;

#
# Meat and Pototoes here
# Walk throught the m3u file correcting the file names
# according to the Music Bank standard
# http://www.musicbank.com/pages/Standard/index.html#file_names
#
foreach $file (@files) {
    if ($str eq "") { chop $file; }

    open INFILE, "<$file" or die "$! : Can't open $file\n";
    @lines = ;
    close INFILE;

    foreach $line (@lines) {
        chop $line;
        $new_line = $line;

        #
        # Normlize the path to the music file in
        # the m3u
        #
        $line =~ tr/A-Z/a-z/;
        @parts = split /\//, $line;
        foreach $part (@parts) {
            $part_save = $part;
            @bits = split /\_/, $part;
            foreach $bit (@bits) {
                $uc_bit = ucfirst $bit;
                $part =~ s/$bit/$uc_bit/;
            }   # end of foreach $bit
            $part =~ tr/_/-/;
            $line =~ s/$part_save/$part/;
        }   # End of foreach $part

        #
        # Move the ripped music file to the new location
        # with the file name normilized
        #
        @dirs = split /\//, $line;  # 35
        $fqp = $alpha_stage;
        foreach $dir (@dirs) {
            if ($dir =~ m/mp3|ogg|flac|shn|wav/) {
                $i++;
                $fqp = sprintf("%s%2.2d--%s", $fqp, $i, $dir);
                $o_loc = $stage_base . "/" . $new_line;
                if (-e $o_loc) {
                    print "$fqp\n";
                    `mv $o_loc $fqp`;
                }
            } else {
                $fqp = $fqp . $dir . "/";
                if (! -e $fqp) { `mkdir -p $fqp`; }
            }
        }   # End of foreach $dir
    }   # End of foreach $line
    $i = 0;
}   # end of foreach loop

exit;

# ----------------------------------------------------------
# Usage
# ----------------------------------------------------------
sub Usage {
    my ($name, $path, $suffix, $str);

    ($name, $path, $suffix) = fileparse($PROGRAM_NAME, '\.pl');
    $str = sprintf("Usage : %s%s [-h] 
                         [-b ] 
                         [-f <[fqp].m3u>]
                         [-d ]\n",
                   $name, $suffix);
    print $str;
    exit;
}   # End of Usage

    



Webmaster@musicbank.com