#!/usr/bin/perl -w

# $Id: index.cgi,v 1.9 1999/01/06 22:25:36 fn Exp $

use CGI;
use strict;
use integer;

srand(time ^ $$);

my $query = new CGI;
my $queryString = $query->query_string;

### DATA


# url hash.  key is url, value is description.
# The tilde in the description separates the title and the painter's name.
my %images =
    (
     "painting-images/prosperine.jpg"	=> "'Prosperine'~Dante Gabriel Rossetti",
     "painting-images/bower.jpg"	=> "'The Bower Meadow'~Dante Gabriel Rossetti",
     "painting-images/monna-vanna.jpg"	=> "'Monna Vanna'~Dante Gabriel Rossetti",
     "painting-images/beatrix.jpg"	=> "'Beata Beatrix'~Dante Gabriel Rossetti",
     "painting-images/ghirlandata.jpg"	=> "'La Ghirlandata'~Dante Gabriel Rossetti",
     "painting-images/wdmorg04.jpg"	=> "'Thistle Tiles'~William de Morgan",
     "painting-images/fdcrown.jpg"	=> "'The Two Crowns'~Sir Frank Bernard Dicksee",
     "painting-images/flmusic.jpg"	=> "'The Music Lesson'~Fredrick Lord Leighton",
     "painting-images/flnaus.jpg"	=> "'Nausicaa from Homer's Ulysses'~Fredrick Lord Leighton",
     "painting-images/flhoney.jpg"	=> "'The Painter's Honeymoon'~Fredrick Lord Leighton",
     "painting-images/flinvoc.jpg"	=> "'Invocation'~Fredrick Lord Leighton",
     "painting-images/asshad.jpg"	=> "'Love's Shadow'~Anthony Fredrick Augustus Sandys",
     "painting-images/fsmais.jpg"	=> "'Proud Maisie'~Anthony Fredrick Augustus Sandys",
     "painting-images/fsmari.jpg"	=> "'Proud Maisie'~Anthony Fredrick Augustus Sandys",
     "painting-images/jemophe.jpg"	=> "'Ophelia'~Sir John Everett Millais",
     "painting-images/ehnight.jpg"	=> "'Night with her Train of Stars'~Edward R Hughes",
     "painting-images/pcstorm.jpg"	=> "'The Storm'~Pierre-Auguste Cot",
     "painting-images/pcspri.jpg"	=> "'Springtime'~Pierre-Auguste Cot",
     "painting-images/cffog.jpg"	=> "'Wanderer above the Sea of Fog'~Caspar David Friedrich",
     "painting-images/cfsea.jpg"	=> "'Seascape in the Moonlight'~Caspar David Friedrich",
     "painting-images/jtlondo.jpg"	=> "'London Visitors'~James Tissot",
     "painting-images/whbrave.jpg"	=> "'Mon Brave'~John William Hennessy",
     "painting-images/edflora.jpg"	=> "'Flora, the Goddess of Blossoms and Flowers'~Evelyn De Morgan",
     "painting-images/jsbozz.jpg"	=> "'Portrait of Katharina Bozzaris'~Joseph Stieler",
     "painting-images/ampeach.jpg"	=> "'Sapphires'~Albert J. Moore",
     "painting-images/amread.jpg"	=> "'A Reader'~Albert J. Moore",
     "painting-images/fhkiss.jpg"	=> "'A Kiss'~Francesco Hayez",
     "painting-images/jcguin.jpg"	=> "'Queen Guinevere's Maying'~John Collier",
     "painting-images/mscompa.jpg"	=> "'Two's Company, Three's None'~Marcus Stone",
     "painting-images/gdastro.jpg"	=> "'Astronomer By Candle Light'~Gerrit Dou",
     "painting-images/water04.jpg"	=> "'Sleep and his Half-Brother Death'~John William Waterhouse",
     "painting-images/Rosset10.jpg"	=> "'Lillith'~Dante Gabriel Rossetti",
     "painting-images/Rosset11.jpg"	=> "'The Beloved'~Dante Gabriel Rossetti",
     "painting-images/Rosset15.jpg"	=> "'Veronica Veronese'~Dante Gabriel Rossetti",
     "painting-images/Rosset5.jpg"	=> "'Dante's Dream at the Time of the Death of Beatrice'~Dante Gabriel Rossetti",
     "painting-images/Rosset7.jpg"	=> "'Il Ramoscello'~Dante Gabriel Rossetti",
     "painting-images/Rosset8.jpg"	=> "'La Bella Mano'~Dante Gabriel Rossetti",
     "painting-images/LaBelleDameSansMerci.jpg" => "'La Belle Dame Sans Merci'~John William Waterhouse",
     "painting-images/Miranda_TheTempest.jpg" => "'The Tempest'~John William Waterhouse",
     "painting-images/burne28.jpg"	=> "'The Beguiling of Merlin'~Edward Burne-Jones",
     "painting-images/image008.jpg"	=> "'Prosperine'~Dante Gabriel Rossetti",
     "painting-images/image033.jpg"	=> "'Flight and Pursuit'~William Rimmer",
     "painting-images/Lddeath.gif"	=> "'Death'~Neil Gaiman",
     "painting-images/death_3.jpeg"	=> "'Death'~Neil Gaiman",
     "painting-images/Ldeath_1.gif"	=> "'Death'~Neil Gaiman",
     "painting-images/death2.gif"	=> "'Death'~Neil Gaiman"
     );

