#! /usr/bin/perl -w

########################################################################
#### file          : srt2ltt.pl
#### date          : april 28 1998
#### author        : Cedric Auzanne for NIST
#### description   : parses a srt file to create a ltt file
####
########################################################################
#### usage         : srt2ltt.pl srtfile_full_path
####
########################################################################


########################################################################
#### check parameters
die "\nERROR : no srt filename supplied\n" if ($#ARGV != 0);

$srtfilename      = $ARGV[0];
$lttfilename      = "$1.ltt" if ($srtfilename =~ /(.*?).srt/);

die "\nERROR : srt file $srtfilename not found\n" if (! -f $srtfilename);

print "Processing file $srtfilename to $lttfilename\n";


########################################################################
#### open output file, print comments

open OFD, ">$lttfilename" or die "\nERROR : can't open output file $lttfilename\n";
print OFD "<Comment>\n";
print OFD "\tThis file was generated by srt2ltt.pl\n";
print OFD "\tdate      : ", scalar localtime, "\n";
print OFD "\tsrt file  : $srtfilename\n";
print OFD "</Comment>\n\n";



########################################################################
########################################################################
########################################################################
#### start processing

$string = "";         # output string, needed to wrap words.

# read each line in srt file and process
open SRTFD, "<$srtfilename" or die "ERROR : can't open srt file $srtfilename\n";
while (<SRTFD>)
{
    # line is episode tag
    if ((/^<Episode/) || (/^<\/Episode/))
    {
	print OFD "\n$_\n";
	next;
    }

    # line is section tag
    if (/^<Section/)
    {
        print OFD $_;
	$string = "";
        next;
    }

   # line is end of section tag
    if (/^<\/Section/)
    {
	print OFD "$string\n";
	$string = "";
        print OFD "$_\n";
        next;
    }
   
    # line is a word
    if (/^<Word\s+S_time=(\d+\.\d+)\s+E_time=(\d+\.\d+)\s*>\s+(.*?)\s+<\/Word>/)
    {
	my $word = $3;
	$word =~ tr/A-Z/a-z/;
	my $wordsize = length $word;
	if (((length $string) >= 78) || (($wordsize + (length $string)) >= 76))
	{
	    print OFD "$string\n";
	    $string = "";
	}
	$string = "$string $word";
    }
    
    # evrything else is lost.

} # while
close SRTFD;


# finish processing
print OFD "\n<Comment>\n\tend of file\n</Comment>\n";
close OFD;

print "Done.\n";








