#!/usr/bin/perl
#
# Erzeugt ein Kalenderblatt fuer einen Monat
# entweder als CGI-Reply auf STDOUT oder als HTML-Datei
# falls eine Datei als Parameter angegeben wurde.
#
my @months = ();
# "$start" is the day of the week on which the first of the month falls:
# 0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday
my @start = ();
# "$end" keeps the last day of the month
my @end = ();

$year='2002';
$months[1]='January';   $start[1]=weekday(1,1,$year);   $end[1]=31;
$months[2]='February';  $start[2]=weekday(1,2,$year);   $end[2]=28;
$months[3]='March';     $start[3]=weekday(1,3,$year);   $end[3]=31;
$months[4]='April';     $start[4]=weekday(1,4,$year);   $end[4]=30;
$months[5]='May';       $start[5]=weekday(1,5,$year);   $end[5]=31;
$months[6]='June';      $start[6]=weekday(1,6,$year);   $end[6]=30;
$months[7]='July';      $start[7]=weekday(1,7,$year);   $end[7]=31;
$months[8]='August';    $start[8]=weekday(1,8,$year);   $end[8]=31;
$months[9]='September'; $start[9]=weekday(1,9,$year);   $end[9]=30;
$months[10]='October';  $start[10]=weekday(1,10,$year); $end[10]=31;
$months[11]='November'; $start[11]=weekday(1,11,$year); $end[11]=30;
$months[12]='December'; $start[12]=weekday(1,12,$year); $end[12]=31;
$end[2]=29 if ((($year%4 == 0) and ($year%100 != 0)) or ($year%400 == 0));

&calendar(0,$year,$ARGV[0]); # 0 = current month

# Use the following code to generate the calendar for an entire year
# "index.html" will be the current month

# foreach $month (0..12) 
#   {
#   &calendar($month,$year,OUTFILE);
#   }

sub calendar #(Month, Year, Output_File) 
  {
  my($month,$year,$f) = @_;
  my($file);
  my($day,$day1,$day2,$day3,$day4,$day5,$day6,$day7);
  if ($month == 0) 
    {
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
    $secs = time();
    $secs -= ($mday-1)*24*60*60;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($secs);
    $year += 1900;
    $month = $mon+1;
    $file = 'kalender-index.html';
    $day = 1-$wday;
    } 
  else 
    {
    $file = lc($months[$month]).'.html';
    $day = 1-$start[$month];
    }
  if ($f eq '') 
    { $f = 'STDOUT'; } 
  else 
    { open($f,'>'.$file) || die('Unable to open '.$file); }
  print $f "Content-type: text/html\n\n" if ($f == 'STDOUT');
  print $f "<HTML><HEAD><TITLE> $months[$month] $year </TITLE></HEAD>\n";
  print $f "<BODY BGCOLOR='#FFFFFF' TEXT='#000000'>\n";
  print $f "<CENTER><TABLE BORDER=2 WIDTH='98%' CELLSPACING=0 CELLPADDING=8>\n";
  print $f "<TR><TH COLSPAN=7> $months[$month] $year</TH></TR>\n";
  print $f "<TR><TH WIDTH='14%'>Sunday</TH><TH WIDTH='14%'>Monday</TH>";
  print $f "<TH WIDTH='14%'>Tuesday</TH><TH WIDTH='14%'>Wednesday</TH>";
  print $f "<TH WIDTH='14%'>Thursday</TH><TH WIDTH='14%'>Friday</TH>";
  print $f "<TH WIDTH='14%'>Saturday\n";
  while ($day <= $end[$month]) 
    {
    if ($day>0 && $day<=$end[$month]) {$day1=$day;} else {$day1='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day2=$day;} else {$day2='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day3=$day;} else {$day3='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day4=$day;} else {$day4='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day5=$day;} else {$day5='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day6=$day;} else {$day6='&nbsp;';} $day++;
    if ($day>0 && $day<=$end[$month]) {$day7=$day;} else {$day7='&nbsp;';} $day++;
    if ($day1 ne '') {$day1.='<BR><BR><BR>';} else {$day7.='<BR><BR><BR>';}
    print $f "<TR VALIGN=TOP><TD ALIGN=RIGHT>$day1</TD><TD ALIGN=RIGHT>$day2</TD><TD ALIGN=RIGHT>$day3</TD><TD ALIGN=RIGHT>$day4</TD><TD ALIGN=RIGHT>$day5</TD><TD ALIGN=RIGHT>$day6</TD><TD ALIGN=RIGHT>$day7</TD>\n";
    }
  print $f "</TABLE></CENTER>\n";
  print $f "</BODY></HTML>\n";
  close($f);
  }

sub weekday
  {
  # Set $d to date (1-31), $m to month (1-12), 
  #  $y to year (yyyy)
  my($d,$m,$y) = @_;
  @hd = (0,3,2,5,0,3,5,1,4,6,2,4);
  $y-- if $m < 3;
  my $day = ($y+int($y/4)-int($y/100)+int($y/400)+$hd[$m-1]+$d) % 7;
  return ($day);
  }

