#!/usr/bin/perl  -w

my $title="1988-91 Honda Hawk GT partsfiche" ;  #  this is the tile of the index page, 
my $tnperrow=4;   # thumbnails per row
my $tnsize=200;    # size of thumbnails
my $tnquality=25; # quality of thumbnails [0..100]
# use small thumbnails with poor quality to speed up your index-page

my $medsize=600;    # size of thumbnails
my $medquality=50; # quality of thumbnails [0..100]
# used to create medium sized pictures

$makeimages="0";	#set to 0 if you just want to remake the html or the index

# "calculate" how wide each index table entry should be
$tablewidth = 100/$tnperrow;

# create a directory for the thumbnails
system ("mkdir tn") if (!-d "tn");
system ("mkdir med") if (!-d "med");

my @pages = `cat index.list` ;
$outsideheader = `cat header.html`;

###  open the index and setup the head and stuff:

open ( INDEX, ">index.html");
print INDEX <<ENDOFZ ;

<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
$outsideheader
<table border=1 cellpadding=10 >


ENDOFZ

$colcounter=0 ;

foreach $pageinfo (@pages) {
	($imagename, $keywords)  = split /	/ , $pageinfo ;
	$basename = $imagename ;
	$basename =~ s/.gif$//;
	$prettyname = $basename ;
	$prettyname =~ s/_/ /g ;
	$srcname = $basename."\.src" ;	

	print ("working on ", $prettyname, ".... " );
	open (FILE, ">$basename.html" );

	print FILE <<ENDOFA ;

<html>
<head>
<title>$title</title>
<body>
<h2>$title</h2>
<center>
<h2>$prettyname</h2>
<a href="$imagename"><img src="med/$basename.jpg" ></a>
<p>
<table border=1 cellpadding=2 bgcolor="#FFDDDD">
<tr><th>no.<th>description<th>part number<th>typical price
ENDOFA

### this bit makes the main index entry

print INDEX "\n<tr valign=bottom>" if (!($colcounter++%$tnperrow));
  print INDEX <<ENDOFTD ;

<td width=$tablewidth%>
<center>
<a href="$basename.html"><img src="tn/$basename.jpg"></a>
<br>
<small><b>$keywords</b></small>
</center>

ENDOFTD



### this bit generates the data in the table, nothing else

@filelines = `cat $srcname` ;
foreach $line (@filelines) {	# do this for each line

    if ( $line =~ m/<TABLE/ ) {
	@rows = split /<TR>/, $line;
	$numofrows = @rows;
	$count=0;
	while ( $count < $numofrows) {
	    $row = $rows[$count];	
	    if ( $row =~ m/ISBN/ ) {    # match ISBN 
		($tr, $itemnumber, $description, $partnumber) = split /<TD/, $row ;
		$itemnumber =~ s/.*<A HREF.*?>//;
		$itemnumber =~ s/<.*//;
		#$itemnumber =~ s/<\/A>.*//;
		$description =~ s/.*<A HREF.*?>//;
		$description =~ s/<.*//;
		#$description =~ s/<\/A>.*//;
		$listprice = $partnumber;
                $partnumber =~ s/.*<A HREF.*ISBN=//;
                $partnumber =~ s/&.*<\/.*//;
                #$partnumber =~ s/&.*<\/.*//;
		$listprice =~ s/.*\$/\$/ ;
		$listprice =~ s/<.*// ;
		#$listprice =~ s/<\/A>.*// ;


		print FILE <<ENDOFB ;
<tr><td align=center>$itemnumber
    <td>$description
    <td>&nbsp;&nbsp;$partnumber
    <td align=right>$listprice
ENDOFB
		
	    }		# end of foreach rows, match ISBN
	    $count ++;
	}		# end of while (count < numofrowls)
    }  # end of "if m/TABLE
}	# end of foreach line	

####  this ends the table data part

#  now close the table

print FILE "</table></center>";

if ($makeimages eq "1") {

    pix2tn ($imagename);
}

print ("done.\n");

close FILE  ;

}  # end of foreach line in index.list

print INDEX "</table>\n</body></html>" ;
close INDEX ;



sub pix2tn {

  system ("convert -geometry ".$tnsize."x".$tnsize.
      " -quality $tnquality $imagename tn/$basename.jpg") == 0 
      || die "Problems with convert: $?\n";


  system ("convert -geometry ".$medsize."x".$medsize.
      " -quality $medquality $imagename med/$basename.jpg") == 0
      || die "Problems with convert: $?\n";


}	#  This is the end of Pix2tn.


