#!/usr/local/bin/perl

use strict;
use lib '/home/slash';
use vars '%I';
use Slash;
use AvantGo;

##### 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);
   $AVG->env_load();    
   $AVG->user_load(); 

   pda_header("AvantGo PDA Login");
   pda_sign_nav();
   print "<HR>\n";
   if ($AVG->env_get("USERID") ne "") {
      if (!CGI::param("action")) { pda_sign_input(); }
      if (lc(CGI::param("action")) eq "on") { pda_sign_on(); }
      if (lc(CGI::param("action")) eq "off") { pda_sign_off(); }
      if (lc(CGI::param("action")) eq "offnow") { pda_sign_offnow(); }
   } else {
      pda_sign_fail();
   }
   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 CGI::end_html(),"\n";
}

#################################################################
sub pda_sign_nav {
   print "<A HREF=\"index.pl\">Home</A> ";
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get();
   if ($uid == -1) {
      print "<A HREF=\"sign.pl\">Login</A>";
   } else {
      print "<A HREF=\"sign.pl?action=off\">Logoff</A>"; 
   }
   print "\n";
}

#################################################################
sub pda_sign_input {
   print "<FORM METHOD=\"POST\" ACTION=\"sign.pl\">\n";
   print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"on\">\n";
   print "User: <INPUT TYPE=\"text\" NAME=\"user\" VALUE=\"\"><BR>\n";
   print "Password: <INPUT TYPE=\"text\" NAME=\"password\" VALUE=\"\"><BR>\n";                                 
   print "<P><CENTER>";
   print "<INPUT TYPE=\"submit\" VALUE=\"Login\">";
   print "<INPUT TYPE=\"reset\" VALUE=\"Reset\">";
   print "</CENTER>\n";
   print "</FORM>\n";
}

#################################################################
sub pda_sign_on {
   my $user = CGI::param("user");
   my $pwd = CGI::param("password");
   my ($passwd, $uid) = sqlSelect("passwd,uid", "users", "nickname='".$user."'");
   if ($pwd eq $passwd) {
      my ($story, $comment, $poll, $hof, $old_uid) = $AVG->user_get();
      $AVG->user_set($AVG->env_get("USERID"), $story, $comment, $poll, $hof, $uid);
      print "<CENTER>Signed on as '$user'.</CENTER>\n";
      $AVG->user_save();
   } else {
      print "<CENTER>Signed on as '$user' <B>failed</B>.</CENTER>\n";
   }
}

#################################################################
sub pda_sign_off {
   print "<FORM METHOD=\"POST\" ACTION=\"sign.pl\">\n";
   print "<INPUT TYPE=\"hidden\" NAME=\"action\" VALUE=\"offnow\">\n";
   print " Are you sure to sign of?\n";
   print "<CENTER>";
   print "<INPUT TYPE=\"submit\" VALUE=\"Yes\">";
   print "</CENTER>\n";
   print "</FORM>\n";
}

#################################################################
sub pda_sign_offnow {
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get();
   print "<CENTER>Signed off.</CENTER>\n";
   $AVG->user_set($AVG->env_get("USERID"), $story, $comment, $poll, $hof, "-1");
   $AVG->user_save();
}

#################################################################
sub pda_sign_fail { 
   print "<H1 ALIGN=\"center\">Sorry, only for AvantGo-User</H1>\n";
}

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