#!/usr/bin/perl -w

use strict;
use warnings;

if (@ARGV < 2)
{
	print "<fichier ctm> <rep out>"
}
else
{
	my $Fic = $ARGV[0];
	my $RepOut = $ARGV[1];
	my $Name;
	my $Start;
	my $Duration;
	my $Word;
	my $MinStart=1000000000;
	my $NewZero;
	my $WordNum=0;
	my $BaseName;

	$ARGV[0]=~/(.*?)\.(.*)/;
	$BaseName = $1;
	$BaseName=~/.*\/(.*)/;
	$BaseName=$1;

	open (CTM, $Fic) or die "Ouverture de $Fic loupée";

	foreach my $Ligne (<CTM>)
	{
		if ( $Ligne=~/(.+) (.+) (.+) (.+) (.+)/ )
		{
			($Name,$Start,$Duration,$Word)=($1,$3,$4,$5);

			$Start*=100;
			$Start=int($Start+.5);

			if ($Start < $MinStart)
			{
				$MinStart = $Start;
			}

			#print "--> Start : $Start ,  Minstart : $MinStart \n";

			$NewZero=int($Start) - int($MinStart);

			#print "--> Start : $Start ,  Minstart : $MinStart , zero : $NewZero\n";

			$Duration*=100;
			$Duration=int($Duration+.5);

			if ($Duration <= 5)
			{
				$Duration = 6;
			}

			$Word=~s/'/_/g;

			`echo "${BaseName}_${WordNum} ${BaseName} $NewZero $Duration" >> ${RepOut}/${BaseName}.gvalign`;
			`echo "$Word" > ${RepOut}/${BaseName}_${WordNum}.gv`;

			$WordNum++;
		}
	}

	close(CTM);
}






