#!/usr/bin/perl -w

#  TROWEL - To Render Our Wonderful Excellent Lightvectors
#  Use TROWEL to apply cement
#
#  Script to cement multiple images using different weights
#  using the pnmcement.c program
#
#  Expects to find blank.pdm and cement in the same directory
#
#############################################################

use strict;                     #To try and stop me from installing bugs

my($lineOfInput,@inputParams);  # inputparams is a list of lists, each sublist
                                # containing picturename and the 3 weights. Yes
                                # it hurts readability, but it gives me so much
                                # more flexibility later...
open(COMMANDFILE,"<cement.txt");

my($vectorIndex,$numVectors);
$vectorIndex = 0;

#Parse file into aforementioned nasty array of arrays
while ($lineOfInput = <COMMANDFILE>) {
  $lineOfInput =~ s/^(.*?)#(.*)$/$1/;
  if ($lineOfInput =~ /^([\S]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)\s+([\.|\d|-]+)/ ) {
      $inputParams[$vectorIndex] -> [0] = $1;
      $inputParams[$vectorIndex] -> [1] = $2;
      $inputParams[$vectorIndex] -> [2] = $3;
      $inputParams[$vectorIndex] -> [3] = $4;
      $vectorIndex++;
  }
  elsif (!($lineOfInput =~ /^\s*$/)) {
    #Allow blank lines once comments are stripped, but complain about other imparsables
    printf "Bad input line:\n$lineOfInput";
  }

}

$numVectors = $vectorIndex;
$vectorIndex = 0;

system("/bin/cp ./black.pdm ./temp_trowel.pdm");

for ($vectorIndex=0;$vectorIndex<$numVectors;$vectorIndex++) {
  printf("About to: ./cement temp_trowel.pdm $inputParams[$vectorIndex]->[0].ppm $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] > temp_trowel2.pdm\n");
  system("./cement temp_trowel.pdm $inputParams[$vectorIndex]->[0].ppm $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] > temp_trowel2.pdm");
  system("mv temp_trowel2.pdm temp_trowel.pdm");
}

system("./pnmdouble2uchar temp_trowel.pdm -o trowel_built.ppm");

