#!/usr/bin/perl

#!C:\perl\bin\perl.exe

print "Content-type: text/html\n\n";

use Sys::Hostname;

my (@Monat)=(' ','J&auml;nner','Feber','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
my (@Month)=(' ','January','February','March','April','May','June','July','August','September','October','November','December');

my ($limit, $host, $DIR1, $DIR2, $DIR3, $DIR4);
$host = hostname();
if ($host eq "fptchlx03.tu-graz.ac.at" ) {
  $DIR1 = "/var/www/docs/qwd/";
  $DIR3 = "/qwd/";
  $DOMAIN1 = "http://www.qwd.biz";
  $DOMAIN2 = "http://www.qwd.biz";
} elsif ($host eq "qwd" ) {
  $DIR1 = "../qwd/";
  $DIR3 = "/qwd/";
  $DOMAIN1 = "";
  $DOMAIN2 = "../biz";
} else {
  $DIR1 = "../";
  $DIR3 = "/";
  $DOMAIN1 = "http://www.qwd.biz";
  $DOMAIN2 = "http://www.qwd.biz";
}

$length = $ENV{'CONTENT_LENGTH'};
if ($length ne "") {
  read (STDIN, $rawData, $length);  #read this many bytes from standard input
} else {
  $rawData = $ENV{'QUERY_STRING'};
}
#print "$rawData\n";
my ($ia,$ie);
$rawData =~ tr/+/ /;
@args=split(/&/,$rawData);
foreach $pair (@args) {
  ($name, $value) = split(/=/, $pair);
  $FORM{$name}=$value;
#print "$name=$value\n";
}

if ($FORM{'lang'} eq "") {$FORM{'lang'} = "de" ;}

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
$mon++;
$year = $year+1900;

if ($FORM{'lang'} eq "de") {
  open (LESEN,"<".$DIR1."extras.html") || die;
} else {
  open (LESEN,"<".$DIR1."addons.html") || die;
}
$limit=0;
while (defined($zeile = <LESEN>)) {
  $limit++;
  if (substr($zeile,0,30) eq '<script src="./kalender-a.js" ' 
     || $limit>10000) { last; }
  $zeile =~ s/="\.\//="$DIR3/g;
  $zeile =~ s?http://www.qwd.biz/cgi?$DOMAIN1/cgi?g;
  $zeile =~ s?http://www.qwd.biz/zaehler?$DOMAIN2/zaehler?g;
  $zeile =~ s?http://www.qwd.biz/counter?$DOMAIN2/counter?g;
  print $zeile;
}
#print "monat: $FORM{'monat'} jahr:$FORM{'jahr'}\n";
$FORM{'monat'}=$mon;
$FORM{'monat'}=1 if ($FORM{'monat'} <= 0);
$FORM{'monat'}=12 if ($FORM{'monat'} >= 13);
$FORM{'jahr'}=$year;
$FORM{'jahr'}=1901 if ($FORM{'jahr'} <= 1900);
$FORM{'jahr'}=2099 if ($FORM{'jahr'} >= 2100);

$m=24; 
$n=5; 
$a=$FORM{'jahr'} % 19; 
$b = $FORM{'jahr'} % 4; 
$c = $FORM{'jahr'} % 7;
$d = (19*$a+$m) % 30; $e = (2*$b+4*$c+6*$d+$n) % 7;
$o = 22+$d+$e;
if ($o == 57) {$o = 50};
if ($o == 56 && $d == 28 && $a > 10) {$o = 49};
#print "Ostern: $o -te Maerz\n";
$so = $o % 7;
#print "Erster So im Maerz: $so\n";
$so = $so + 31 + 28;
$schalt=0;
if ($FORM{'jahr'}%4 == 0) {$schalt=1};
$so=$so+$schalt;
#print "das ist der $so -te Tag im Jahr\n";
$so=$so%7;
#print "erster Soo im Jae: $so\n";
for ($i=1; $i<=366; $i++) {$tag[$i]=0};
$tag[1]=2;  # 1. jaenner
$tag[6]=2;  # dreikoenig
$o=$o+31+28;
$o=$o+$schalt;
$tag[$o+1]=2;   # ostermontag
$tag[$o+50]=2;  # pfingstmontag
$tag[$o+39]=2;  # christi himmelfahrt
$tag[$o+60]=2;  # fronleichnam
$mh = 31+28+31+30+1;
$mh=$mh+$schalt;
if ($FORM{'jahr'}>=1919) {$tag[$mh] = 2;}  #1.mai
$mh = 31+28+31+30+31+30+31+15;
$mh=$mh+$schalt;
$tag[$mh] = 2;  #maria himmerfahrt = 15.8.
$mh = 31+28+31+30+31+30+31+31+30+26;
$mh=$mh+$schalt;
if ($FORM{'jahr'}>=1966) {$tag[$mh] = 2;}  #nationalfeiertag = 26.10.
$mh = 31+28+31+30+31+30+31+31+30+31+1;
$mh=$mh+$schalt;
$tag[$mh] = 2;  #1. november
if ($FORM{'jahr'}>=1919 && $FORM{'jahr'} < 1934) {
  $mh = 31+28+31+30+31+30+31+31+30+31+12;
  $mh=$mh+$schalt;
  $tag[$mh] = 2;  #12. november
}
$mh = 31+28+31+30+31+30+31+31+30+31+30+8;
$mh=$mh+$schalt;
$tag[$mh] = 2;  #8. dezember
$tag[$mh+17] = 2;  #25. dezember
$tag[$mh+18] = 2;  #26. dezember
for ($i=$so; $i<=366; $i=$i+7) {$tag[$i]=1};

$ia=1; 
$ie=31; 
if ($FORM{'monat'}>1) {$ia=$ia+$ie};
if ($FORM{'monat'}>=2) {$ie=28; if ($FORM{'jahr'}%4 == 0) {$ie++} };
if ($FORM{'monat'}>2) {$ia=$ia+$ie; $ie=31};
if ($FORM{'monat'}>3) {$ia=$ia+$ie; $ie=30};
if ($FORM{'monat'}>4) {$ia=$ia+$ie; $ie=31};
if ($FORM{'monat'}>5) {$ia=$ia+$ie; $ie=30};
if ($FORM{'monat'}>6) {$ia=$ia+$ie; $ie=31};
if ($FORM{'monat'}>7) {$ia=$ia+$ie; $ie=31};
if ($FORM{'monat'}>8) {$ia=$ia+$ie; $ie=30};
if ($FORM{'monat'}>9) {$ia=$ia+$ie; $ie=31};
if ($FORM{'monat'}>10) {$ia=$ia+$ie; $ie=30};
if ($FORM{'monat'}>11) {$ia=$ia+$ie; $ie=31};
#print "aktueller Monat beginnt mit dem $ia -ten Tag im Jahr\n";
for ($i=$ia; $i<=366; $i++) { 
  if ($tag[$i]==1) { last;
#goto anfang
  }
}
#anfang: 
#print "Erster So im aktuellen Mo ist der $i -te Tag im Jahr\n";

print '<table align="right" border="0" cellpadding="0"><tr><td width="9">';
print '&nbsp;&nbsp;</td><td>';

#print '<table bgcolor="#eeeeee" border="1" cellpadding="0"><tr><td>',"\n";
print '<table border="1" cellspacing="0"><tr><td>';
print '<table background="',$DIR3,'designc.png" cellspacing="0" ';
print 'border="0" cellpadding="5">',"\n";
print '<tr><th colspan="7">';
if ($FORM{'lang'} eq "de") {
  print "$Monat[$FORM{'monat'}] $FORM{'jahr'}</th></tr>\n";
} else {
  print "$Month[$FORM{'monat'}] $FORM{'jahr'}</th></tr>\n";
}
print '<tr>',"\n";
if ($FORM{'lang'} eq "de") {
  print '<th class="wotag">Mo</th>',"\n";
  print '<th class="wotag">Di</th>',"\n";
  print '<th class="wotag">Mi</th>',"\n";
  print '<th class="wotag">Do</th>',"\n";
  print '<th class="wotag">Fr</th>',"\n";
  print '<th class="wotag">Sa</th>',"\n";
  print '<th class="wotag"><font color="#cc0000">';
  print '<strong>So</strong></font></th></tr>',"\n";
} else {
  print '<th class="wotag">Mo</th>',"\n";
  print '<th class="wotag">Tu</th>',"\n";
  print '<th class="wotag">We</th>',"\n";
  print '<th class="wotag">Th</th>',"\n";
  print '<th class="wotag">Fr</th>',"\n";
  print '<th class="wotag">Sa</th>',"\n";
  print '<th class="wotag"><font color="#cc0000">';
  print '<strong>So</strong></font></th></tr>',"\n";
}
for ($j=$i-6; $j<=367; $j=$j+7) {
  print "<tr>\n";
  for ($jj=$j; $jj<=$j+6; $jj++) {
    $jjj=$jj-$ia+1;
    print '<td align="center">';
    if ($jjj<1 || $jjj>$ie) {
      print "&nbsp;";
    } else {
      if ($tag[$jj]>0) {print '<font color="#cc0000">';};
      if ($FORM{'monat'} == $mon && $FORM{'jahr'} == $year && $mday == $jjj) {
        print "<b>$jjj</b>";
      } else {
        print $jjj;
      }
      if ($tag[$jj]>0) {print '</font>';};
    }
    print "</td>";
  }
  print "</tr>\n";
  if ($jjj>=$ie) {last;
#goto ende
  }
}
#ende:
#print "</table>\n";
print "</td></tr></table>\n";
print "</td></tr></table><br />&nbsp;\n";
print "</td></tr></table>\n";

$zeile = <LESEN>;
$limit=0;
while (defined($zeile = <LESEN>)) {
  $limit++;
  if (substr($zeile,0,31) eq '<script src="./randomimage.js" '
     || $limit>10000) { last; }
  $zeile =~ s?http://www.qwd.biz/cgi?$DOMAIN1/cgi?g;
  $zeile =~ s/="\.\//="$DIR3/g;
  print $zeile;
}
print '<table align="right" border="0"><tr><td width="9">&nbsp;</td><td>';
print '<table align="right" border="1"><tr>';
print '<td align="center" bgcolor="white">';
print '<img width="296" height="296" alt="" src="';
$mmax=1;
if (-e $DIR1.'random_image/0001.jpg') {$JPG="jpg";} else {$JPG="JPG";}
if (-e $DIR1.'random_image/0159.'.$JPG) {$mmax=2;}
if (-e $DIR1.'random_image/0259.'.$JPG) {$mmax=3;}
if (-e $DIR1.'random_image/0359.'.$JPG) {$mmax=4;}
if (-e $DIR1.'random_image/0459.'.$JPG) {$mmax=5;}
if (-e $DIR1.'random_image/0559.'.$JPG) {$mmax=6;}
if (-e $DIR1.'random_image/0659.'.$JPG) {$mmax=7;}
if (-e $DIR1.'random_image/0759.'.$JPG) {$mmax=8;}
if (-e $DIR1.'random_image/0859.'.$JPG) {$mmax=9;}
if (-e $DIR1.'random_image/0959.'.$JPG) {$mmax=10;}
  if ($sec <=9) {$sec="0".$sec;}
  $m = $min%$mmax;
  if ($m <=9) {$m="0".$m;}
  print $DIR3,'random_image/';
  print "$m$sec";
  print '.'.$JPG;
print '" border="2" /><br />';
print '&copy; 2002-',$year,' Dr. Michael Ramek.</td></tr></table>';
print '</td></tr></table>';

$zeile = <LESEN>;
while (defined($zeile = <LESEN>)) {
  $zeile =~ s/="\.\//="$DIR3/g;
  print $zeile;
}
exit;

