#!/usr/bin/perl
use warnings;
use strict;


use Data::Dumper;
use Getopt::Long;
use Pod::Usage;


use Tagyu::Search  '0.02'; # from http://upster.blogspot.com/2005/10/tagyu-perl-api.html -- version .02


=head1 NAME

fixfurl.pl   -  a Machete script to fix the furl submition form, and suggest tags/categories

=head1 SYNOPSIS

Add  a rule in  Pithhelmet for a wildcard match of 'http://www.furl.net/storeIt.jsp*',  attach this as a 
 machete  script.   

You'll probably want to edit you bookmarklet, to use this 'scrollbars=yes,width=475,height=675,left=75,top=20,status=no,resizable=yes'  as the last parameter to the window.out  call.


=head1 AUTHOR

Nathan McFarland  C<< <nmcfarl@cpan.org> >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2005, Nathan McFarland C<< <nmcfarl@cpan.org> >>. All rights reserved.

This is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.


=cut	







my $DEBUGGING;
my $help;
GetOptions("v" => \$DEBUGGING, "key=s","h" => \$help, );
pod2usage(-verbose => 2)  if ($help);






my ($first, $last, $count, $found, @tags);

while (<>) {
  
  if (!$found) {
    if ( $_ !~ m|\<select name="category"|) {
      $first .= $_;
    } else {
      $found++
    }
  } else {


    if ( /option/ ) {
      if (@tags) {
	$_ =~ s/selected="true"//;      ##  remove the ones furl selected
	for my $tag (@tags) {
	  if ($_ =~  /$tag/gi) {
	    $_ =~ s/\<option/<option selected="true"/;
	  }
              
	}
      }

      $count++
    }
    $last .= $_;
    
  }

  #####  tags
  if (m|<input type="text" name="title" value="(.*)" size="40" maxlength="150" />|) {
    my $stuff = $1;
    @tags = Tagyu::Search->SuggestTags($stuff,   timeout => 10 );
  }
  if (m|<input type="text" name="newCategory" value="" size="20" maxlength="250" />| &&  @tags ) {
    $last .= "<br><b>Suggested Tags</b>: @tags <br/>";
  }
  

}




print $first;
print qq{Suggested Tags: @tags <br/>}     if @tags;
print qq{<select name="category" multiple="true" size="25">\n  } ;
print $last;


exit;



__END__
