#! /usr/bin/perl

use DBI;
use CGI;
do "lock.pl";


$q = new CGI;
$from = $q->param("from");
if ($from eq "")
{
  $from = 0;
}
$maxPerPage = 20;

#push(@INC,"e:\\html\\test\\cgi-bin");
require "myvars.pl";



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

IncludeFile("newstemplate1.html");


if (my $dbh = DBI->connect( $dbName, $dbUser, $dbPw))
{
  if ($from == 0)
  {
    open(FH, ">newscopy.html");
    lock(FH);
  }
  $sth = $dbh->prepare("select count(*) from news where theme != 'WAP only (for www.wapterror.de)' and theme != 'Kloppi'");
  $results = $sth->execute() or print "SQL-Error: $DBI::errstr\n";
  if (@result = $sth->fetchrow_array )
  {
    $newsCount = $result[0];
  }
  $sth->finish();

  $sth = $dbh->prepare("select title, text, date, comment, c, theme, user from news where theme != 'WAP only (for www.wapterror.de)' and theme != 'Kloppi' order by c desc limit $from, $maxPerPage");
  $results = $sth->execute() or print "SQL-Error: $DBI::errstr\n";

  if ($newsCount > $from + $maxPerPage)
  {
    print "<a href = \"news.pl?from=".($from + $maxPerPage)."\">View older news</a><br>";
  }
  else
  {
    print "<br>";
  }

  if ($from != 0)
  {
    print "<a href = \"news.pl?from=".($from - $maxPerPage)."\">View more recent news</a><br>";
  }
  else
  {
    print "<br>";
  }
  print "<br>";

  PrintTableHeader();

  my $date = "";
  while (@result = $sth->fetchrow_array )
  {
    if ($date ne $result[2])
    {
      $date = $result[2];
      print "<tr bgcolor = '#301c18'><th colspan = 3>$result[2]</th></tr>";
      if ($from == 0)
      {
        print FH "<tr bgcolor = '#301c18'><th colspan = 3>$result[2]</th></tr>";
      }
    }
    print "<tr><td bgcolor = ".($result[5] eq "Site update" ? "#401c18" : "#602020" )." valign = top>$result[0]</td>";
    print "<td bgcolor = '#502c28' valign = top>$result[1]<br><i>[$result[6]]</i></td>";
    if ($from == 0)
    {
      print FH "<tr><td bgcolor = ".($result[5] eq "Site update" ? "#401c18" : "#602020" )." valign = top>$result[0]</td>";
      print FH "<td bgcolor = '#502c28' valign = top>$result[1]<br><i>[$result[6]]</i></td>";
    }
    if ($result[3] != 0)
    {
      print "<td bgcolor = '#502c28' valign = top>";
      print "<table width = \"100%\" cellpadding = 4><tr><td bgcolor = '#802020'><font size = 1><a href = \"cgi-bin/showarticle.pl?reftype=news&ref=$result[4]\">View Comments</a></font></td></tr>";
      print "<tr><td><br><font size = 1><a href = \"cgi-bin/addcomment.pl?ref=$result[4]&reftype=news\">Add Comment</a></font></td></tr></table>";
      print "</td></tr>";
    }
    else
    {
      print "<td bgcolor = '#502c28' valign = top><font size = 1><a href = \"cgi-bin/addcomment.pl?ref=$result[4]&reftype=news\">Add Comment</a></font></td></tr>";
    }
  }
  $sth->finish();
  $dbh->disconnect();
  if ($from == 0)
  {
    unlock(FH);
    close(FH);
  }
}
else
{
  PrintTableHeader();
  print "<tr><td></td><td>";
  print "<font size = 2>";
  print "MySQL-Server error: $DBI::errstr\n";
  print "<br><br>Msg to the Strato-guys:<br>";
  print "If you don't wanna see all these flames, make your servers run!<br><br>";
  print "</font>";
  print "Here's the automatic backup copy of the last news page:<br><br>";
  print "</td></tr>";

  open(FH, "<newscopy.html") or print "nee";
#  lock(FH);
  while (<FH>)
  {
    print $_;
  }
#  unlock(FH);
  close(FH);
}

IncludeFile("newstemplate2.html");


sub PrintTableHeader()
{
  print "<table width = \"90%\">\n";
  print "<colgroup>\n";
  print "<col width=\"12%\">\n";
  print "<col width=\"80%\">\n";
  print "<col width=\"10%\">\n";
  print "</colgroup>\n";
}

sub IncludeFile
{
  $file = $_[0];

  if (open (FH, "<$file"))
  {
    while (<FH>)
    {
      $_ =~ s/\$homedir/$homedir/sgi;
      print $_;
    }
    close (FH);
  }
  else
  {
    print "File $file not found\n";
  }
}