# these are the images on the front page.
# they are a subset of the above images.
my @frontImages =
    (
     "painting-images/image008.jpg",
     "painting-images/gdastro.jpg",
     "painting-images/cfsea.jpg",
     "painting-images/cffog.jpg",
     "painting-images/pcstorm.jpg",
     "painting-images/mscompa.jpg",
     "painting-images/flhoney.jpg",
     "painting-images/flinvoc.jpg",
     "painting-images/pcspri.jpg",
     "painting-images/Rosset11.jpg",
     "painting-images/image033.jpg",
     "painting-images/Lddeath.gif",
     "painting-images/Ldeath_1.gif",
     "painting-images/death2.gif",
     "painting-images/ehnight.jpg"
     );


# width/height hash.  key is url, value is width/height, separated via "x".
# there HAS to be a better way.
my %widthHeight =
    (
     "painting-images/prosperine.jpg"	=> "294x607",
     "painting-images/bower.jpg"	=> "340x448",
     "painting-images/monna-vanna.jpg"	=> "410x482",
     "painting-images/beatrix.jpg"	=> "320x416",
     "painting-images/ghirlandata.jpg"	=> "499x600",
     "painting-images/wdmorg04.jpg"	=> "495x508",
     "painting-images/fdcrown.jpg"	=> "366x468",
     "painting-images/flmusic.jpg"	=> "544x546",
     "painting-images/flnaus.jpg"	=> "289x671",
     "painting-images/flhoney.jpg"	=> "410x436",
     "painting-images/flinvoc.jpg"	=> "339x559",
     "painting-images/asshad.jpg"	=> "334x486",
     "painting-images/fsmais.jpg"	=> "159x209",
     "painting-images/fsmari.jpg"	=> "154x204",
     "painting-images/jemophe.jpg"	=> "573x365",
     "painting-images/ehnight.jpg"	=> "482x286",
     "painting-images/pcstorm.jpg"	=> "353x467",
     "painting-images/pcspri.jpg"	=> "328x450",
     "painting-images/cffog.jpg"	=> "400x555",
     "painting-images/cfsea.jpg"	=> "351x468",
     "painting-images/jtlondo.jpg"	=> "330x486",
     "painting-images/whbrave.jpg"	=> "400x556",
     "painting-images/edflora.jpg"	=> "323x737",
     "painting-images/jsbozz.jpg"	=> "294x376",
     "painting-images/ampeach.jpg"	=> "241x716",
     "painting-images/amread.jpg"	=> "173x485",
     "painting-images/fhkiss.jpg"	=> "403x563",
     "painting-images/jcguin.jpg"	=> "460x695",
     "painting-images/mscompa.jpg"	=> "502x306",
     "painting-images/gdastro.jpg"	=> "395x590",
     "painting-images/water04.jpg"	=> "753x600",
     "painting-images/Rosset10.jpg"	=> "582x600",
     "painting-images/Rosset11.jpg"	=> "507x600",
     "painting-images/Rosset15.jpg"	=> "454x600",
     "painting-images/Rosset5.jpg"	=> "800x533",
     "painting-images/Rosset7.jpg"	=> "454x600",
     "painting-images/Rosset8.jpg"	=> "479x600",
     "painting-images/LaBelleDameSansMerci.jpg" => "601x800",
     "painting-images/Miranda_TheTempest.jpg" => "800x555",
     "painting-images/jpg/burne28.jpg"	=> "455x800",
     "painting-images/image033.jpg"	=> "320x217",
     "painting-images/image008.jpg"	=> "168x365",
     "painting-images/Lddeath.gif" => "56x224",
     "painting-images/death_3.jpeg" => "301x554",
     "painting-images/Ldeath_1.gif" => "101x257",
     "painting-images/death2.gif"	=> "301x554"
     );

