#!/usr/bin/perl

=head1 AKR Digicam Image Utility

=item Written: 20020218

=back

=item Creator: Andrew Robertson

=back

=item Revised: 20020701

=back

=item License: GPL 2

=back

    Copyright (C) 2002 Andrew Robertson <critter_75075@yahoo.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Global Variables:

  %html_files	Files with full path, keyed by the file name prefix.
  %images	Hash (keys by file prefix) of image data.  References
		anonymous hashes.  Keyed by data type.  Keys of
		referenced hashes 'in_web', 'in_html', 'web_name',
		'web_full_path'.

=cut

#%web_images = the name of the file keyed by GTK ID
my $web_dir = '/home/httpd/html';
my $base_dir = '/home/andy/Projects/20020218.images';
my $image_dir = '/home/andy/palmpix/images';
my $notes_file = '/home/andy/.sfnotes';
my %web_images;
my %html_images;
my $selected;
my $notes1;
eval `cat $notes_file`;
use Gtk '-init';
use Data::Dumper;
my $window = new Gtk::Window ("toplevel");
$window->set_title("AKR: Digicam Image Utility");
my $body = new Gtk::Table(3, 10, 0);
my $image_dir_entry = new Gtk::Entry;
my $web_dir_entry = new Gtk::Entry;
my $note_entry = new Gtk::Entry;
my @array;
my @list_items;
my %html_files;
my %images;

=head2 sub insert_before_text

Description:

  subroutine insert_before_text takes a file.  Parses it for a string.  Inserts a text string before that string.  Then,
  saves the file.

=cut

sub insert_before_text {

	my ($html_file, $insert_point, $image_name) = @_;
	if (not(open(HTML, $html_files{$html_file}))) {
		debug_message("Unable to read file");
		return(undef);
	}
	my ($text, $line);
        while ($line = <HTML>) {
		$text .= $line;
	}
	close (HTML);
	if ($text =~ /^(.*)$insert_point(.*)$/s) {
		$text = $1 . "<A HREF=\"$images{$image_name}->{web_name}\">$$notes1{$image_name}</A><BR/>\n" . $insert_point . $2;
		if (open (HTML, "> $html_files{$html_file}")) {
			print HTML $text;
			close (HTML);
		} else {
			debug_message("Unable to open file for writing!");
			return (undef);
		}
	} else {
		debug_message("Unable to find insert point");
		return (undef);
	}
	return (1);

}

=head2 sub save_notes

Description:

  subroutine save_notes exports the $notes1 anonymous hash to the file specified by the $notes_file variable.  The $notes1 anonymous
  hash references strings describing files, keyed by file index.  That is, the name of the file without path and extension.

=cut

sub save_notes {
  open (NOTES, "> $notes_file");
  local $Data::Dumper::Purity = 1;
  local $Data::Dumper::Varname = 'notes';
  print NOTES Dumper($notes1);
  close (NOTES);
}

