#!/usr/local/bin/perl

##############User defined parametes  #################
#Use this  with the option -f i.e parse.pl -ffile_name
#
#To search and replace set $replace to 1 and give
# $str1 to be the string to be searched and $str2 to be the string to be 
#replaced

$replace =1;
$str1 = 'softlib.rice.edu';
$str2 = 'kayak.npac.syr.edu:7050';

#To log the details make $log =1 and give logfile name

$log =1;
$logfile="W.log";

#The temporary to which the output is to be written
#This will not work if you give the same filename 
#as the one you are reading from.

$writefile = 'tmp.file';


########################################################
$done =0;
$count = 0;
$replace_count = 0;
open(L,">".$logfile) || die "$0: Cannot log\n";
open(W,">".$writefile) || die "$0: Cannot write \n";
if ($replace && !(defined($str1)) || !(defined($str2))) {
    &log("replace strings not defined \n");
    close(L);
    close(W);
    exit(0);
}elsif ($replace == 0) {
   undef $str1;
   undef $str2;
}

if ($_ =$ARGV[0],/^-/) {
   shift;
   if(/^-f(\s*)?(.*)$/ && length($2)){ $file = $2};
   #print "###$file \n";
   if (!open(R,$file)) {
      print "\n####Cannot open $file ####\n";
      exit(0);
    }else {
      close(R);
    }
   
}else{
  print "\n#####Usage is parse.pl -ffile_name ####\n\n";
    close(L);
    close(W);
  exit(0);
}
      
if ($file=~m!\.mapa?$! && open(I,$file)) {
	&log("mapping $out\n");
	while ($line=<I>) {
	     next if $line=~/^#/;
             ($ref)=($line=~/^\s*\S+\s+(\S+)/);                 
             ($ref)=($line=~/\s(\S+)\s*$/) unless $ref=~/^\D/;  
	     &add_url($ref) if $ref;
        }
	close(I);
        $done =1;
}
if ($done==0 && open(I,$file)){
    $line="";
    while (<I>) {
	$line.=$_;
        $backline = $_;
	$line=~tr/\n\t\r/   /;
	while ($line=~/\<([^\>]*)\>(.*)$/) {	        
                $fulltag=$1;
	        $line=$2;
	        ($tag,%args)=&split_tag($fulltag);             
		&check_tag("A",       "HREF",       ) ||
		&check_tag("IMG",     "SRC",        ) ||
		&check_tag("IMG",     "LOWSRC",     ) ||
		&check_tag("IMG",     "DYNSRC",     ) ||
		&check_tag("FIG" ,    "SRC",        ) ||
		&check_tag("OVERLAY", "SRC",        ) ||
		&check_tag("BODY",    "BACKGROUND", ) ||
		&check_tag("TABLE",   "BACKGROUND", ) ||
		&check_tag("BGSOUND", "SRC",        ) ||
		&check_tag("FRAME",   "SRC",        ) ||
		&check_tag("AREA",    "HREF",       ) ||
		0;	         
       }
       printf W $backline;
    }
    close(W);
    close(I);
} 

while ($file=shift(@files)) {
    print "##$file ## \n";
 }
print "Total URL's Found = $count \n";
print "Total string's replaced = $replace_count\n" if $replace;
&log("Total URL's Found = $count \n");
&log("Total URL Replaced = $replace_count \n") if $replace;
&log("end\n");
close(L) if $log;
exit;

sub split_tag {
    local($fulltag)=@_;
   # print "The fulltag is $fulltag \n";
    local($tagname,$etc,$var,$quote,$arg,$value,@tagargs);
    undef $etc;
    ($tagname,$etc)=$fulltag=~/\s*(\S+)\s*(.*)$/;
    while ($etc!~/^\s*$/) {
	($arg,$var,$value,$etc)=$etc=~/\s*([^=\s]+)\s*(=)?\s*(\S+)?(.*)$/;
	($value,$etc)=((undef),$value.$etc) unless $var=~/^=$/;
	($quote)=$value=~/^([\"\'])/;
	if ($quote) {
	    if ($value!~/.$quote$/) {
		($_,$etc)=$etc=~/^([^$quote]*)$quote?(.*)$/;
		$value.=$_.$quote;
	    }
	    $value=~s/$quote\s*(.*)\s*$quote$/$1/;
	    $value=~s/\s*$//;
	}
	$arg="\U$arg\E";
	push(@tagargs,$arg,$value);
    }
    $tagname="\U$tagname\E";
    ($tagname,@tagargs);
}

sub check_tag {
    local($thetag,$thearg)=@_;
    if (($thetag eq $tag) && defined($args{$thearg})) {
       if ($replace) {
            $replace_count++ if ($backline =~ /$str1/);
       }
       $backline =~ s/$str1/$str2/g if $replace;
       &add_url($args{$thearg});
	1;
    } else {
	undef;
    }
}

sub add_url {
    local($ref)=@_;
    &log("reference $ref");
    if ($ref=~/^\s*$/) {
	&log("null reject-invalid\n");
	return;
    }
    	&log(" accept\n");
	push(@files,$ref);
    $count++;
}
sub log {
    local($message)=@_;
    print(L $message) if $log;
}
