#!/usr/bin/perl -w

# TROWEL - To Render Our Wonderful Excellent Lightvectors
# Use TROWEL to apply cement
#
# Script to cement multiple images using different weights
#
# requires:
#  cementinit, cementi, plm2pnm (executables), and cement.txt in the directory
#  cement.txt can have comments, and it can also have unused lightvectors, e.g.
#  v001  # with no cement weights after it is ignored
#  (this is so you can ls v???.ppm > cement.txt, or the like and then just
#  put weights on the ones you want to use).
#
# bug reports, etc: mann@eecg.toronto.edu
#
#############################################################
#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

#die "Need a filename" if ($#ARGV != 0);

$que = $ENV{QUERY_STRING};
$que =~ s/[^0-9]//g;

#print $que;
if($#ARGV==-1)
{
    #open(COMMANDFILE,"<cement.txt");

     $ARGV[0]="/raid0/home/dusting/public_html/cgi-bin/cement.txt.$que";
    #exit(0);
}

#$ENV{PATH}=".:/usr/local/bin:$ENV{PATH}";
#$ENV{PATH}=".:/usr/local/bin:$ENV{PATH}";
$ENV{PATH}=".:/usr/local/bin:/usr/bin:/bin";

open(debugfile,">/home/dusting/public_html/cgi-bin/debugfile.txt");
print debugfile $ENV{PATH};
close(debugfile);

open(COMMANDFILE,"$ARGV[0]");

#printf("$#ARGV File name is $ARGV[0]\n");
#printf("Opening $ARGV[0]\n");


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;

#printf("cementinit $inputParams[0]->[0] $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o total.plm\n");
system("cp $inputParams[0]->[0] pork$que.jpg>/dev/null 2>&1");
system("cementinit pork$que.jpg $inputParams[0]->[1] $inputParams[0]->[2] $inputParams[0]->[3] -o yuck$que.plm > /dev/null 2>&1");

for ($vectorIndex=1;$vectorIndex<$numVectors;$vectorIndex++)
{
    #    printf("About to: ./cementi trowel_out.plm.gz $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");

    if($inputParams[$vectorIndex]->[1]!=0 || $inputParams[$vectorIndex]->[2]!=0 || $inputParams[$vectorIndex]->[3]!=0)
    {
        #        printf("cementi total.plm $inputParams[$vectorIndex]->[0] $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] \n");
        system("cp $inputParams[$vectorIndex]->[0] pork$que.jpg > /dev/null 2>&1");
        system("cementi yuck$que.plm pork$que.jpg $inputParams[$vectorIndex]->[1] $inputParams[$vectorIndex]->[2] $inputParams[$vectorIndex]->[3] > /dev/null 2>&1");
    }
}

#printf("plm2pnm total.plm -o total.ppm\n");
system("./plm2jpg yuck$que.plm yuck$que.jpg >/dev/null 2>&1");
#system("echo Content-type: image/jpeg \n\n");
print "Content-type: image/jpeg\n";
print "Expires: ",scalar gmtime(),"\n";
#printf("Content-type: text/jpeg\n\n");
print "\n";
print `cat yuck$que.jpg`;
system("rm yuck$que.* > /dev/null 2>&1");
####system("rm /raid0/home/dusting/public_html/cgi-bin/cement.txt.$que");
#system("rm $filename.plm\n");