sub update_clist {
	my ($clist, $scrolled_win) = @_;
        my $adjustment = undef;
        if (defined($$scrolled_win)) {
          my $ca = $$scrolled_win->get_vadjustment;
          $adjustment = new Gtk::Adjustment($ca->get_value, $ca->lower, $ca->upper, $ca->step_increment, $ca->page_increment, $ca->page_size);
        }
	@array = keys(%{&get_palmpix_images});
	@list_items = sort @array;
        if (defined($$scrolled_win)) {$$scrolled_win->destroy;}
	if (defined($$clist)) {$$clist->destroy;}
	$$scrolled_win = new Gtk::ScrolledWindow(undef, $adjustment);
	$$scrolled_win->set_usize(250,200);
	$$scrolled_win->set_policy(-automatic, -automatic);
	$$clist = new Gtk::CList(4);
	$$clist->set_selection_mode(-multiple);
	$$clist->set_selection_mode(-browse);
	$$clist->set_row_height(20);
	$$clist->set_column_width(0,100);
	$$clist->set_column_width(1,100);
	$$clist->set_column_width(2,100);
	$$clist->set_column_width(3,100);

	$$clist->signal_connect('select_row', \&select_clist);
	%web_images = ();
	for($i=0;$i<@list_items;$i++) {
		my $temp_list_item = $list_items[$i];
		$temp_list_item =~ s/^(.+)\.(png|gif|bmp|jpg)$/$1/i;
		$web_images{$temp_list_item} = $list_items[$i];
	}
	my $web_items = get_web_images(keys(%web_images));
	my $html_items = get_html_images(keys(%web_images));
	my $key;
#	print Dumper($html_items);
	for($i=0;$i<@list_items;$i++) {
		my $temp_list_item = $list_items[$i];
		$temp_list_item =~ s/^(.+)\.(png|gif|bmp|jpg)$/$1/i;
	        my $text = '';
	        my $next_text = '';
	        if (defined($$web_items{$temp_list_item})) {
	        	$text = 'In Website';
		}
                if (defined($$html_items{$temp_list_item})) {
                        $next_text = 'In Html';
                }
                my $note_text = '';
                if (defined($$notes1{$temp_list_item})) {
                  $note_text = $$notes1{$temp_list_item};
                }
		$$clist->append($temp_list_item, $text, $next_text, $note_text);
	}
	$$scrolled_win->add_with_viewport($$clist);
	$body->attach_defaults($$scrolled_win, 0, 2, 3, 4);
	$window->show_all;
}

sub get_html_files {

	open (TEMP, 'find /home/httpd/html | grep "\.html$" | ');
	my $temp;
	while ($line = <TEMP>) {
		chomp($line);
		if ($line =~ /^[\/\w\d\_\.]*\/([^\/]+)$/) {
			$html_files{"$1"} = $line;
		} else {
			debug_message("get_html_files does not understand line \"$line\"");
		}
	}
	close (TEMP);

}

=head2 sub get_html_images

Description:

  subroutine get_html_images returns a hash keyed by all images directly linked to on
  the website, or used as an inline image, with the file extention and directory stripped off.

=cut

sub get_html_images {

  my @images = @_;
  my %temp_hash;
  open (TEMP, 'cat /home/httpd/html/*.html | grep -i "a href" |');
  my $line;
  while ($line = <TEMP>) {
    if ($line =~ /a\shref="?([\/\_\-\w\.\d]*\/)([\_\-\w\d]+)\.(png|gif|jpg)"?/i) {
	$temp_hash{$2} = 'defined';
	$images{$2}->{'in_html'} = 1;
    }
  }
  close (TEMP);
  open (TEMP, 'cat /home/httpd/html/*.html | grep -i "img src" |');
  while ($line = <TEMP>) {
    if ($line =~ /img\ssrc="?([\/\_\-\w\.\d]*\/)([\_\-\w\d]+)\.(png|gif|jpg)"?/i) {
	$temp_hash{$2} = 'defined';
	$images{$2}->{'in_html'} = 1;
    }
  }
  close (TEMP);
  return \%temp_hash;

}

sub get_web_images {
	my @images = @_;
	my @directories = ('/home/httpd/html', '/home/httpd/html/franklin');
	my %temp_hash;
	my $cat_lines;
	
        open (TEMP, '/usr/bin/find /home/httpd/html|');
	my $line;
        while ($line = <TEMP>) {
		if (($line !~ /\/\./) and ($line =~ /([^\/.]+)\.[\w\d\_\-]+/)) {
			chomp($line);
			my $name = $1;
			$images{$name}->{'web_full_path'} = $line;
			$cat_lines .= $line;
			my $s_temp = $line;
			$s_temp =~ s/^$web_dir(.*)/$1/;
			$s_temp =~ s/\/(.*)/$1/;
			$images{$name}->{'web_name'} = $s_temp;
		}
	}
        close (TEMP);
	my $item;
	foreach $item(@images) {
		if ($cat_lines =~ /$item/) {
			$temp_hash{$item} = 'defined';
			$images{$item}->{'in_web'} = 1;
		}
	}

	return \%temp_hash;
}

