############################################################
#
# stuff_setup.pl
#
# Handles parameters and sets global variables
#
############################################################

#require "date_calc.pl";
require "stuff_funcs.pl";

use File::Basename;
use CGI;

############################################################
#   |
#   |_cgi-bin
#   |
#   |_htdocs
#     |
#      tripe
#      |
#      -public
#       |
#       |__data
#       |   |
#       |   |__bible
#       |   |
#       |   |__captions
#       |   |
#       |   |__editorial
#       |   |
#       |   |__events
#       |   |
#       |   |__general
#       |   |
#       |   |__groups
#       |   |
#       |   |__help
#       |   |
#       |   |__lists
#       |   |
#       |   |__meta
#       |   |
#       |   |__multi
#       |   |
#       |   |__news
#       |   |
#       |   |__passages
#       |   |
#       |   |__phrases
#       |   |
#       |   |__poetry
#       |   |
#       |   |__poo
#       |   |
#       |   |__stories
#       |
#       |__images...
#
############################################################

### Global variables

# Directories
$CGI_BIN  = "";
$POODIR   = "";
$HTDOCS   = "";
$DATADIR  = "";
$METADIR  = "";
$GENDIR   = "";

$BIBLEDIR = "";
$EDITDIR  = "";
$EVENTDIR = "";
$GROUPDIR = "";
$HELPDIR  = "";
$LISTDIR  = "";
$MULTIDIR = "";
$NEWSDIR  = "";
$PASSDIR  = "";
$POEMDIR  = "";  # archaic
$POETRYDIR = "";
$STORYDIR = "";
$LOGDIR   = ""; # Where user data can be written to
$VIEWLOGDIR = "";
$VOTELOGDIR = "";
$CACHEDIR   = "";

$IMGDIR1        = "";
$IMGDIR2        = "";
$IMGPOODIR      = "";
$IMGTROOPDIR    = "";
$IMGSHITEDIR    = "";
$IMGCAPTIONSDIR = "";
$IMGNNMHDIR     = "";
$IMGKKDIR       = "";

# Files (Metadata files)
$GROUPS_FILE        = "";
$HELP_FILE          = "";
$PAGE_INDEX_FILE    = "";
$PASSAGES_FILE      = "";
$POEMS_FIXED_FILE   = "";
$POEMS_RANDOM_FILE  = "";
$POO_HTML_FILE      = "";
$PROPERTIES_FILE    = "";
$SHITE_INDEX_FILE   = "";
$STOCKS_GREAT_FILE  = "";
$STOCKS_LESSER_FILE = "";
$STOCKTYPES_FILE    = "";
$TROOP_FILE         = "";

# Files (General data files)
$POO_MENUS_FILE     = "";
$QUOTES_FILE        = "";
$EVENTS_FILE        = "";
$NEWWORD_FILE       = "";

$VOTING_DATA_FILE   = "";
$VOTING_LOG_FILE    = "";
$TEMP_FILE          = "";

# Scripts and Links
$STUFF_CGI  = "";
$POO_CGI    = "";
$SHITESPACE = "";

# Constants
$STOCK_BONUS = 5;  # Multiplier for primary stock

# CGI Parameters
$page    = "";
$arg1    = "";
$arg2    = "";
  # Today's date as entered by user...
$u_mday  = "";   # Day of month (1,2, .. ,31)
$u_month = "";   # Full name of month ("January","February", .. , "December")
$u_year  = "";   # Year (4 digits)
  # Date of Birth...  (formats as above)
$b_mday  = "";   
$b_month = "";
$b_year  = "";

$poemNum = "";   # Current position in poem voting list

$u_mon   = "";   # Numeric value of $u_month (0,1, .. ,11)
$b_mon   = "";   #    "      "   "  $b_month       "
$r_now   = "";   # The real time (HH:MM) 

$r_yyyymmdd = 0; # The real date (set by system clock)
$u_yyyymmdd = 0; # User date (set by user) Default = real date
$b_yyyymmdd = 0; # Date of birth (set by user)

@colour = ();    # List of colours.  The colour scheme changes if
                 # the user selects a User date which is in the past.

$invalid_date = ""; # Info about validity of dates supplied by user.
$age          = 0;  # User's age in days
$b_format     = ""; # Date of birth formatted for display
$u_format     = ""; # User date formatted for display
$birth_seed   = 0;  # Seed for DoB calcs
$date_seed    = 0;  # Seed for User date calcs
$date_args    = ""; # String of date args to be passed to server
$timezone     = "+0000";

