#!/usr/bin/perl -w

use strict;
use IT;

IT::start("all");
my $file = "quotations.xml";
my $data;

if(open DATA, "<$file"){
   undef $/;
   $data = <DATA>;
   close DATA;
} else {
	print "content-type: text/html\n\n oops\n";
	exit 0;
}

my $param = $ENV{QUERY_STRING} || '';
my $n1  = "";
my $n2  = "";
if($param =~ /(\d+)-(\d+)/){
	$n1 = $1;
	$n2 = $2;
} else {
	if($param =~ /(\d+)/){
		$n1 = $1;
		$n2 = $1;
	}
}

my @data = ();
IT::start("parse data");

if(1){
	my @list = split /<quote>/, $data;
	foreach $_ (@list){
		s/<\/quote>.*//g;
		s/^\s+//g;
		s/\s+$//g;
		if($_ && $_ !~ /<pre>/){ # pre-formatierte texte passen einfach nicht rein
			push @data, $_;
		}		
	}
}

if(0){
	while($data =~ /<quote>(.*?)<\/quote>(.*)/is){
		my $quote = $1;
		$data = $2;
		
		push @data, $quote;
	}
}
IT::stop("parse data");

my $len = scalar @data;
my $quote = "";

if($n1 =~ /^\d+$/){
	if($n1 > $len){
		$n1 = $len -1 ;
	}
	if($n2 > $len){
		$n2 = $len -1 ;
	}
	foreach my $i ($n1 .. $n2){
		$quote .= any_one($i);
	}
} else {
	$quote .= any_one($n1);
}

sub print_wap_header {
	print "content-type: text/vnd.wap.wml\n\n";
}
sub print_html_header {
	print "content-type: text/html\n\n";
}
sub print_js_header {
	print "content-type: text/javascript\n\n";
}
print_js_header();

my $rand = random_number();

$quote .= "<br><a href='q-html.pl'>more quotes...</a>";


$quote =~ s/\n/\\n/sg;
$quote =~ s/<p>/<br><br>/sg;
$quote =~ s/"/&quot;/sg;

print <<__EOF__;
document.write("$quote");
__EOF__

sub random_number {
	my $x = int(rand() * $len);
	return $x;
}

sub any_one {
  IT::start("any_one");
	my ($n) = @_;
	my $cnt = 0;
  again:
	my $x = $n;
	if($n eq  ''){
		$x = int(rand() * $len);
		$n = '';
	} else {
		if($n !~ /^(\d+)$/){
			#
			# $n ist ein suchwort
			#
			$x = find_n($n);
		}
	}
	my $text = $data[$x];

	my $source= "";
	if($text =~ /(.*)<source>(.*)<\/source>/si){
		$text = $1;
		$source= $2;
	}
	my $text_comment   = "";
	my $source_comment = "";
	my $source_detail  = "";
	if($text =~ /(.*)<comment>(.*?)<\/comment>(.*)/si){
		$text = "$1\n$3";
		$text_comment = $2;
	}
	if($source =~ /(.*)<comment>(.*?)<\/comment>(.*)/si){
		$source = "$1\n$3";
		$source_comment = $2;
	}
	if($source =~ /(.*)<detail>(.*?)<\/detail>(.*)/si){
		$source = "$1\n$3";
		$source_detail = $2;
	}
	if($text =~ /<pre>(.*)<\/pre>/si){
		#
		# don't change the text
		#
	} else {
		$text =~ s/^\s+//g;			# remove unnecessary spaces at the start
		$text =~ s/\s+$//g;			# and at the end
		$text =~ s/<br>/\n\n/g;		# <br> => double newlines
		$text =~ s/<p.*?>/\n\n/g;	# <p> => double newlines
		$text =~ s/<.*?>//g;		# kill *all* html tags
		$text =~ s/ +/ /g;			# remove multi spaces
		$text =~ s/\n\n+/<br><br>/g;	# any sequence of newlines => 1 br
		$text =~ s/([\x80-\xff])/'&#' . ord($1) . ';'/eg; 	# escape dangerous characters
	}
	if(! $text){
		if($cnt ++ > 10){
			return "again and again and again there was no quote for you.";
		}		
		goto again;
	}	
	my $msg = "";
	$msg .= "Quote #$x<br><br>";
	$msg .= "$text<br><br>";
	if($source){
		$msg .= "<div class='source'>-- $source</div>\n";
	}
  IT::stop("any_one");
	return $msg;
}

sub find_n {
	my ($n) = @_;
  IT::start("find_n");
	my $i;
	my $x;

	foreach $x (@data){
		if($x =~ /$n/i){
		  IT::stop("find_n");
			return $i;
		}
		$i ++;
	}
  IT::stop("find_n");
	return 0;
}
IT::stop("all");

