#!/usr/bin/perl -w

# 5.0 port. verio suckage? or ours? -gaal

use strict;

print_self() if ($ENV{QUERY_STRING}||'') =~ /source/;

use vars qw($HEIGHT $WIDTH $bg @colors @fonts @artists $styles $artisthtml);

$HEIGHT = 600;
$WIDTH = 1000; # not used directly; influences max font size fudge
$bg = '#667984';
@colors = qw(cf09a7 1fa9f8 e7e250 89ea79 e3bd6a 332f27 e8e3d7);
@fonts  = qw(Arial Courier_New Comic_Sans_MS Tahoma helvetica sans
		eraserdust figaro Times_New_Roman twentieth_century_poster1
		lucida_console georgia trebuchet_ms);
shuffle($_) for \(@colors, @fonts);
s/_/ /g for @fonts;
@artists = <DATA>;
shuffle(\@artists);
$#artists = 9;

for (0 .. $#artists) {
	gen_style($_);
	gen_artisthtml($_);
}

print << "EO_HTML";
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><title>Gaal's favorite music</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
a {
	text-decoration: none;
	border-bottom: 1px dotted #00F;
	color: inherit;
}
a.dummy {
	border:none;
}
a:hover {
	background-color: #132;
	position: relative;
	z-index:+1;
}
body { background: $bg }
div.cell {
	/* border: 1px solid #000; */
	padding-top: ${HEIGHT}px;
	/* width: ${WIDTH}px; */
	margin-left: 2em;
	margin-right: 2em;
	margin-bottom: 4em;
	margin-top: 5em;
	clear: both;
}
p { 
	white-space: nowrap;
	text-indent: 0;
	
}
p.greeting, div.footer {
	font: bold italic 14px Georgia, helvetica, sans;
	color: #e3bd6a;
}
div.footer {
	border-top: 1px dotted #132;
	position: absolute;
	padding-bottom: 2em;
	bottom: 0;
}
$styles
-->
</style>
</head>
<body>
<p class="greeting">Some of the music <a href="/gaal/">I</a> like, randomly generated.
<a href="javascript:window.location.reload()">Generate again</a>.</p>
<div class="cell">
&nbsp;
$artisthtml
</div>
<p/>
<div class="footer">
Copyright 2004 Gaal Yahas.
<a href="mymusic50?view=source">View source</a>; 
<a name="zap_style_sheets" href="javascript:(function(){var i,x;for(i=0;x=document.styleSheets[i];++i)x.disabled=true;})();">zap style</a>
</div>
</body></html>
EO_HTML

exit 0;
######################################################

sub print_self {
	seek DATA, 0, 0;
	read DATA, $_, 2<<20;
	s/&/&amp;/g;
	s/</&lt;/g;
	s/>/&gt;/g;
	print<< "END";
Content-type: text/html

<html><body>
<pre>$_</pre>
</body></html>
END
	exit 0;
}

sub shuffle { # Fisher-Yates, lifted from perlfaq4
	my $deck = shift;  # $deck is a reference to an array
	my $i = @$deck;
	while ($i--) {
		my $j = int rand ($i+1);
		@$deck[$i,$j] = @$deck[$j,$i];
	}
}

sub gen_style {
	my $id = shift;
	$styles .= "#art$id {\n";
	my $fn = join ", ", map { "'" . $fonts[ $_ % @fonts ] . "'" }
		$id, $id + 1, $id + 2;
	my $size = 7 + int rand($WIDTH / 12); # fudge obtained from experiment
	my $ital = rand > .5 ? "italic" : "";
	my $bold = rand > .5 ? "bold"   : "";
	my $len  = length $artists[$id]; # to avoid slipping out of the border
	(my $top = 30 + abs rand($HEIGHT) - $size) =~ s/(\..).*/$1/;
	(my $left = 10 + abs rand($WIDTH - $size) / $len * 4 ) =~ s/(\..).*/$1/;
	my $color = "#" . $colors[$id % @colors];
	$styles .= << "EO_CSS";
	font: $bold $ital ${size}px $fn;
	position: absolute;
	/* margin: ${top}px ${left}px; */
	top: ${top}px;
	left: ${left}px;
	/* top-margin: ${top}px !important;
	left-margin: ${left}px !important; */
	color: $color;
}
EO_CSS
}

sub gen_artisthtml {
	my $id = shift;
	$artisthtml .= << "EO_HTML";
<p id="art$id"><a class="dummy">$artists[$id]</a></p>
EO_HTML
}

__DATA__
Air
Amir Lev
Arik Einstein
Arik Einstein
Yitzhak Klepter
Atmosphera
Johan Sebastian Bach
The Beatles
Bill Evans
Billie Holiday
Bjork
Brad Mehldau
Brahms
Brian Eno
Camel
Can
Caravan
Carl Orff
Chick Corea
Chopin
Cocteau Twins
Corinne Allal
Cream
Crustation
Dana Berger
Dave Brubeck
David Bowie
Dead Can Dance
Deep Purple
dEUS
Dire Straits
The Doors
Eels
ELO
Emerson, Lake & Palmer
Enya
Eran Zur
Carmela Gross Wagner
Erik Satie
Everything but the girl
Fairouz
Faith No More
Genesis
Gentle Giant
George Winston
Gong
Goran Bregovic
Hatfield and the North
Hawkwind
Hooverphonic
Janovark
Jefferson Airplane
Jethro Tull
Jimi Hendrix
John Cale
John Coltrane
John Dowland
Joni Mitchell
Joshua Redman
Josie Katz & Shmulik Kraus
Kate Bush
Keith Jarrett
King Crimson
Kristin Hersh
Led Zeppelin
Leonard Cohen
Loreena McKennitt
Lou Reed
Love
Ludwig Van Beethoven
Massive Attack
Matti Caspi
Mazzy Star
Medeski Martin and wood
Mike Oldfield
Miles Davis
Modest Mussorgsky
Mose Allison
Mozart
Neil Young
Nick Drake
Nico
Nina Simone
Nirvana
Oregon
Oscar Peterson
Ossian
Ozric Tentacles
Pearl Jam
Peter Hammill
Pink Floyd
Portishead
Ravi Shankar
Red Hot Chili Peppers
Renaissance
Rick Wakeman
Ride
Rik Wright
Robert Fripp
Robert Wyatt
Runrig
Rush
Santana
Satin Whale
Shadowfax
Shalom Hanoch
Shimon Adaf
Shmulik Kraus
Sinead O'Connor
Smoke City
Soft Machine
Stacey Kent
Stanley Jordan
Stanley Turrentine
Steve Reich
Swingle Singers
Television
The Blue Aeroplanes
The Cranberries
The End of Violence
The Fireman
The Flying Pickets
Thelonious Monk
The Postal Service
The Rolling Stones
The Smashing Pumpkins
The Smiths
The The
The Who
Tom Lehrer
Tori Amos
Trainspotting
Tricky
Tsjaikovski
Tuxedomoon
U2
Uriah Heep
Van Der Graaf Generator
Van Morrison
Yehudith Ravitz
Yes
Zakir Hussain