#!/usr/bin/perl

use File::Copy;

$boarddir = "/home/kirkjerk/SITES/MAIN/loveblender.com/board";

#script prints out n days worth of comments

print "Content-type: text/html\n\n" ;
&ReadParse(*input);

$month = $input{"month"};
$year=$input{"year"};


print<<__EOQ__;
<html>
<head>
<title>Love Blender Comments</title>
</head>
<FORM ACTION="comments.cgi">
<BODY
 BGCOLOR="#FFFFFF"  TEXT="#000000"
    LINK="#000088" VLINK="#888888" ALINK="#FF0000" >
<FONT SIZE=6><B>
<img src = "../blimage/blenderi.gif" border = 0 alt = "*" width=32 height=32>     
The Blender Board</B></FONT>
<HR>
__EOQ__

if(length($month) == 1) {
    $month = "0".$month;
}



opendir(DIR,"$boarddir/$year/$month") or print "can't open $dirname";
while(defined($file= readdir(DIR))) {

    if($file ne "." && $file ne ".."){
	$dayon{$file} .= "on";
    }
}
closedir(DIR);


foreach $mday (sort(keys(%dayon))) {
    printComments($year,$month,$mday);
}



sub printComments {
    my($showyear,$showmonth,$showmday) = @_;
    if(-e "$boarddir/$showyear/$showmonth/$showmday") {
	open(READ, "$boarddir/$showyear/$showmonth/$showmday") || print "Could not open $boarddir/$showyear/$showmonth/$showmday: $!";
	while(defined($nextline=<READ>)){
	    print $nextline;
	}
	close READ;
    }

}


print<<__EOQ__;

</body>
</html>


__EOQ__





sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);
  # Read in text
  if (&MethGet) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }
  @in = split(/&/,$in);
  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;
    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;
    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }
  return length($in); 
}
# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document
sub PrintHeader {
  return "Content-type: text/html\n\n";
}
# MethGet
# Return true if this cgi call was using the GET request, false otherwise
sub MethGet {
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}
# MyURL
# Returns a URL to the script
sub MyURL  {
  return  'http://' . $ENV{'SERVER_NAME'} .  $ENV{'SCRIPT_NAME'};
}
# CgiError
# Prints out an error message which which containes appropriate headers,
# markup, etcetera.
# Parameters:
#  If no parameters, gives a generic error message
#  Otherwise, the first parameter will be the title and the rest will 
#  be given as different paragraphs of the body
sub CgiError {
  local (@msg) = @_;
  local ($i,$name);
  if (!@msg) {
    $name = &MyURL;
    @msg = ("Error: script $name encountered fatal error");
  };
  print &PrintHeader;
  print "<html><head><title>$msg[0]</title></head>\n";
  print "<body><h1>$msg[0]</h1>\n";
  foreach $i (1 .. $#msg) {
    print "<p>$msg[$i]</p>\n";
  }
  print "</body></html>\n";
}


