#!/usr/bin/perl
############################################################
#
# pooclub.cgi
#
# Top level Pooclub cgi script.
#
# When uploading this from dev to prod, don't forget to
# comment out the line:
#   $siteHost = "dev";
# and remove the top line so that the first line reads:
#   #!/usr/bin/perl
#
############################################################

    unshift @INC, ".";
    unshift @INC, "..";

require "uber/uber_main.pl";

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

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

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

$siteHost = "dreamhost";
#$siteHost = "prod";
#$siteHost = "dev";

# Set host-specific global variables - default="prod"
$DEBUG      = 0;
$HOST       = "http://www.shite.org";
$HOMEDIR    = "/home/shite/htdocs/tripe/public";
$CSSDIR     = "/tripe/public/data";
$IMGHOMEDIR = "/tripe/public/images";
$WRITEDIR   = "../htdocs/write/tripe/monthly";
$READDIR    = "/home/shite/htdocs/write/tripe/monthly";  # same dir as WRITEDIR but
                                               # our host (titanhosts)
                                               # requires us to access it
                                               # differently

if ($siteHost eq "dev")
{
    unshift @INC, "c:/xampp/cgi-bin/tripe";
    $DEBUG      = 0;
    $HOST       = "http://localhost";
    $HOMEDIR    = "C:/xampp/htdocs/tripe/public";
    $CSSDIR     = "/tripe/public/data";
    $IMGHOMEDIR = "../tripe/public/images";
    $WRITEDIR   = "C:/xampp/htdocs/write/tripe/monthly";
    $READDIR    = "C:/xampp/htdocs/write/tripe/monthly";
}
elsif ($siteHost eq "prod")
{
    unshift @INC, "tripe";
}
elsif ($siteHost eq "dreamhost")
{
    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";
}

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);

#($DEBUG, $HOMEDIR, $HOST, $IMGHOMEDIR) = split /;/, server_info();

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
############################################################
