#!/usr/bin/perl

#@filenames=`ls -1 *gif`;
#chomp @filenames;
#
#$numoffiles=@filenames;
#$i=0;
#while ($i<$numoffiles ) {
#    $from=$filenames[$i];
#    $to=$filenames[$i];
#    $to =~ s/9798_//;
#    print ($from, "   ", $to, "\n");
#    `mv $from $to`;
#    $i++;
#}


# select each file
# Deal only with the "table" line
# separate the line into rows
# separate the rows into fields
# change the data in each field

#system ("mkdir output") if (!-d "output");

my @filenames = `ls -1 *.src ` ;
#my @filenames = (<test.src>);
chomp @filenames;


foreach $_ ( sort @filenames) {
    print "\n";
    print ;
    $outputfile= $_;
    $outputfile =~ s/src/txt/;
    #print $outputfile;
    #print "\n";
    open (FILE, "> $outputfile" );
    dealwithfiles($_);
    #print FILE "$outputfile";
    close (FILE);
}


sub dealwithfiles {
    my @filelines = `cat $_` ;	#$_ is the .src file
    my $filename = $_;
    $filename =~ s/\.src$//;
    #$filename =~ s/^srchtml\///;  # I don't think this is necessary

    #foreach $_ (@filelines) {	# do this for each line in the .src file
    foreach $line (@filelines) {	# do this for each line in the .src file
	if ( $line =~ m/pspwordsmaller.gif/ ) {
		$line =~ s/pspwordsmaller.gif/med\/$filename.gif/ ; 
		$line =~ s/http:\/\/www.powersportspro.com\//$filename.gif/;
	}


        if ( $line =~ m/<TABLE/ ) {
	    $line =~ s/.*<TABLE// ;
	    chomp $line;
#	    print FILE "it *DID* match TABLE";
#           @rows = split(/<TR>/);
            @rows = split /<TR>/, $line;
	    $numofrows = @rows;
	    $count=3;
	    while ( $count < $numofrows) {

            #foreach $row  (@rows) {	#do this for each row
	        $row = $rows[$count];	
		#if ($count>0) {  # I don't think this is necessary since I'm not writing html
		    #$row = "<TR>".$row;  
		#}
		if ( $row =~ m/ISBN/ ) {    # match ISBN 
		    #print FILE "<tr><td>$count<td>desc.<td>part num.";
#		    print FILE "\n";
#		    print FILE "\n";
#		    print FILE $row;
#		    print FILE "\n";
#		    print FILE "\n";
		    #($itemnumber, $description, $partnumber) =~ m/(.*<TD>)(.*<TD>)(.*)/, $row ;
		    ($tr, $itemnumber, $description, $partnumber) = split /<TD/, $row ;

		    $itemnumber =~ s/.*<A HREF.*?>//;
		    $itemnumber =~ s/<.*\/TD>//;
		    $description =~ s/.*<A HREF.*?>//;
		    $description =~ s/<.*<\/TD>//;
		    $price = $partnumber ;
                    $partnumber =~ s/.*<A HREF.*ISBN=//;
                    $partnumber =~ s/&.*<\/A>.*//;
		    
		    if ( $price =~ m/<\/TABLE/ ) {  # fix the last line 
			
		        $price =~ s/<\/TABLE>.*// ;
		    }
		    $price =~ s/.*>[^<]//;
		    $price =~ s/<.*//;

		    #print FILE $tr;
		    #print FILE "<TD";
		    print FILE $itemnumber;
		    print FILE "	";
		    #print FILE "<TD";
                    print FILE $description;
		    print FILE "	";
		    #print FILE "<TD";
                    print FILE $partnumber;
		    print FILE "	";
                    # print $partnumber;
		    print FILE $price ;
		    print FILE "	\n";
                } else {
                    #print FILE $row ; # print the rows that don't match
                    #print FILE "didn't match ISBN" ;
                }	# end of if m/ISBN
		#print FILE "-\n";
	    	#print FILE "<TR>";
	    $count ++;
	    }		# end of foreach rows

        } else {
            #print FILE $line;  # print the lines that don't match
            #print FILE "didn't match TABLE";
        }  # end of m/TABLE

    }	# end of foreach filelines
}	# endo of dealwith files