sub debug_message {

  my $message = shift;
  my $window = new Gtk::Window;
  my $button = new Gtk::Button("OK");
  my $body = new Gtk::VBox 0, 5;
  my $label = new Gtk::Label("$message");
  $body->pack_start($label, 0, 0, 0);
  $body->pack_start($button, 0, 0, 0);
  $window->add($body);
  $window->show_all;
  $button->signal_connect("clicked", sub {$window->destroy;});

}

sub select_clist {
	my($widget, $row, $column, $event) = @_;
	$selected = $widget->get_text($row, 0);
}

sub get_palmpix_images {
  my %temp_hash;
  my $line;
  $image_dir = $image_dir_entry->get_text;
  open (TEMP, "/bin/ls $image_dir |");
  while ($line = <TEMP>) {
    chomp($line);
    $temp_hash{$line} = 'defined';
  }
  close TEMP;
  return \%temp_hash;
}

my $clist;
my $scrolled_win;
my $preview_button = new Gtk::Button("Preview");
my $quit_button = new Gtk::Button("Quit");
my $gimp_button = new Gtk::Button("Gimp");
my $set_note_button = new Gtk::Button("Save Note");
my $refresh_button = new Gtk::Button("Refresh");
my $insert_html_button = new Gtk::Button("Insert Image into HTML");

$image_dir_entry->set_usize(400, 20);
my $image_dir_label = new Gtk::Label ("Image Directory");
$image_dir_entry->set_text($image_dir);
my $web_dir_label = new Gtk::Label ("Web Directory");
$web_dir_entry->set_text($web_dir);
my $note_label = new Gtk::Label ("Note");
my $file_label = new Gtk::Label ("HTML File");

get_html_files;

my $file_list = new Gtk::Combo;
$file_list->set_popdown_strings(sort {$a cmp $b} keys(%html_files));

$quit_button->signal_connect("clicked", sub {save_notes; Gtk->main_quit; exit(0);});

$refresh_button->signal_connect("clicked", sub {update_clist(\$clist,\$scrolled_win);});

$preview_button->signal_connect("clicked", sub {system("ee $image_dir/" . "$web_images{$selected}&");});

$gimp_button->signal_connect("clicked", sub {$ENV{'PWD'} = $web_dir; system("cd $web_dir; gimp-remote -n /home/andy/palmpix/images/$web_images{$selected}&");});

$set_note_button->signal_connect("clicked", sub {$$notes1{$selected} = $note_entry->get_text;});

$insert_html_button->signal_connect("clicked", sub {if (insert_before_text($file_list->entry->get_text, '<!--narf-->', $selected )) {} else {print "FAILURE!\n";}});

$window->signal_connect("delete_event", sub {save_notes; Gtk->main_quit; exit(0);});

$window->add($body);
$body->attach_defaults($image_dir_label, 0, 1, 0, 1);
$body->attach_defaults($image_dir_entry, 1, 2, 0, 1);
$body->attach_defaults($web_dir_label, 0, 1, 1, 2);
$body->attach_defaults($web_dir_entry, 1, 2, 1, 2);
$body->attach_defaults($note_label, 0, 1, 2, 3);
$body->attach_defaults($note_entry, 1, 2, 2, 3);
# 0, 2, 3, 4 is images clist
$body->attach_defaults($file_label, 0, 1, 4, 5);
$body->attach_defaults($file_list, 1, 2, 4, 5);
$body->attach_defaults($quit_button, 0, 2, 5, 6);
$body->attach_defaults($preview_button, 0, 2, 6, 7);
$body->attach_defaults($gimp_button, 0, 2, 7, 8);
$body->attach_defaults($refresh_button, 0, 2, 8, 9);
$body->attach_defaults($set_note_button, 0, 2, 9, 10);
$body->attach_defaults($insert_html_button, 0, 2, 10, 11);


update_clist(\$clist, \$scrolled_win);
$window->show_all;
$window->set_position("center");
print "$root\n";
Gtk->main;
