#!/usr/bin/perl

use File::Copy;

$politicsdir = "/home/kirkjerk/SITES/MAIN/loveblender.com/politics";
$surveyfile = "/home/kirkjerk/SITES/MAIN/DATA/data-loveblender/survey/current";

#script prints out n days worth of comments

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

#get number of days, assume 3

$dayseconds = 24 *60*60;
$daycount = $input{"daycount"};
if($daycount eq "") {
    $daycount = 3;
}


open(READ,$surveyfile);
$askid =  <READ>;
chomp $askid;
$ask = <READ>;
chomp $ask;
close READ;




print<<__EOQ__;
<html>
<head>
<title>Love Blender Soapbox Forum</title>
</head>

<BODY
 BGCOLOR="#FFFFFF"  TEXT="#000000"
    LINK="#000088" VLINK="#888888" ALINK="#FF0000" >

<table border=0 width="100%"><tr>
<td width="600">
<FONT SIZE=6><B>
<a href="/"><img src = "../blimage/blenderi.gif" border = 0 alt = "*" width=32 height=32></a>     
The Blender Soapbox Forum</B></FONT>
</td>


</table>

<hr>
__EOQ__
#    printDayInput();


#go back n-1 days, calculate date, then use that to get comments for that day

for($i = 0; $i < $daycount; $i++){
    $numdays = ($daycount - $i) - 1;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - ($dayseconds * $numdays));
$showmonth = $mon+1;
if(length($showmonth) == 1) {
    $showmonth = "0".$showmonth;
}
$showyear = $year + 1900;
$showmday = $mday;
if(length($showmday) == 1) {
    $showmday = "0".$showmday;
}

printComments($showyear,$showmonth,$showmday);


}

sub printDayInput {
    print <<__EOQ__;

<I><B>
<a href="/blend/politicsback.html">Post Comment to Soapbox Forum</a>
<br>

Show Last <INPUT TYPE="TEXT" SIZE=2 NAME="daycount" VALUE="$daycount"> Days of Messages

<INPUT  TYPE =SUBMIT VALUE="Go">


</B></I>
__EOQ__
}


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

}

print "<HR>";
print <<__EOQ__;
<FORM ACTION="politics.cgi">  
    <table width="100%" border=0>
<tr><td>
__EOQ__
printDayInput();  
print<<__EOQ__;







<td align="right">
<script language="JavaScript">
<!--
    google_ad_client = 'pub-8393964454751607';
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = '468x60_as';
// -->
</script>
<script language="JavaScript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>

</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";
}


