#!/usr/bin/perl

############################################################
#
# tripe.cgi or index.cgi
#
# Top level Shitespace cgi script.
#
# When uploading this from dev to prod, don't forget to
# comment out the line:
#   $siteHost = "dev";
# and make sure that the first line reads:
#   #!/usr/bin/perl
#
############################################################

    unshift @INC, ".";
    unshift @INC, "..";
    unshift @INC, "/Users/mike/Sites/pages/cgi-bin";
    
$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, "/Users/mike/Sites/pages/cgi-bin/tripe";
    $DEBUG      = 0;
    $HOST       = "http://localhost/cgi-bin/pages";
    $HOST       = "http://localhost/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";
}
elsif ($siteHost eq "prod")
{
    unshift @INC, "tripe";
}
elsif ($siteHost eq "dreamhost")
{
    unshift @INC, "tripe";
    unshift @INC, ".";
    unshift @INC, "..";
    $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";
}

require "date_calc.pl";
#require "stuff_serve.pl";
require "stuff_setup.pl";
require "stuff_funcs.pl";
require "stuff_html.pl";
require "stuff_pages.pl";
#require "stuff_test.pl";

unshift @INC, "tripe/lock";
require "empty.pl";

use File::Basename;

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

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



initialise();

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

$copyright = "";
$hr = "<hr>";


if ($page eq "test" && $DEBUG)
{
#    test_menu();  # gone
    write_help();
}
elsif ($invalid_date)
{
    write_invalid_date ($invalid_date);
}
elsif ($page eq "help")  # Help pages
{
    write_help ($arg1);  # arg1 = help page id (Default: welcome)
    write_log("T");
}
elsif ($page eq "todayspoem")  # Today's Poem (to be removed)
{
    write_poem();
    $copyright = "Shitespace Limited";
}
elsif ($page eq "stock") # Stock details
{
    write_log("T");
    if ($arg1 eq "")     # arg1 = stock id
    {
        #write_all_stocks(); # to be written
        write_stock();   # temp
    }
    else
    {
        write_stock();   # details of a specific stock
    }
}
elsif ($page eq "prospects") # User's Prospects Page
{
    write_log("T");
                     # arg1 = not used
    if ($arg2 < 2)   # arg2 = number of days of historical prospects
    {
        write_prospects(); # Today's prospects
    }
    else
    {
        write_prospects_history(); # Summary of recent prospects
    }
}
elsif ($page eq "group") # Group Prospects Page
{
    write_log("T");
                     # arg1 = groupId
    if ($arg2 < 2)   # arg2 = number of days of historical prospects
    {
        write_group(); # Today's prospects
    }
    else
    {
        write_group_history(); # History of recent group winners
    }
}
elsif ($page eq "forth") # Group K-Days & Birthdays page
{
    write_log("T");
    write_forth();       # arg1 = group id
}
elsif ($page eq "kday")  # User's K-Day page
{
    write_log("T");
    write_kday();        # arg1 = num of k-days (Default: 30)
}
elsif ($page eq "events")# Forthcoming Events
{
    write_log("T");
    write_events();      # arg1 = num of days (Default: 8)
}
elsif ($page eq "home" || $page eq "stuff")
{
    write_log("T");
    write_home();        # Stuff Today home page
}
#elsif ($page eq "whatisshite")
#{
#    write_whatisshite(); # An explanation about the Poem Of The Day's opinions
#    $copyright = "Shite Space Limited";
#}
elsif ($page eq "front")
{
#    write_front();       # www.shite.org front page  (to be renamed write_homepage and moved into shite_pages.pl)
    require "shite_pages.pl";           # assume it is a Shitespace page and see if
    write_shite ("shite");              # we can find the pageId in the page index.
    $copyright = "Shite Space Limited";
    $hr = "";
}
else                     # default
{
    require "shite_pages.pl";           # assume it is a Shitespace page and see if
    write_shite ($page);                # we can find the pageId in the page index.
    $copyright = "Shitespace Limited";
    $hr = "";
}

copyright($copyright, $hr);

exit( 0 );

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