#!/usr/bin/perl

############################################################
#
# pooclub.cgi
#
# Top level Pooclub cgi script.
#
############################################################

    unshift @INC, ".";
    unshift @INC, "..";
    unshift @INC, "/Users/mike/Sites/pages/cgi-bin";

require "uber/uber_main.pl";

#$SCRIPT_TITLE = "poopages";
$SCRIPT_TITLE = "pooclub";
$ADMIN_EMAIL  = "mike2sheds\@gmail.com"; 
@CSS_LIST = ("basic_stuff.css");

uber_main("uber_utils",
          "uber_login",
          "uber_admin",
          "uber_polls",
          "uber_pooclub");

print $cgi->header(-cookie => [@COOKIE_LIST]);

$httpHost = $ENV{HTTP_HOST};
                                            
 # Set host-specific global variables - default="localhost"
$DEBUG      = 0;
$HOST       = "http://localhost";
$HOMEDIR    = "";
$CSSDIR     = "";
$IMGHOMEDIR = "";
$WRITEDIR   = "";
$READDIR    = "";  

if ($httpHost eq "pages.pooclub.com"
 || $httpHost eq "www.pages.pooclub.com")
{
    unshift @INC, "tripe";
    $DEBUG      = 0;
    $HOST       = "http://pages.pooclub.com";
    $HOMEDIR    = "../htdocs/tripe/public";
    $CSSDIR     = "../htdocs/tripe/public/data";
    $IMGHOMEDIR = "../htdocs/tripe/public/images";
    $WRITEDIR   = "../htdocs/write/tripe/monthly";
    $READDIR    = "../htdocs/write/tripe/monthly";
}
elsif ($httpHost eq "shite.org"
    || $httpHost eq "www.shite.org")
{
    unshift @INC, "tripe";
    $DEBUG      = 0;
    $HOST       = "http://www.shite.org";
    $HOMEDIR    = "../htdocs/tripe/public";
    $CSSDIR     = "../htdocs/tripe/public/data";
    $IMGHOMEDIR = "../htdocs/tripe/public/images";
    $WRITEDIR   = "../htdocs/write/tripe/monthly";
    $READDIR    = "../htdocs/write/tripe/monthly";
}
else    # "localhost"
{
    unshift @INC, "/Users/mike/Sites/pages/cgi-bin/tripe";
    $DEBUG      = 0;
#    $HOST       = "http://localhost";
    $HOST       = "http://localhost/pages";
#    $HOST       = "http://localhost/cgi-bin/pages";
    $HOMEDIR    = "/Users/mike/Sites/pages/htdocs/tripe/public";
    $CSSDIR     = "/pages/htdocs/tripe/public/data";
    $IMGHOMEDIR = "/pages/htdocs/tripe/public/images";
    $WRITEDIR   = "/Users/mike/Sites/pages/htdocs/write/tripe/monthly";
    $READDIR    = "/Users/mike/Sites/pages/htdocs/write/tripe/monthly";
}

use CGI qw(:standard :html3);

$q = new CGI;

require "stuff_setup.pl";
require "stuff_funcs.pl";
require "pooclub_html.pl";
require "pooclub_pages.pl";

use File::Basename;

$PID     = $$;
$SCRIPT  = basename($0);

initialise();

($poem_file, $poem_title, $poem_author) = split /;/, poem_of_the_day($u_yyyymmdd);


# Start writing HTML

$copyright = "";

$page = $arg1 if ($page eq "html");  # for historical reasons

my $date = $u_yyyymmdd;
$date = $arg1 if ($arg1);
write_log("P");

if ($page eq "home" || $page eq "main" || $page eq "today" || $page eq "lobby" || $page eq "club"
 || $page eq "poo" || $page eq "pooclub" || $page eq "pooclubtoday")
{
    pooclub_header("Pooclub Today");
    pooclub_lobby($date);
}
elsif ($page eq "shed")
{
    pooclub_header("The Shed");
    pooclub_shed($date);
}
elsif ($page eq "prospects")
{
    pooclub_header("Pooclub Prospects", "Prospects for Pooclub Members");
    pooclub_prospects_page($date);
}
elsif ($page eq "league")
{
    pooclub_header("Prospects League Table", "League Table Of Recent Prospects");
    pooclub_leaguetable("pooclubbers", 14, $date);
}
elsif ($page eq "news" || $page eq "clubnews")
{
    pooclub_header("Club News", "Recent Happenings In Pooclub");
    pooclub_clubnews($date);
}
elsif ($page =~ /^ed/)
{
    pooclub_header("Editorial", "Wise Words From Our Authors");
    pooclub_editorial($date);
}
elsif ($page =~ /^olded/ || $page eq "backcat")
{
    pooclub_header("Editorial Back Catalogue", "Wise Words From Our Authors");
    pooclub_backcat($date);
}
elsif ($page eq "oldnews")
{
    pooclub_header("Old Club News", "Past Happenings In Pooclub");
    pooclub_oldnews($date);
}
elsif ($page eq "events")
{
    pooclub_header("Forthcoming Events & Weather", "Great Things To Look Forward To");
    pooclub_events($date);
}
elsif ($page =~ /^troop/ || $page =~/^stortroop/)
{
    pooclub_header("Stortroopers", "What we look like");
    pooclub_stortroopers();
}
elsif ($page =~ /^sparetroop/)
{
    pooclub_header("Sparetroopers", "Troopers for new members");
    pooclub_sparetroopers();
}
elsif ($page =~ /^shedlight/)
{
    pooclub_header("Shedlight", "Shed Light On Your Messages");
    shedlight();
}
elsif ($page =~ /^celsius/)
{
    pooclub_header("Celsius/Centigrade Converter", "Celsius To Centigrade Converter");
    celsius();
}
elsif ($page =~ /chat/)
{
    my $chatGroup = $cgi->param("c");
    my $chatId = $cgi->param("i");
    # Note: no header here - that's done in pooclub_chats
    pooclub_chats("$chatGroup", "$chatId");
}
elsif ($page eq "problems" || $page eq "problem" || $page eq "prob")
{
    my $problemId = $arg1;

    if ($problemId eq "")  # Default to home Problems Page
    {
        $problemFile = "pooclub_problems.html";
    }
    else                   # show specific problem page
    {
        $problemFile = "problem_${problemId}.html";
    }

#    pooclub_header ("Problems Page", "All Your Problems Solved Here");
    pooclub_readhtml("$problemFile");
}
else
{
    my $htmlFile = "pooclub_$page.html";

    if (open HTMLFILE, "<$POODIR/$htmlFile") # Check for pooclub html file
    {
        close (HTMLFILE);
#        pooclub_header("$page");
        pooclub_readhtml("$htmlFile");
    }
    else      # Check for special days...
    {
        my ($dow, $mday, $month, $year) = split /;/, parse_date($date);
    
        if ($dow eq "Tuesday" && $mday eq "22")  # It's a lucky day!
        {
#            pooclub_header("Tuesday 22nd", "The Official Pooclub Lucky Day");
            pooclub_readhtml("pooclub_tuesday22nd.html");
        }
        elsif (open NEWSFILE, "<$NEWSDIR/news_${date}.txt")
        {
            # if there's a new newsfile today, show clubnews page
            close (NEWSFILE);
            pooclub_header("Club News", "Recent Happenings In Pooclub");
            pooclub_clubnews($date);
        }
        else  # Default to home page
        {
            pooclub_header();
            pooclub_lobby($date);
        }
    }
}

copyright($copyright);

exit( 0 );

############################################################
# END OF FILE
############################################################
