#!/usr/bin/perl -w

# include perl packages
use strict;
use warnings;
use diagnostics;

use Tk;
use Tk::FileSelect;
use File::Basename;

my $nr = @ARGV;

if ($nr != 3) {
  print "Error: $0 called with wrong number of arguments (should be 3).\n";
  usage();
  exit();
}

my $red   = "";
my $green = "";
my $blue  = "";

foreach (0 .. $nr-1) {
	my $pic  = $ARGV[$_];
	if ($pic =~ m/.*red.*/i)   { $red   = $pic; } 
	if ($pic =~ m/.*green.*/i) { $green = $pic; } 
	if ($pic =~ m/.*blue.*/i)  { $blue  = $pic; } 
  }

if ($red eq "") {
  print "$0 called with no red channel!\n";
  usage();
  exit();
}
if ($green eq "") {
  print "$0 called with no green channel!\n";
  usage();
  exit();
}
if ($blue eq "") {
  print "$0 called with no blue channel!\n";
  usage();
  exit();
}

my $dir = dirname($red);
if (!-d $dir) {
  print "$dir is no valid directory\n";
  usage();
  exit();
}

my $top = MainWindow->new;
$top->title("MaPiVi Plugin RGB Seperator");

$top->Label(-text => "Create a",
		   )->pack();
$top->Button(-text => "RGB picture",
			 -command => sub { export(); })->pack();
$top->Label(-text => "picture of the three selected channel pictures",
		   )->pack();

$top->Button(-text => "exit plugin", -command => \&exit)->pack();

$top->MainLoop;


sub export {

  my $rgb   = $red;
  $rgb   =~ s/(.*)(red)(\.jp(g|eg))/$1RGB$3/i;
  print "rgb outoput file = $rgb\n";
  if (-f "$rgb") {
	my $rc = $top->messageBox(-icon  => 'warning', -message => "output file $rgb exist. Ok to overwrite?",
							  -title => "Plugin",   -type    => "OKCancel");
	return if ($rc !~ m/Ok/i);
  }

  if (-f "$rgb.ppm") {
	my $rc = $top->messageBox(-icon  => 'warning', -message => "temp file $rgb.ppm exist. Ok to overwrite?",
							  -title => "Plugin",   -type    => "OKCancel");
	return if ($rc !~ m/Ok/i);
  }

  my $command = "composite -compose copyred  $red  $green $rgb.ppm";
  (system "$command") == 0 or print "Error: $command failed: $!\n";
  
  $command = "composite -quality 95 -compose copyblue $blue $rgb.ppm $rgb";
  (system "$command") == 0 or print "Error: $command failed: $!\n";

  if (-f "$rgb.ppm") {
	if ( unlink("$rgb.ppm") != 1) { # unlink returns the number of successfull removed files
	  $top->messageBox(-icon => 'warning', -message => "Could not delete file \"$rgb.ppm\": $!",
					   -title   => "Error", -type => "OK");
	  print "Plugin $0 finished with errors!\n";
	  exit();
	}
  }

  print "Plugin $0 finished successfully!\n";
  exit();
}

##############################################################
# usage
##############################################################
sub usage {
  my $prog = basename($0);
  print "\nUsage: $prog file1-Red.jpg file2-Green.jpg file3-Blue.jpg\n\n";
  print "This is a plugin for mapivi (see http://mapivi.de.vu)\n";
  print "It will join the three selected RGB channel pictures from mapivi\n";
  print "into one RGB picture\n";
  print "The file names must include the corresponding color\n";
  print "(red, green, blue; in upper or lower case)\n";
  print "Author:  Martin Herrmann <martin-herrmann\@gmx.de>\n";
  print "License: GNU General Public License, version 2\n";
}


# Tell Emacs that this is really a perl script
# Local Variables:
# mode:perl
# End:
