#!/usr/bin/perl -w
#
# Distributed under GPL license.
#
# To run this script you need:
#
# - perl (available from CPAN, but preinstalled in most unix/linux systems,
#         you may find a site near you at http://www.perl.com/CPAN/ or 
#         http://www.cpan.org/)
#
# and  the following perl package (also available from CPAN)
# - libwww-perl
#
# which in turn requires other packages
# -  URI
# -  MIME-Base64
# -  HTML-Parser
# -  libnet
# -  Digest::MD5                                                                   
#
# HELP: Could someone doing a clean instalation please verify 
# that the above instructions are correct? 
# I think that some of these packages are not strictly necessary. Thanks!
#

########################################################## 
# These are the parameters that need to be configured

my $work_address = "http://localhost:2317/";
my $work_base = "base";
my $public_address = "http://localhost:2317/";
my $public_base = "base";

my @langs = ( "cn", "cs", "de", "dk", "en", "eo", "es", "fi", "fr", "he", "is", "it", "nl", "no", "pt", "ru", "se" );
# my @langs = ( "pt" );

# When the script is ok no further changes are be necessary

###########################################################                     

use HTTP::Request::Common;  
use LWP::UserAgent; 

###########################################################                     

sub check_dir ( $ ) {
  my ($d) = @_;
  if ( ! -e $d ) {
    mkdir ($d, 0777);
  } else {
    if (! ( -d $d)) {
      die "Error: $d exists, but is not a directory\n";
    }
  }
}
 
###########################################################                     
sub work {
  my $ua = LWP::UserAgent->new; 
  my $req = HTTP::Request->new(GET => $work_address);
  my $res;
  my $text;
  foreach $lang  ( @langs )  {
    check_dir ( $lang );
    check_dir ( $lang . "/names" );
    for ( $c = ord("A"); $c <= ord("Z"); $c = $c + 1 ) {  
      $char = chr($c);
      $file = $lang . "/names/" . $char . ".html";
      open (FILE, ">$file");
      $req->uri($work_address . $work_base . 
		"?lang=$lang;m=N;tri=S;o=A;k=$char");
      $res = $ua->request($req); 
      if (! $res->is_success) {
	die "Could not contact " . $work_address . $work_base .
	  "?lang=$lang;m=N;tri=S;o=A;k=$char\n" . 
	    "Check that gwd is running on that address\n";
      }   
      $text =  $res->content();

      #
      # The page is here.  Clean it up
      #
      # Change the name of the base
      # Is this correct? It might be convenient to include other characters 
      # so that family Albaser is not converted into family Alinstallr...
      #
      $text =~ s/$work_base/$public_base/mg;
      #
      # Instructions for robots
      #
      $text =~ s/content="NONE"/content="INDEX,NOFOLLOW"/;
      #
      # Links for the images
      # Is this a general case? What about the second alternative?
      # The question is (I think!) whether we use gwd or (eg) apache 
      # in the public address. Opinions?
      #
      # $text =~ s|geneweb\?m=IM;v=/|$public_address/images/|;
      #
      $text =~ s|geneweb\?m=IM;v=/|$public_address\?m=IM;v=/|ms;
      #
      # Base link for all other links
      #
      $text =~ 
	s|</head>|<base href=\"$public_address$public_base\"></head>|; # OK
      # 
      # If the base href is correctly defined above, this one 
      # is not necessary, or is it ...
      # -e "s'href=\"#'href=\"http://cristal.inria.fr/~ddr/gene/$f#'"
      # 
      #
      print FILE $text;
      close FILE;
    }
  }
}
###########################################################                     

work

