#!/usr/bin/perl
#This script cements in the last taken image.
# It calls cemeninit if its the first time, otherwise it calls cementi


#Packages to use
use Cwd;

#first we have some constants
$camera_out_name = "pork.ppm";
$start = cwd();
$bigjpg_dir = "vjpg";
$smalljpg_dir = "svjpg";
$smallppm_dir = "svppm";
@required_files = ("cementi","cementinit","plm2jpg");
$remote_IP = "128.100.10.200";
$login_name = "dusting";
$password = "anything";
$remote_dest_dir = "";
$ver_num ="2.0";
$trowel_out = "trowel_out.plm";
$trowel_jpg_out = "trowel_out.jpg";

if ($#ARGV != 2){ die("you must enter RGB values")};

#check to see if the required files exist
foreach $file (@required_files) {
   if (!-e $file) {
	printf("Missing file %s!\n\n",$file);
	exit -1;
   }
}

# see what highest number yet used is:
$pictnum=0;
for ($counter=999;$counter>99;$counter--){
  if (-e "$bigjpg_dir/v$counter.jpg") {
	$pictnum = $counter;
	last;
  }
}
if ($pictnum eq 0) {
	printf("No image found.\n");
	exit -1;
}

printf("cementing sv%d.jpg\n",$pictnum);

#add in reading argv stuff

	    #here we add the image using cement
	    printf("Adding the image.\n");

	    #cementing for the first time
if (!-e $trowel_out) {
 printf("Running cemeninit\n");
 `./cementinit $smallppm_dir/sv$pictnum.ppm $ARGV[0] $ARGV[1] $ARGV[2] -o trowel_out.plm`;
 printf("Add w/ colours %s %s %s\n",$ARGV[0],$ARGV[1],$ARGV[2]);
}
else {
 printf("Running cement\n");
 `./cementi $trowel_out $smallppm_dir/sv$pictnum.ppm $ARGV[0] $ARGV[1] $ARGV[2]`;
 printf("Added using colours %s %s %s\n",$ARGV[0],$ARGV[1],$ARGV[2]);
}
	    #now turn the trowel_out into a jpg using plm2jpg
	    `./plm2jpg $trowel_out $trowel_jpg_out`;
	    printf("cemented image now a jpg\n");
	
	    #check if it worked
	    if (!-e $trowel_jpg_out) {
		printf("Error creating %s.\n",$trowel_jpg_out);
	    }

	    #display it
	    `zgv $trowel_jpg_out`;

