#!/usr/bin/perl
# The following are constants you may wish to change
#
$jarfile='javadjvu.jar';
$codebase='/0/8/09/build/';
$code='DjVuApplet.class';
$IMAGE='http://javadjvu.foxtrottechnologies.com/djvu_badge-140x49.gif';
$MESSAGE='A Java Enabled browser is required to view this page.';

# The rest of this is the script.
# There should be no need to change anything below.
#
$basename=$ENV{SCRIPT_FILENAME};
$basename =~ s,^.*/,,g;

$uri=$ENV{REQUEST_URI};
$uri =~ s,^/,http://javadjvu.foxtrottechnologies.com/,;
$uri =~ s,^.*/$basename/,/,g;
$uri =~ s,[?].*,,;
$uri =~ s,^/*,/,;

$server=$ENV{SERVER_NAME};

# This section allow the applet to open the document directly, instead of 
# an HTML page.
if($ENV{HTTP_USER_AGENT} =~ /^Java/)
{
  $uri =~ s,[^/]*/[.][.],,g;
  print "Status: 302 Moved Temporarily\n";
  print "Location: http://$server/$uri\n\n";
  exit(0);
}

# foreach(split(/;/,$ENV{HTTP_COOKIE})){
#   my($key)=split(/=/,$_);
#   $cookies{$key}=substr($_,index($_,"=")+1);
# }

$param='<param name="image" value="'.$IMAGE.'">'
.'<param name="cache_archive" value="'.$jarfile.'">';
$opts = $ENV{REQUEST_URI};
$opts =~ s,^[^?]*[?]*,,;
$opts =~ s,[&]amp;,\&,g;
foreach(split(/[&]/,$opts)){
  my($key)=split(/=/,$_);
  my($value)=substr($_,index($_,"=")+1);
  $value =~ s,%2F,/,g;
  if($key eq "codebase")
  { 
    $codebase=$value;
  }
  elsif($key eq "code")
  {
    $code=$value;
  }
  elsif($key eq "data")
  {
    $uri=$value;
  }
  elsif($key eq "jarfile")
  {
    $jarfile=$value;
  }
  elsif(!($value eq ""))
  {
    $param.='<param name="'.$key.'" value="'.$value.'" >';
  }
#  else
#  {
#    $param.='<!-- param name="'.$key.'" value="'.$value.'" -->';
#  }
}
$param.='<param name="data" value="'.$uri.'">';

# Output the HTML page.
print <<END
Content-Type: text/html

<html>
<head>
<title>http://$server$uri</title>
</head>
<body scroll="no" bgcolor="#ffffff" style="margin:0px 0px 0px 0px;border:0px none">
<script language="JavaScript"><!--
var width=720, height=480;
if(window && window.innerWidth)
{
  width=window.innerWidth;
  height=window.innerHeight;
}
else if(document.body && document.body.clientWidth)
{
  width=document.body.clientWidth;
  height=document.body.clientHeight;
}
document.writeln('<applet ');
document.writeln('  style="border:0px none;margin:0px 0px 0px 0px;width:100%;height:100%;"');
document.writeln('  code="$code"');
document.writeln('  codebase="$codebase"');
document.writeln('  archive="$jarfile"');
document.writeln('  width="'+(width-12)+'"');
document.writeln('  height="'+(height-12)+'" >');
document.writeln('$param');
document.writeln('$MESSAGE');
document.write('</applet>');
// --> </script><noscript>
<applet 
  style="border:0px none;margin:0px 0px 0px 0px;width:100%;height:100%;"
  code="$code"
  codebase="$codebase"
  archive="$jarfile"
  width="720"
  height="480" >
$param
$MESSAGE
</applet></noscript></body></html>
END