#!/usr/bin/perl

#`setenv UI_DIR /home/embedded/UNIXimager`;
#`export UI_DIR=/home/embedded/UNIXimager`;
$ENV{UI_DIR} = "/home/embedded/UNIXimager";

#Packages to use
use Cwd;

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

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

if (!-d $bigjpg_dir) {
   printf("Creating %s/%s/\n",$start,$bigjpg_dir);
   `mkdir $bigjpg_dir`;
   if (!-d $bigjpg_dir) {die("Cannot create dir ",$start,"/",$bigjpg_dir);}
}

if (!-d $smalljpg_dir) {
   printf("Creating %s/%s/\n",$start,$smalljpg_dir);
   `mkdir $smalljpg_dir`;
   if (!-d $smalljpg_dir) {die("Cannot create dir", $start,"/",$smalljpg_dir);}
}

if (!-d $smallppm_dir) {
   printf("Creating %s/%s/\n",$start,$smallppm_dir);
   `mkdir $smallppm_dir`;
   if (!-d $smallppm_dir) {die("Cannot create dir ",$start,"/",$smallppm_dir);}
}

# see what highest number yet used is:
$pictnum=100;
for ($counter=100;$counter<999;$counter++){
  if (-e "$bigjpg_dir/v$counter.jpg") {
	$pictnum = $counter + 1;
  }
}
printf("\n\nWe are starting at picture sv%d.jpg\n",$pictnum);
printf("Start directory is %s.\n\n\n",$start);

#Now we have $pictnum representing the next picture we wish
#to take.  We now loop infinitely untill se are done dusting

while (1) {
    if (-e $camera_out_name) {
	printf("%s already exists.  Remove? ",$camera_out_name);
        sysread stdin,$input,128;
	if ($input eq "y\n" or $input eq "yes\n") {
	    `rm $camera_out_name`;
	    printf("Removed file %s.\n",$camera_out_name);
	}
	else {
	    printf("Remove %s, then restart.\n\n",$camera_out_name);
	    exit -1;
	}
    } 	
    while(1) {
    	printf("Type 'y' or 'yes' to take a picture, q to quit\n\n");
    	sysread stdin,$input,128;
	if ($input eq "y\n" or $input eq "yes\n") {
	    `../UNIXimager/bin/dcs -x -f ppm -a $camera_out_name`;
	    #temp debug
	    #`touch $camera_out_name`;
	}
	elsif ($input eq "q\n") {exit 0;}
	else {next;}
	
	#user has said yes
	if (!-e $camera_out_name) {
	    #picture didn't take
	    printf("I think there was an error taking the picture.\n\n");
	    next;
	}	
	else {last;}
    }

    #now we convert the picture to other formats.
    `./ppm2all $camera_out_name $smalljpg_dir/sv$pictnum.jpg $smallppm_dir/sv$pictnum.ppm $bigjpg_dir/v$pictnum.jpg`;
    printf("Converted %s to %s/sv%s.jpg, %s/sv%s.jpg, %s/v%s.jpg.\n",$camera_out_name,$smalljpg_dir,$pictnum,$smallppm_dir,$pictnum,$bigjpg_dir,$pictnum);
    # maybe add some error checking here
    if (!-e "$bigjpg_dir/v$pictnum.jpg") {
	printf("Error creating big jpg %s/v%s.jpg!\n",$bigjpg_dir,$pictnum);
    }
    else { `rm $camera_out_name`; }

    #Now we send it over
    `rm ~/.netrc`;
    `touch ~/.netrc`;
    `echo "machine $remote_IP" >> ~/.netrc`;
    `echo "login $login_name" >> ~/.netrc`;
    `echo "password $password" >> ~/.netrc`;
    `echo "macdef init" >> ~/.netrc`;
    `echo "bin" >> ~/.netrc`;
    `echo "put $start/$smalljpg_dir/sv$pictnum.jpg sv$pictnum.jpg" >> ~/.netrc`;
    `echo "bye" >> ~/.netrc`;
    `echo >> ~/.netrc`;
    #won't work unless only you can read/execute it
    `chmod 600 ~/.netrc`;
    `ftp $remote_IP`;
    printf("Sent image\n\n");

    `zgv $smalljpg_dir/sv$pictnum.jpg`;

    while (1) {
	printf("Would you like to cement in (include) this image?\n");
	sysread stdin,$input,128;
	if ($input ne "y\n" and $input ne "yes\n") {
	    printf("image not added.\n");
	    last;
	}
	else {
	    printf("What colour would you like to add it in?\n");
	    sysread stdin,$input,128;
	    #parse input by skipping whitespace, placing values in the array
	    @colour = split /\s+/,$input;

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

	    #cementing for the first time
	    if (!-e $trowel_output) {
	        printf("Running cemeninit\n");
	        `./cementinit $smallppm_dir/sv$pictnum.ppm @colour[0] @colour[1] @colour[2] -o trowel_out.plm`;
	        printf("Added using colours %s %s %s\n",@colour[0],@colour[1],@colour[2]);
	    }
	    else {
		printf("Running cement\n");
		`./cementi $trowel_output $smallppm_dir/sv$pictnum.ppm @colour[0] @colour[1] @colour[2]`;
	        printf("Added using colours %s %s %s\n",@colour[0],@colour[1],@colour[2]);
	    }
	    #now turn the trowel_output into a jpg using plm2jpg
	    `./plm2jpg $trowel_output $trowel_jpg_output`;
	    printf("converted cemented image to a jpg\n");
	
	    #check if it worked
	    if (!-e $trowel_jpg_output) {
		printf("Error creating %s.\n",$trowel_jpg_output);
	    }
	    else {
		#send it over
	        `rm ~/.netrc`;
    		`touch ~/.netrc`;
    		`echo "machine $remote_IP" >> ~/.netrc`;
    		`echo "login $login_name" >> ~/.netrc`;
    		`echo "password $password" >> ~/.netrc`;
    		`echo "macdef init" >> ~/.netrc`;
                `echo "bin" >> ~/.netrc`;
    		`echo "put $trowel_jpg_output fsv$pictnum.jpg" >> ~/.netrc`;
    		`echo "bye" >> ~/.netrc`;
    		`echo >> ~/.netrc`;
    		#won't work unless only you can read/execute it
    		`chmod 600 ~/.netrc`;
    		`ftp $remote_IP`;
	        printf("Sent the cemented file over.\n");
                `zgv $trowel_jpg_output`;
	    }	
	}
    	last;
    }
    printf("Hit q to quit, anything else to take next picture\n");
    sysread stdin,$input,128;
    if ($input eq "q\n") {
	printf("Thanks for using Dusting Script Version %s.\n",$ver_num);
	exit 0;
    }

         
$pictnum++;
}

