#!/usr/bin/perl -w
use File::Basename; 

my $verbose=0;
if($ARGV[0]=~/-v/){
    $verbose=1;
    printf "Title                   Start   End         Length\n";
    shift;
    }

while(<>){
    chomp;

    next unless /<label /;
    #<label t="244.14875652" t1="421.18633779" title="Sussurus"/>

    my ($j0,$start,$j1,$end,$j2,$title)=split /\"/;
    warn "no title:" unless $title;

    my @titleWords = split /_/, $title;
    $title=formatTitle(@titleWords) if $#titleWords;

    my $len=$end-$start;

    if($verbose){
	printf "%-20s\t%02.02f\t%02.02f\t%02.02f\t%s", 
	$title,$start,$end,$len,convertTime($len);
    }
    else {
	printf "%-20s\t%s", $title,convertTime($len);
    }
}

sub formatTitle {
    my @words=@_;
    my $fLine='';
    foreach my $word (@words) {
	chomp($word);
	$fLine.=sprintf("%-20s\n", $word);
    }
    chomp($fLine);
    return $fLine;
}

sub convertTime {
    my $seconds=shift;
    warn "no seconds" unless $seconds;

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

    my $minutes = int ($seconds / 60);
    $seconds = $seconds % 60;
    printf "%02d:%02d:%02d\n", $hours,$minutes,$seconds;

    my %timeVal=(
	'hour'=>$hours,
	'min'=>$minutes,
	'sec'=>$seconds
	);

    my $timeVal_ref= \%timeVal;
    return $timeVal_ref;
}
