#!/usr/bin/perl

#versao 1.01 - 16 MAI 2008
#correcao dos links dos nomes das seq, caminho das images (gifs) e
#retirada de botões dos arquivos html gerados pelo InterPro
#nos diretorios /grupo*.dir/interpro_dir/*.html  

print "Enter the path images directory: ";

my $images_dir = <STDIN>;
chomp $images_dir;

my $dir_list1 = `ls`;

my @directories1 = split("\n",$dir_list1);

foreach my $dir1 (@directories1){
    
    if($dir1 =~ m/.*\.dir$/){

	my $dir_list2 = `ls $dir1`;

	my @directories2 = split("\n",$dir_list2);

	foreach my $dir2 (@directories2){

	    if($dir2 =~ m/interpro_dir$/){
		
		my $new_dir_images = $dir1 . "/" . $dir2 . "/images";

		#Created directory images
		if(not -d $new_dir_images){
		    !system("mkdir $new_dir_images")
			or die "Could not created directory $new_dir_images\n";
		}
		
                #copy files images_dir
		!system("cp $images_dir/* $new_dir_images")
		    or die "Could not moved $images_dir for $new_dir_images\n";
		
		my $interpro = $dir1 . "/" . $dir2;

		my $dir_list3 = `ls $interpro`;

		my @directories3 = split("\n",$dir_list3);

		foreach my $file (@directories3){

		    if($file =~ m/.*\.html$/){

			my $path = $dir1 . "/" . $dir2 . "/" . $file;
			
			open(HTMLFILE, "<$path") 
			    or die "Could not open file:$file for reading\n";
			
			my @all_lines = <HTMLFILE>;
			
			close(HTMLFILE);
			
			open(HTMLFILE, ">$path") 
			    or die "Could not open file:$file for writing\n";
			
			my $flag;
			$flag = 0;
			
			foreach my $lines (@all_lines){
			    
			    $lines =~ s/&nbsp;<b><a href=(\S+) target="(\S+)">//g;
			    
			    $lines =~ s/http:\/\/(\S+)\/images/images/g;
			    
			    if( $lines =~ m/<table border="0">/){
				$flag = 1;
			    }
			    
			    if( $lines =~ m/<\/table>/){
				$flag = 0;
			    }
			    
			    if( ! $flag ){
				print HTMLFILE "$lines";
			    }
			}
			
			close(HTMLFILE);
		    }
		}
	    }
	}
    }
}
