#!/usr/bin/perl -w

# I, Pablo Duboue, dedicate this work to the public domain (CC0 1.0)

use strict;

my($timings_file, $seconds_first, $fps, $suffix, $audio_length)=@ARGV;

open(T, $timings_file) or die "$!";
my@t=<T>;
chomp(@t);

# to seconds
my$zero=0;
foreach my$i(0..$#t){
    my($s)=$t[$i]=~m/\s(\d+)$/;
    $t[$i] =~ s/^[^ ]+ //;
    $t[$i] =~ s/ .*//;
    my@p=split(/:/,$t[$i]);
    my$base = $t[$i];
    $t[$i]=[ $p[0] * 60 * 60 + $p[1] * 60 + $p[2], $s ]; # time, slide number
    my$abs = $t[$i]->[0];
    if(!$zero){
        $zero = $t[$i]->[0];
    }
    $t[$i]->[0] -= $zero;
    #print "$base $abs $t[$i]->[0]\n";
    push @{$t[$i]}, $t[$i]->[0];
}

my$accum = $seconds_first;
foreach my$i(0..($#t - 1)){
    $accum += $t[$i+1]->[2] - $t[$i]->[2];
    $t[$i]->[0] = $accum;
}

unshift @t, [ $seconds_first, 0, 0 ];

foreach my$i(0..$#t){
    push@{$t[$i]}, $i>0? $t[$i]->[0] - $t[$i-1]->[0] : 0;
}


if(0){
    print "s\tt\t#\td\n";
    foreach my$i(0..$#t){
        print $i."\t".join("\t", @{$t[$i]})."\n";
    }
    die;
}

my$current = 0;
my$sec = 0;
while($sec < $audio_length){
    if($current == $#t){
        print "$suffix-".$t[$current]->[1].".png\n";
    }else{
        if($sec > $t[$current]->[0]){
            $current++
        }
        print "$suffix-".$t[$current]->[1].".png\n";
    }
    $sec += 1.0 / $fps;
}

