#!/usr/bin/perl 
use strict;
use warnings;

sub UsageInfo {
  print "Usage:\n";
  print "shiftstr n s -- shifts n subtitle entries and s seconds\n\n";
}
if ( $#ARGV != 1 ) {
  UsageInfo;
  exit 1;
}

my $nShift = $ARGV[0];
my $tShift = $ARGV[1];

my $t1;
my $t2;

my $n;
my $period;
my $sline;
my $subtitle;

while ( 1 ) {
  $n = <STDIN>;
  $n or last;
  $n += $nShift;

  $period = <STDIN>;
  $period or last;
  $period =~ /(..):(..):(..),(...) --> (..):(..):(..),(...)/ or last;
  $t1 = $1 * 3600 + $2 * 60 + $3.".".$4 + $tShift;
  $t2 = $5 * 3600 + $6 * 60 + $7.".".$8 + $tShift;

  $subtitle = "";
  $subtitle .= $sline while ( defined($sline = <STDIN>) && ($sline ne "\r\n") && ($sline ne "\n") );
  $sline or last;

  print "$n\r\n";

  printf("%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
	 $t1/3600, $t1%3600/60, $t1%60, 1000*($t1 - int($t1)),
	 $t2/3600, $t2%3600/60, $t2%60, 1000*($t2 - int($t2)) );
  print $subtitle;
  print "\r\n";
}