# make an array of urls for images.  we need to do this here to get the image description for the title.
# we use lots of variables.  fix me?
my @urls = keys %images;
my $numImages = @urls;
my $randChoice = rand $numImages;
my $numFrontImages = @frontImages;
my $randFrontChoice = rand ($numFrontImages + 1);

# page-specific info
my $title;
my $painter;
my $width;
my $height;
my $keywords;                   # for the meta tag.  generally the same as $title.

# lyrics stuff
# this is ugly.  fix me: find a better way of storing them.
my @lyrics =
    (
     "the <a href=\"http://home.earthlink.net/~athamay/sunshine.html\">loves</a> we worship,<br>\nthe ones that never last<br>\nwe are the world together,<br>\n until they sell out their half",
     "lies and fairytailes<br>\nnot much difference between the two<br>\neverything and anything, it'll come right back to <a href=\"http://www.indifferent.com/wfg/\">you</a>.\n",
     "considered writing novels<br>\ni thought hard about this <a href=\"http://www.brainwashed.com/lpd/\">career</a><br>\nsolved a dozen famous mysteries<br>\nbut still i can't be clear<br>\nif there is anybody up there<br>\nperhaps it's been a year",
     "pass the crystal spread the tarot<br>\nin <a href=\"http://www.cs.cf.ac.uk/Sisters.Of.Mercy/\">illusion</a> comfort lies<br>\nthe safest way the straight and narrow<br>\nno confusion no surprise",
     "everything will be all right<br>\neverything will turn out fine<br>\nsome nights i still can't sleep<br>\nand the <a href=\"http://www.cs.cf.ac.uk/Sisters.Of.Mercy/\">voices</a> pass with time",
     "don't need a cure<br>\ndon't need a cure<br>\ndon't need a cure<br>\nneed a final <a href=\"http://www.itis.com/murphy/\">solution</a>",
     "<a href=\"http://www.itis.com/murphy/\">intelligence</a> so cold<br>\na heart like glass<br>\nfrom the surface to deep<br>\nyou wave then you dive<br>\nlike the heart and the sun<br>\nyou wave and you dive<br>\nyou hold me so cold<br>\nyou wave and you dive",
     "are we <a href=\"http://www.waste.org/bauhaus/\">looked</a> at<br>\nare we set back?<br>\ncan we fake him<br>\nemulate him?",
     "some people live in hell<br>\nmany bastards succeed<br>\nbut i. i've learned nothing<br>\ni can't even elegantly bleed<br>\nout the poison blood of <a href=\"http://www.swans.pair.com/\">failure</a>\n",
     "but i don't care if you don't<br>\nand i don't feel if you don't<br>\nand i don't want it if you don't<br>\nand i won't say it<br>\nif you won't say it <a href=\"http://www.thecure.com/\">first</a>"
     );
my $numLyrics = @lyrics;


### CODE

# $seq is set to 'r' if we're viewing images randomly, otherwise a number.
my $seq = $query->param('seq');

