#!/usr/local/bin/perl -w
###############################################################################
# index.pl - this code displays the pda index page 
#
# Copyright (C) 2001 Norbert "Momo_102" Kuemin
# momo_102@bluemail.ch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###############################################################################
# pre stories cache update
use strict;
use lib '/home/slash';
use vars '%I';
use Slash;
use AvantGo;

my %LINKS;

##### Configuration #############################################
my $AVG = new AvantGo("/home/slash/avantgo.user");

#################################################################
sub main {
   *I = getSlashConf();
   getSlash();
   my $SECT = getSection($I{F}{section});
   $SECT->{mainsize} = int($SECT->{artcount} / 3);
   my $title = $SECT->{title};
   $title = "$title" unless $SECT->{isolate};

   # Default configuration
   $AVG->env_load();    
   $AVG->user_load(); 
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get("-");
   if ($AVG->env_get("USERID") ne "") { ($story, $comment, $poll, $hof, $uid) = $AVG->user_get(); }

   # Analyze parameters
   $LINKS{"Feedback"} = "feedback.pl";
   if ($hof != 0) { $LINKS{"Top"} = "hof.pl"; }
   if ($poll != 0) { $LINKS{"Umfrage"} = "vote.pl"; }
   if ($AVG->env_get("USERID") ne "") {
      $LINKS{"Setup"} = "config.pl";
      if ($uid == -1) {  
         $LINKS{"Login"} = "sign.pl";
      } else {
         $LINKS{"Logoff"} = "sign.pl?action=off";
      }
   }

   pda_header($I{sitename});
   pda_index_title($title,"title.jpg");
   pda_index_list($SECT, $story, "story.pl");
   pda_index_footer();
   pda_footer();
}

#################################################################
sub pda_header {
   my $title = shift;
   print CGI::header();
   print <<EOT if $title;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>$title</TITLE></HEAD><BODY>
EOT
}

#################################################################
sub pda_footer {
   print <<EOT;
</BODY></HTML>
EOT
}

#################################################################
sub pda_index_list {
   my ($SECT, $limit, $dispcmd) = @_;
   my $cursor = selectStories ($SECT, $limit);
   my $nr=1;
   print "<UL>\n";
   while (my($sid, $sec, $title, $date, $comment, $day, $hit) = $cursor->fetchrow()) {
      print "<LI> ";
      print "<A NAME=\"$nr\" HREF=\"".$dispcmd."?sid=$sid&nr=$nr\">";
      print $title;
      print "</A>";
      print "\n";
      $nr++;
   }
   print "</UL>\n";
}

#################################################################
sub pda_index_title {
   my $title = undef;
   my $pic = undef;
   my %links;
   if (scalar(@_) > 0) { $title = shift; }
   if (scalar(@_) > 0) { $pic = shift; }
   print "<CENTER>";
   if (defined($pic)) { print "<IMG SRC=\"$pic\"><BR>\n"; }
   if (defined($title)) { print "<B>$title</B><BR>\n"; }
   if (scalar(keys %LINKS)) {
      print "<HR>";
      foreach (sort (keys %LINKS)) {
         print "<A HREF=\"$LINKS{$_}\">$_</A>\n";
      }
   }
   print "<HR></CENTER>";
}

#################################################################
sub pda_index_footer {
   print "<HR>Stand: ".gmtime()." (GMT)";
}

#################################################################
main();
#################################################################
