# Add existing html page with thumbnail

$webdir = "c:/home/web";
$htmlname = shift;
$thumbname = shift;

if (!$thumbname) { $thumbname = "${htmlname}_t"; }

if (!$htmlname) {
  print "usage: makepage htmlname [thumbname]\n";
  exit 0;
}

if ( -e "$webdir/html/${htmlname}.htm" ) {
    $htmlname = "${htmlname}.htm";
} elsif ( -e "$webdir/html/${htmlname}.html" ) {
    $htmlname = "${htmlname}.html";
}

sub findthumbnail {
    if ( -e "$webdir/images/${thumbname}_t.jpg" ) {
	$thumbname = "${thumbname}_t.jpg";
    } elsif ( -e "$webdir/images/${thumbname}.jpg" ) {
	$thumbname = "${thumbname}.jpg";
    } elsif ( -e "$webdir/images/${thumbname}" ) {
	$thumbname = "${thumbname}";
    }
}


sub addthumbnail {
    findthumbnail();
    $curpictures = "$webdir/html/curpictures.html";
    $info = `identify $webdir/images/${thumbname}`;
    $_ = $info;
    ($w,$h) = /\s(\d+)x(\d+)\s/;
    
    open(TMPHTML, ">tmp$$");
    open(PICSHTML, $curpictures);
    $searching = 1;
    while($searching) {
	$_ = <PICSHTML>;
	if (m%</P>%) {
	    $searching = 0;
	    print TMPHTML;
	print TMPHTML <<EOL;
<P><a href="../html/$htmlname" target="main"><img src="../images/${thumbname}" width="$w" height="$h" border="1"></a></P>
EOL
	    break;
	}
	else { print TMPHTML; } 
    }
    
    while(<PICSHTML>) { print TMPHTML; }
    close(PICSHTML);
    close(TMPHTML);

    rename "tmp$$", $curpictures;
}

sub makeindex {
    open(TMPHTML, ">tmp$$");
    print TMPHTML <<EOP;
<HTML>
<HEAD>
  <META NAME="GENERATOR" CONTENT="Perl Script">
  <TITLE>Rachel and Andrew's Home Page</TITLE>
</HEAD>
<FRAMESET FRAMEBORDER=0 BORDER=0 FRAMESPACING=0 ROWS="89,83%">
<FRAME SRC="album.htm" NAME="FramePage1" MARGINWIDTH=3 MARGINHEIGHT=3 SCROLLING=NO>
<FRAMESET FRAMEBORDER=0 BORDER=0 FRAMESPACING=0 COLS="145,79%">
<FRAME SRC="curpictures.html" NAME="FramePage4">
<FRAME SRC="../html/${htmlname}" NAME="main">
</FRAMESET>
<NOFRAMES>
<BODY>
Viewing this page requires a browser capable of displaying frames.
</BODY>
</NOFRAMES>
</FRAMESET>
</HTML>
EOP

    close(TMPHTML);
    rename "tmp$$", "$webdir/html/index.html";
}

# Now do the work

addthumbnail();
makeindex();

chdir("$webdir/utils");
open(NEWFILES,">>newfiles.txt");
print NEWFILES "html/$htmlname\n";
print NEWFILES "images/${thumbname}\n";
print NEWFILES "html/curpictures.html\n";
print NEWFILES "html/index.html\n";

open(HTML, "$webdir/html/${htmlname}");
while(<HTML>) {
  if (/\/(\S+\/\S+\.jpg)/) { print NEWFILES "$1\n"; }
  if (/\/(\S+\/\S+\.gif)/) { print NEWFILES "$1\n"; }
}
close(HTML);

close(NEWFILES);