# set the appropriate title.
if ($queryString =~ "seq") {
    if ($seq eq "r") {
	($title, $painter) = split(/~/, $images{$urls[$randChoice]});
	$keywords = $title;
    } elsif ($seq >= 0 || $seq < $numImages) {
	($title, $painter) = split(/~/, $images{$urls[$seq]});
	$keywords = $title;
    } else {
	$title = "Unknown image.";
	$keywords = "ignore";
    }
} elsif ($queryString =~ "home") {
    $title = "d i s i n t e g r a t i n g";
} elsif ($queryString =~ "software") {
    $title = "Software";
} else {
    print $query->redirect('?home');
    exit(0);
}

# display the page header
# $query->use_named_parameters(1);
print $query->header;
print $query->start_html(
                         'title' => $title,
                         'author' => q|Faried Nawaz <__undead@canvas-beauty.hungry.org>|,
                         'meta' => {
			     'Description' => 'disintegrating',
			     'Keywords' => 'oblivion',
			     'Author' => 'Faried Nawaz',
			     'ContactEmail' => q|__undead@canvas-beauty.hungry.org|,
			     'Generator' => q|Faried Nawaz (__undead@canvas-beauty.hungry.org)|},
                         'bgcolor' => '#000000',
                         'text' => '#ffffff',
                         'link' => '#a0a0a0',
                         'vlink' => '#808080'
                         );

# display the appropriate page body
if ($queryString =~ "seq") {
    if ($seq eq "r") {
	showRandomImage();
    } else {
	showImage();
    }
} elsif ($queryString =~ "software") {
    showSoftware();
} else {
    showHome();
}


# we display this at the bottom of every page.
sub showFooter {
    print "<hr>\n";
    print q|<table border="0" cellpadding="0" cellspacing="0" cols="2">| . "\n";
    print "<tr><td align=\"left\">\n";
    print "<p>\n";
    print q|<a href="mailto:fn-web@hungry.org"><address>faried.</address></a>| . "\n";
    print "</p>\n";
    print "</td>\n";
#     print "<td align=\"center\">\n";
#     print "<p>\n";
#     print "<a href=\"http://www.xemacs.org/\"><img src=\"Image3.gif\" border=\"0\" alt=\"Emacs now!\"></a>\n";
#     print "</p>\n";
#     print "</td>\n";
    print "<td align=\"right\">\n";
    print "<p>\n";
    print "<a href=\"http://www.xemacs.org/\"><img src=\"benEmacs.gif\" border=\"0\" alt=\"powered by Emacs\" width=100 height=30></a>\n";
    print "</p>\n";
    print "</td></tr></table>\n";
    
#  print "<p align=\"right\">\n";
#  print "<img src=\"self-image.png\" alt=\"an image of me.\" width=320 height=240>\n";
#  print "</p>\n";
    print $query->end_html;
    exit(0);
}

# display a random url.
sub showRandomImage {
    print "<p align=\"center\">\n";
    ($width, $height) = split(/x/, $widthHeight{$urls[$randChoice]});
    print "<a href=\"p.cgi?seq=r\"><img src=\"$urls[$randChoice]\" alt=\"$title by $painter\" border=0 width=$width height=$height></a>\n";
    print "</p>\n";
    print "<p align=\"center\">\n";
    print "$title by $painter\n";
    print "</p>\n";

    showFooter();
}

