#!/usr/bin/perl
my $verbose=0;
use strict;
use warnings;
use diagnostics;
use Data::Dumper;

sub showVals{
    my $sRef=shift;

#    print Dumper($sRef);
#   print map { "$_=$$sRef{$_}\n" } sort keys %$sRef;
    printf "Title: %s Start: %s End: %s\n", 
    $sRef->{title},
    $sRef->{start},
    $sRef->{end};
    return;
}

sub insert {
    #adapt from mediadb.pl
    my $fn=shift;
    die unless $fn;

    $fn=$dbh->quote($fn);

    my $st=sprintf "INSERT INTO mp3s (path) VALUE (%s)", $fn;
    printf "$st\n";
    $st=$dbh->prepare($st);
    my $result=$st->execute();
    my $error = $dbh->errstr;
    die $error if $error;

    die "Table unchanged!" if $result=~/0E0/;

    $st=sprintf "SELECT id FROM mp3s WHERE path = %s", $fn;
    $st=$dbh->prepare($st);
    $st->execute();

    my @row_ary=$dbh->selectrow_array($st);

    printf "Found id: %s\n", $row_ary[0];
}

foreach my $arg (@ARGV) { 
    next if $arg eq '';
    if($arg=~/-v/) {
	$verbose=1;
	shift;
	next;
    }
}

    
while(<>) {
    chomp;
    my ($start,$end,$title,@cosmicDebris)=split /\s+/;
    die "No title!" unless length($title);
    die "No start!" unless $start=~/\d+/;
    die "No end!"   unless $end=~/\d+/;

    #die "Duplicate filename: $title" if exists $titleH{$title};

    my $song_ref={
	title=>$title,
	start=>$start,
	end=>$end,
    };

    showVals($song_ref);
}

__END__
    my $%song=\$_;

    die "no start: $song->[start]" unless $song->[start];
    die "no end: $song->[end]" unless $song->[end];

    my $totalSecs=$song->[end]-$song->[start];
    die "Invalid length!" unless $totalSecs;

    my $hours = int ($totalSecs / 3600);
    $totalSecs = $totalSecs % 3600;

    my $mins = int ($totalSecs / 60);
    my $secs = $totalSecs % 60;

    if($hours > 0 or $verbose) {
	printf "%-20s %12s %12s %02s:%02s:%02s\n",
        $song->[title],$song->[start],$song->[end],$hours,$mins,$secs; 
    }
    else {
	printf "%-20s %02s:%02s\n", $song->title,$mins,$secs;
    }

    my @return=($mins,$secs);
    return @return;
}


__DATA__
760.277125	1009.945417	ActLikeNothingsWrong
1365.080567	1588.281917	HotLesPaulAction
1652.106445	1860.336327	DontTellMingNa
1970.851701	2181.726928	Turnip
2193.437734	2405.710148	Bandicoot
2525.251338	2731.825414	FarBeItFromMe
3070.351383	3280.860712	FarBeItFromMe
3756.682460	4032.164405	Reset
4075.466220	4234.241957	FloatFactor
4294.722710	4573.645236	FloatFactor
4642.605758	4928.056036	Reset
4987.561644	5220.098748	Day


__END__
# @common = inter( \%foo, \%bar, \%joe ); 
# sub inter {
#     my ($k, $href, %seen); # locals
#     foreach $href (@_) {
#         while ( ($k) = each %$href ) {
#             $seen{$k}++;
#         } 
#     } 
#     return grep { $seen{$_} == @_ } keys %seen;
# }
# http://docstore.mik.ua/orelly/perl/prog/ch02_07.htm#PERL2-CH-2-SECT-7.2
