#!/usr/bin/perl -w
# program mainIndex.pl
use strict;
use lib "/pathToYourWebspace/cgi-bin/gnu";

use CGI;
use CGI::Carp qw( fatalsToBrowser);

use PathInfo;
use Comments;
use MostRecentSideIndex;
use WorkInProgressSideIndex;
use MainIndex;
use PageFormatterTemplate;

# list of the side indexes
# Note the @groupNameAry must be updated each time you create a new subject group
# Note: make sure you have default group specified TWICE at $pathInfoObj->setGroupId("subjectGroup1") below
# and in case of error at # fatal error: do not pass go : $sideIndexObj    = subjectGroup1::SideIndex->new();

use subjectGroup1::SideIndex;
use subjectGroup2::SideIndex;
use subjectGroup3::SideIndex;
use subjectGroup4::SideIndex;
use subjectGroup5::SideIndex;


# clean up environment for taint mode before calling mail
BEGIN {
    $ENV{PATH} = "/bin:/usr/bin";
    delete @ENV{ qw( IFS CDPATH ENV BASH_ENV) };
} # end BEGIN

    # we'll be checking the environment for post parameters etc
    my $cgiObj    = new CGI();
    


# to create a new index entry
# 1. enter the name of the package into the use list
# 2. enter the name of the group into the @groupNameAry for use by pathInfo validation
# 3. use group and package names to create new elsif stmt for getting the sideIndex data
# 4. add thumb and url data for the group to the @linkAry

    # create content type header to facilitate output of error messages
    print "Content-type: text/html; charset=iso-8859-1\n\n";
    
    
    
    # header content is best treated separately
    # note that the pageTitle is also the title of the first picture to be displayed
    
    my $metaKeywords    = "Irish,Julius,Guzy,paintings,blah blah";
    my $metaDescription = "figurative blah description";
    my $pageTitle       = "Main Index to thisIsMySite.com";
    my $indexGroup      = undef;
    my $pageFileName    = "mainIndex.pl";
    
    my $newFormatUrl    = "http://yourWebsiteDotCom/cgi-bin/gnu/";

    # update this each time you create a new subject group
    my @groupNameAry    = qw(    subjectGroup1 subjectGroup2 subjectGroup3 subjectGroup4
                                subjectGroup5
                        );


    # pathInfo determines the side index to use
    # Note that tests for informationType of any given type need only be conducted if 
    # the page features on one of the informationType indexes
    my $pathInfoObj    = PathInfo->new($cgiObj);
    if ($pathInfoObj->pathInfo()) {
        if ( ($pathInfoObj->mostRecentSideIndex() ) || 
             ($pathInfoObj->workInProgressSideIndex() ) || 
             ($pathInfoObj->mainIndex() ) ) {
            # OK
        } else {
            # someone is fooling around
            # send message but keep going - ignore it
            sendErrorEmail("unrecognised pathInfo string","mainIndex",$pathInfoObj->pathInfo());
            $pathInfoObj->setSubjectGroupAndResetError("subjectGroup1");
        } # end if
    } # end if        

    
    # check for comments data having been sent to us
    # new will get these and validate them
    my $commentsObj    = Comments->new($cgiObj);
    
    # select the side index according to the indexGroup 
        
    my $indexGroup      = $pathInfoObj->subjectGroup();
    my $sideIndexObj    = undef;
    if ($indexGroup eq "subjectGroup1") {
        $sideIndexObj    = subjectGroup1::SideIndex->new();
    } elsif ($indexGroup eq "subjectGroup2") {
        $sideIndexObj    = subjectGroup2::SideIndex->new();
    } elsif ($indexGroup eq "subjectGroup3") {
        $sideIndexObj    = subjectGroup3::SideIndex->new();
    } elsif ($indexGroup eq "subjectGroup4") {
        $sideIndexObj    = subjectGroup4::SideIndex->new();
    } elsif ($indexGroup eq "subjectGroup5") {
        $sideIndexObj    = subjectGroup5::SideIndex->new();
    } elsif ($pathInfoObj->mostRecentSideIndex()) {
        $sideIndexObj    = MostRecentSideIndex->new();
    } elsif ($pathInfoObj->workInProgressSideIndex()) {
        $sideIndexObj    = WorkInProgressSideIndex->new();
    } else {
        # fatal error: do not pass go
        sendErrorEmail($cgiObj,"mainIndex.pl","unknown index group:$indexGroup  you've probably forgotten to make an entry in the uses group::SideIndex list or the long if statement");
        $sideIndexObj    = subjectGroup1::SideIndex->new();  # keep going anyway
    }#    end if
    
    my $pageObj    = PageFormatterTemplate->new($cgiObj,$indexGroup,$sideIndexObj,$commentsObj,$pageFileName,$pageTitle,$metaKeywords,$metaDescription);

    $pageObj->mainIndexContent( MainIndex->mainContentLayout() );




sub sendErrorEmail{
my $errorLocation        = shift;
my $pathInfoString       = shift;

# for details on sending mail see Guelich, Gundavaram and Birznieks, CGI Programming with Perl, O'Reilly, 2000, pp.222-225
    
    my $httpUserAgent    = $ENV{HTTP_USER_AGENT};
    my $httpReferer      = $ENV{HTTP_REFERER};
    my $newBrowser       = "browser: " . $httpUserAgent;
    
    my $remoteAddress    = $ENV{REMOTE_ADDR};
    
    # create the subject string
    my $subjectStr       = "Error in mainIndex.pl";
    
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    my $timeStampStr     = "$year:$mon:$hour:$min:$sec";
    
	open MAIL, "| /usr/lib/sendmail -t -i -f'gnuReturnedMailAddr_yourWebsiteDotCom'" or return undef;
	print MAIL <<END_OF_MESSAGE;
To: gnuDiagnosticMailAddr_yourWebsiteDotCom
From: gnuErrorMailAddr_yourWebsiteDotCom
Reply-To: gnuErrorHandlerMailAddr_yourWebsiteDotCom
Subject: gnuError: $subjectStr

Error occurred in mainIndex.pl in  free software
TimeStamp : $timeStampStr

errorLocation : $errorLocation
pathInfoString : $pathInfoString

remoteAddress : $remoteAddress
httpUserAgent : $httpUserAgent
httpReferer : $httpReferer
browser: $newBrowser
END_OF_MESSAGE
     
    
    print MAIL "\n";
    
    
    close MAIL or die "Error closing sendmail in mainIndex.pl: $!";



} # end emailVisitorLogInfo