#!/usr/local/bin/perl -w
#

if ($#ARGV == 3){
    $imgDir = $ARGV[0];
    $nrImg = $ARGV[1];
    $movieName = $ARGV[2];
    $delay = $ARGV[3];
}
else {
    die print "Usage: perl makeMovie.pl imgDir nrImg movieName delay\n";
}

$movieName = "$movieName.mpg";
$myName = `whoami`; chomp($myName);

$movieDir = "/tmp/movieTMP$myName";
$command = "mkdir $movieDir >& /dev/null";
print "$command\n";
system($command);

$min=0;
$max=$nrImg;


$nrFiles=0;
$end=$min;

while ($end<$max) {
    $start=$end;
    $end=50+$start;
    if ($end>$max) {
	$end=$max;
    }
    $command = "convert -delay $delay";
    for ($i=$start; $i<$end; $i++) {
	$command = "$command $imgDir/img$i.gif";
    }
    $command = "$command $movieDir/test$nrFiles.mpg";
    print "$command\n";
    system($command);
    $nrFiles++;
}


$mpgtx = `(which mpgtx) 2> /dev/null`; chomp($mpgtx);
if (!(-e $mpgtx)) {
    die print "ERROR: Program mpgtx is not installed. Cannot merge movie parts from $movieDir.\n";
}


$command="mpgtx -j";
for ($i=0; $i<$nrFiles; $i++) {
    $command = "$command $movieDir/test$i.mpg";
}
$command = "$command -o $movieName";
print "$command\n";
system($command);

print "Movie processing finished. Check output file: $movieName.\n";