# Stocks Data
$stockHash   = {};  # The Great Stocks
@greatList   = ();
@lesserList  = ();  # The Lesser Stocks (share hash with Great Stocks but need separate list)
$propHash    = {};  # Properties of the Stocks
@propList    = ();

# Groups Data
$groupHash   = {};
@groupList   = ();
$groups_size = 0;   # Number of groups
    

sub initialise
{
    my $i;

    $SCRIPT     = basename($0);
    $CGI_BIN    = "$HOST/cgi-bin";
    $THIS_CGI   = "$CGI_BIN/$SCRIPT";
    $STUFF_CGI  = "$CGI_BIN/tripe.cgi";
    $POO_CGI    = "$CGI_BIN/pooclub.cgi";

    # $HOMEDIR and $HOST come from server_info
    $HTDOCS     = "$HOMEDIR";      # Want to move this to $HOMEDIR/htdocs
                                   # stuff_serve.pl will then have to set HOMEDIR
                                   # to /home/shite (online) and .. (offline)
    $SHITESPACE = "$HOST/main";

    $DATADIR    = "$HOMEDIR/data";
    $METADIR    = "$DATADIR/meta";
    $GENDIR     = "$DATADIR/general";

    $BIBLEDIR   = "$DATADIR/bible";
    $EDITDIR    = "$DATADIR/editorial";
    $EVENTDIR   = "$DATADIR/events";
    $GROUPDIR   = "$DATADIR/groups";
    $HELPDIR    = "$DATADIR/help";
    $LISTDIR    = "$DATADIR/lists";
    $MULTIDIR   = "$DATADIR/multi";
    $NEWSDIR    = "$DATADIR/news";
    $PASSDIR    = "$DATADIR/passages";
    $POODIR     = "$DATADIR/poo";
    $POEMDIR    = "$DATADIR/poems";  # archaic
    $POETRYDIR  = "$DATADIR/poetry";
    $STORYDIR   = "$DATADIR/stories";
    $LOGDIR     = "$HOMEDIR/log";
    $VIEWLOGDIR = "$LOGDIR/views";
    $VOTELOGDIR = "$LOGDIR/votes";
    $CACHEDIR   = "$LOGDIR/cache";

    # $IMGHOMEDIR comes from server_info
    $IMGDIR1        = "$IMGHOMEDIR/imgshite";     # for Shitespace and "Shitespace 2000"
    $IMGDIR2        = "$IMGHOMEDIR/imgshite";     # for Shitespace
    $IMGSHITEDIR    = "$IMGHOMEDIR/imgshite";    # for Shitespace poems
    $IMGPOODIR      = "$IMGHOMEDIR/imgpoo";      # for pooclub (formerly "images3")
    $IMGCAPTIONSDIR = "$IMGHOMEDIR/imgcaptions"; # for pooclub captions competition
    $IMGTROOPDIR    = "$IMGHOMEDIR/imgtroop";    # stortroopers
    $IMGNNMHDIR     = "$IMGHOMEDIR/imgnnmh";     # for NotNearMyHouse
    $IMGKKDIR       = "$IMGHOMEDIR/imgkk";       # for poopages knick knacks

    $GROUPS_FILE        = "$METADIR/groups.dat";
    $HELP_FILE          = "$METADIR/help.dat";
    $PAGE_INDEX_FILE    = "$METADIR/page_index.dat";
    $PASSAGES_FILE      = "$METADIR/passages.dat";
    $POEMS_FIXED_FILE   = "$METADIR/poems_fixed.dat";
    $POEMS_RANDOM_FILE  = "$METADIR/poems_random.dat";
    $POO_HTML_FILE      = "$METADIR/pooclub_html.dat";
    $PROPERTIES_FILE    = "$METADIR/properties.dat";
    $SHITE_INDEX_FILE   = "$METADIR/shite_index.dat";
    $STOCKS_GREAT_FILE  = "$METADIR/stocks_great.dat";
    $STOCKS_LESSER_FILE = "$METADIR/stocks_lesser.dat";
    $STOCKTYPES_FILE    = "$METADIR/stocktypes.dat";
    $TROOP_FILE         = "$METADIR/troop.dat";

    $EVENTS_FILE        = "$GENDIR/events.dat";
    $NEWWORD_FILE       = "$GENDIR/newword.dat";
    $POO_MENUS_FILE     = "$GENDIR/pooclub_menus.dat";
    $QUOTES_FILE        = "$GENDIR/quotes.dat";

    $VOTING_DATA_FILE   = "$WRITEDIR/votes.dat";
    $VOTING_LOG_FILE    = "$WRITEDIR/votes.log";
    $TEMP_FILE          = "$WRITEDIR/tempfile.tmp";

    $cgi = new CGI;
    
    $page    = $cgi->param("p");
    $arg1    = $cgi->param("a");
    $arg2    = $cgi->param("b");
    $u_mday  = $cgi->param("ud");
    $u_month = $cgi->param("um");
    $u_year  = $cgi->param("uy");
    $b_mday  = $cgi->param("bd");
    $b_month = $cgi->param("bm");
    $b_year  = $cgi->param("by");
    $poemNum = $cgi->param("poemNum");
    
    $u_mon   = 12;  # "Bad" month
    $b_mon   = 12;  # "Bad" month
    
    for ($i = 0; $i < 12; $i++)  # What month number?
    {
        if ($u_month eq $monthList[$i]) { $u_mon = $i; }
        if ($b_month eq $monthList[$i]) { $b_mon = $i; }
    }
    
    # Get real (system) time/date
    ($r_sec,$r_min,$r_hour,$r_mday,$r_mon,$r_year,$r_wday,$r_yday,$r_isdst)
        = localtime(time);
    $r_year += 1900;
    $r_month = $monthList[$r_mon];
    $r_day = $dayList[$r_wday];
    $r_now = sprintf "%02d:%02d", $r_hour, $r_min;
    $timezone = "+0100" if $r_isdst == 1;
    
    
    if ($u_year eq "")  # No user date - default to real date
    {
        $u_mday = $r_mday; $u_mon = $r_mon; $u_year = $r_year;
        $u_month = $monthList[$u_mon];
    }
    
    
    $r_yyyymmdd  = sprintf "%04d%02d%02d", $r_year, $r_mon+1, $r_mday;
    $u_yyyymmdd  = sprintf "%04d%02d%02d", $u_year, $u_mon+1, $u_mday;

    # Use a different colour scheme if user has changed today's date
    if ($u_yyyymmdd < $r_yyyymmdd)    # Past (green)
    { @colour = @colourList3; }
    elsif ($u_yyyymmdd > $r_yyyymmdd) # Future (brown)
    { @colour = @colourList2; }
    else                              # Present (blue)
    { @colour = @colourList1; }

    if ($b_year =~ /^\d\d\d\d$/)   # Year of birth is valid
    {
        $b_yyyymmdd  = sprintf "%04d%02d%02d", $b_year, $b_mon+1, $b_mday;
        $age = date_calc("-c $b_yyyymmdd $u_yyyymmdd");
        ($notused, $b_dayOfWeek) = split / /, date_calc("-s $b_yyyymmdd");
        $invalid_date = "birth" if ($b_dayOfWeek eq "INVALID_DATE");
        $b_format = "$b_dayOfWeek $b_mday $monthList[$b_mon] $b_year";
        $birth_seed = $b_yyyymmdd;
    }
    else
    {
        $b_yyyymmdd = 0;
        $age        = -1;    # Indicates that no DoB is given
        $b_format   = "";
        $birth_seed = -1;
        $b_mday     = "";
        $b_month    = "";
        $b_year     = "";
    }
    
    if ($b_yyyymmdd > $u_yyyymmdd || $u_yyyymmdd > $r_yyyymmdd)
    {
        $invalid_date = "order";
    }
    
    $dob_args   = "bd=$b_mday&bm=$b_month&by=$b_year";
    $today_args = "ud=$u_mday&um=$u_month&uy=$u_year";
    $date_args  = "${today_args}&${dob_args}&poemNum=${poemNum}";
    
    ($notused, $u_dayOfWeek) = split / /, date_calc("-s $u_yyyymmdd");
    $invalid_date = "todays" if ($u_dayOfWeek eq "INVALID_DATE") ;
    $u_format = "$u_dayOfWeek $u_mday $monthList[$u_mon] $u_year";
    
    $date_seed = $u_yyyymmdd;

    # Populate lists and hashes
    get_stock();
    get_properties();
    get_groups();

    $groups_size = scalar (@groupList);
}
    
############################################################
    
1;

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