#!/usr/bin/perl

$currentmonth=substr(localtime(time),4,4).substr(localtime(time),20,4);

if (int(rand(10)) == 0) {


#open (INF,"datafiles/dlindex") or dienice("Can't open log file");
#$oldindex=<INF>;
#close (INF);



$newindex=int(rand(100));
#open (OUT,">datafiles/dlindex") or dienice("Can't open log file");
#print OUT "dlfiles$newindex";
#close (OUT);
#rename $oldindex, "dlfiles$newindex";

}
print "Content-type:text/html\n\n";
open(INF,"datafiles/template.htm") or dienice("Could not open menu template $!\n");
@data = <INF>;
close(INF);
print @data;

@values = split(/&/,$ENV{'QUERY_STRING'});
foreach $i (@values) { ($name, $value) = split(/=/,$i); 
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/\n/ /g; # replace newlines with spaces
    $value =~ s/\r//g;  # remove hard returns
    $value =~ s/\cM//g; # delete ^M's
    $FORM{$name} = $value;
  }

if ($FORM{p}  eq "" ) {
   $FORM{p}="home";
   }
$tod=localtime(time);
if (($ENV{'HTTP_REFERER'} ne "" ) && ($ENV{'HTTP_REFERER'} !~ /videohelp/g )) {
#open (OUTF, ">>datafiles/referrals") or dienice("Can't open log file");
#print OUTF $tod."|"."$ENV{'HTTP_REFERER'}\n";
#close (OUTF);
}

open (INF,"logs/$FORM{p}") or dienice("Can't open log file");
$size = <INF>;
close (INF);
$size++;
open (OUT,">logs/$FORM{p}") or dienice("Can't open log file");
print OUT $size;
close (OUT);

if ($FORM{p} ne "news") {
open(INF, "datafiles/$FORM{p}.htm") or dienice("Page $FORM{p}.htm not found $!\n");
@data = <INF>;
close(INF);
print @data;
} else {
print <<EndOfHTML;
    <td height="171" valign="top" width="600"><table width="100%" height="253" border="1" align="center" bordercolor="#666666">
        <tr>
          <td height="19"><div align="center"><font size="1" face="tahoma, arial">
           .:.Whats going on .:.</font></div></td>
        </tr>
        <tr>
          <td bgcolor=\$353535 height="200" valign="top">
            <p><font face="tahoma, arial" size="2">
             <ul type=disc>

EndOfHTML


open (INF,"datafiles/news") or dienice("Can't open log file");
@news = <INF>;
close (INF);
foreach $q (reverse(@news)) {
($date,$text)=split(/\|/,$q);
print "<small><li>$date<br></small>$text<br><br>"
}
print <<EndOfHTML;
  </ul>
            </font></p></td>
        </tr>
      </table><br><br>
EndOfHTML

}


sub dienice {

    ($errmsg) = @_;
    print "<td><h2>Error</h2>\n";
    print "$errmsg<p>\n";
    print "</body></html>\n";
    exit;
 }
                    