#!/usr/bin/perl 

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(trimspaces);

my $catalog_file = $ARGV[0];

unless ($catalog_file) {
    die usage("You must specify a catalog file to import");
}

unless ( -e $catalog_file ) {
    die usage("The specified catalog file does not exist");
}

#my $CLIENT_ALBUM_ID = 4906; # set to current max album_id

my @headers = qw(
  album-name
  album-artist
  label-name
  catalog-id
  upc
  album-id
  release-date
  upc-alt
  album-custom-1
  album-custom-2
  album-custom-3
  track-name
  disc-no
  disc-name
  track-no
  track-minutes
  track-seconds
  track-artist
  isrc
  track-id
  genre
  track-custom-1
  track-custom-2
  track-custom-3
);

my %fieldMap = (
    'album-name'     => 2,
    'album-artist'   => 3,
    'label-name'     => undef,
    'catalog-id'     => 0,
    'upc'            => 1,
    'album-id'       => 0,
    'release-date'   => undef,
    'upc-alt'        => undef,
    'album-custom-1' => undef,
    'album-custom-2' => undef,
    'album-custom-3' => undef,
    'track-name'     => 6,
    'disc-no'        => undef,
    'disc-name'      => undef,
    'track-no'       => 5,
    'track-minutes'  => 10,
    'track-seconds'  => undef,
    'track-artist'   => 3,
    'isrc'           => 4,
    'track-id'       => undef,
    'genre'          => undef,
    'track-custom-1' => undef,
    'track-custom-2' => undef,
    'track-custom-3' => undef,
);

print join( "\t", @headers ) . "\n";

open CATALOG, $catalog_file || die "Can't open catalog file '$catalog_file': $!\n";
<CATALOG>;    # purge first line (headers)
my $start = tell(CATALOG);

# loop thru the first time to find albums that SHOULD have 'Various Artists'
# assigned to the album-artist field

my %album_artist = ();
while ( my $line = <CATALOG> ) {
    chomp $line;
    my @row     = split( "\t", $line );
    my $artist  = lc( $row[ $fieldMap{'album-artist'} ] );
    my $albumID = $row[ $fieldMap{'album-id'} ];

    $album_artist{$albumID}{count}++ unless ( $artist eq $album_artist{$albumID}{previous} );
    $album_artist{$albumID}{previous} = $artist;
}

seek( CATALOG, $start, 0 );

my %isrc_seen = ();
while ( my $line = <CATALOG> ) {
    chomp $line;
    my @row = split( "\t", $line );
    my %catalog = ();

    map { $catalog{$_} = defined $fieldMap{$_} ? trimspaces( $row[ $fieldMap{$_} ] ) : '' } @headers;

    next if $isrc_seen{ $catalog{isrc} };
    $isrc_seen{ $catalog{isrc} } = 1;

    # now adjust some of the fields

    $catalog{'album-artist'} = 'Various Artists' if ( $album_artist{ $catalog{'album-id'} }{count} > 1 );
    $catalog{'label-name'} = 'Alligator Records';

    #$catalog{'catalog-id'} =~ s/\D+//g;
    #$catalog{'album-id'} =~ s/\D+//g;
    $catalog{upc} =~ s/\W+//g;
    $catalog{'track-no'} =~ s/\D+//g;
    $catalog{'track-id'} = sprintf( "%s-%02d", $catalog{'album-id'}, $catalog{'track-no'} );

    if ( length $catalog{'track-minutes'} and $catalog{'track-minutes'} =~ m/^\d*:\d+$/ ) {
        ( $catalog{'track-minutes'}, $catalog{'track-seconds'} ) = split /:/, $catalog{'track-minutes'};
    } else {
        $catalog{'track-minutes'} = $catalog{'track-seconds'} = 0;
    }

    for ( my $i = 0 ; $i < scalar @headers - 1 ; $i++ ) {
        print $catalog{ $headers[$i] }, "\t";
    }
    print $catalog{ $headers[-1] }, "\n";
}