# display a specific url.
sub showImage {
    my $prevSeq;
    my $seqInc;
    my $count;

    if ($seq < 0 || $seq >= $numImages) {
	print "<h1 align=\"center\">Image not found</h1>\n";
	print "<p>\n";
	print "I'm sorry.  I'm not sure how we got here.  There is no image $seq.\n</p>\n";
	print "<p><a href=\"p.cgi?seq=0\"><em>Back</em></a></p>\n";
    } else {
	$seqInc = $seq + 1;
	if ($seqInc == 40) {
	    $seqInc = 0;
	}
	print "<p align=\"center\">\n";
	($width, $height) = split(/x/, $widthHeight{$urls[$seq]});
	print "<a href=\"p.cgi?seq=$seqInc\"><img src=\"$urls[$seq]\" alt=\"$title by $painter\" border=0 width=$width height=$height></a>\n";
	print "</p>\n";
	print "<p align=\"center\">\n";
	print "$title by $painter\n";
	print "</p>\n";

	print "<h4 align=\"center\">\n";
	if ($seq == 0) {
	    print "<em>Previous</em>&nbsp;&nbsp;";
	} else {
	    $prevSeq = $seq - 1;
	    print "<a href=\"p.cgi?seq=$prevSeq\"><em>Previous</em></a>&nbsp;&nbsp;";
	}
	if ($seq == ($numImages - 1)) {
	    print "<em>Next</em>&nbsp;&nbsp;";
	} else {
	    print "<a href=\"p.cgi?seq=$seqInc\"><em>Next</em></a>&nbsp;&nbsp;";
	}
	foreach $count (0..($numImages - 1)) {
	    my $tmp = $count + 1;
	    print "<a href=\"p.cgi?seq=$count\"><em>$tmp</em></a>&nbsp;&nbsp;";
	}
	print "\n</h4>\n";

	print "<p align=\"right\">\n";
	print "<a href=\"?home\">Home</a>\n";
	print "</p>\n";
    }
    showFooter();
}


# the main page.
sub showHome {
    print "\n\n<p align=\"center\">\n";
    if ($randFrontChoice == $numFrontImages) {
	print "<a href=\"http://www.brunching.com/toys/toy-tina.html\"><img src=\"http://www.brunching.com/images/tina-dark.gif\" border=0 alt=\"Tina the Troubled Teen\"></a>\n";
    } else {
	($title, $painter) = split(/~/, $images{$frontImages[$randFrontChoice]});
	($width, $height) = split(/x/, $widthHeight{$frontImages[$randFrontChoice]});
	print "<a href=\"?home\"><img src=\"$frontImages[$randFrontChoice]\" alt=\"$title by $painter\" border=0 width=$width height=$height></a>\n";
	print "</p>\n\n<p align=\"center\">\n";
	print "<em>Click on the image or hit reload to get another picture.</em><br>\n";
	print"<em><a href=\"p.cgi?seq=0\">Here</a> are some more images.\n";
    }
    print "</p>\n\n";
    print "<hr>\n";
    print q|<table border="0" cellpadding="0" cellspacing="0" cols="3">| . "\n";
    print q|<tr><td align="left" valign="top">| . "\n";
    print q|<p><strong>Resume: </strong>In <a href="Resume.html">HTML</a> and <a href="Resume.txt">text</a></p>| . "\n";
    print q|</td>| . "\n";
    print q|<td align="center">| . "\n";
    print q|<p><a href="http://dir.yahoo.com/Arts/Humanities/Literature/Genres/Literary_Fiction/Authors/Hemingway__Ernest__1899_1961_/"><img src="hem_happy.jpg" alt="Hemingway" border="0"></a></p>| . "\n";
    print q|</td>| . "\n";
    print q|<td align="right" valign="bottom">| . "\n";
    showRandomLyric();
#     print "<p><font size=+1>\n";
#     print "All good books are alike in that they are truer than if \n";
#     print "they really happened and after you are finished reading one \n";
#     print "you will feel that it all happened to you and afterwards it \n";
#     print "all belongs to you: the good and the bad, the ecstacy, the \n";
#     print "remorse and sorrow, the people and the places and how the \n";
#     print "weather was.  If you can get so that you can give that to \n";
#     print "people, then you are a writer.\n";
#     print "</font></p>\n";
    print q|</tr></table>| . "\n";

### NOTYET
#  print "<p>\n";
#  print "Some <a href=\"?software\">software</a> I've worked on.\n";
#  print "</p>\n";
#
    showFooter();
}


# a page describing software I've worked (?) on.
#sub showSoftware {
#  print "<h1 align=\"center\">Software</h1>\n";
#
#  showFooter();
#}
###

sub showRandomLyric {
    my $whichLyric = rand $numLyrics;

    print "<p><em>\n";
    print "$lyrics[$whichLyric]";
    print "</em></p>\n";
}


### eof.
