#!/usr/local/bin/perl
############################################################################
# Script name:  index.pl
# Date:         Thursday, September 16, 1999 at 0:24
# Author:       Michael Ruder
# Usage:        Physics 2000 Table of contents Script
# Description:  Perl-Script for dynamic table of contents
#
############################################################################

require 5.004;

#
# Version information
#
$VERSION = 0;
$VERSION_SUB = 9;
$VERSION_TYPE = 'beta';
$VERSION_DATE = '09/16/99';
$COPYRIGHT = 'c 1999 by Michael Ruder / Physics 2000';

#
# DEBUG mode?
#
$DEBUG = 1;

#
# Set include path correctly on Unix
#
BEGIN
{
  if ($^O !~ m/MSWin32/)
  {
    #
    # Set include path, not windows OS
    #
    unshift(@INC, './DynaTOC/lib/');
  }
  else
  {
    #
    # Set include path, windows OS
    #
    unshift(@INC, './DynaTOC/lib/');
  }
}

#
# Used modules
#
use CGI;
use CGI::Carp;
use DynaTOC::Utils;
use File::Basename;

#
# Constants
#
$IDX_TitlePrefix = 'Physics 2000';
$IDX_StartPage = 'cover.html';

if ($^O !~ m/MSWin32/)
{
  #
  # flock contants for Unix
  #
  $LOCK_SH = 1;
  $LOCK_EX = 2;
  $LOCK_NB = 4;
  $LOCK_UN = 8;
}

#
# Initialization
#
srand(time() ^ ($$ + ($$ << 15)));
rand; rand; rand;
fileparse_set_fstype('UNIX');

#
# Get parameters from HTML
#
$query = CGI->new();

#
# Handle requests
#
if ($query->param('Type') eq 'Index' or not defined($query->param('Type')))
{
  $title = $IDX_TitlePrefix;
  $page = $IDX_StartPage;
  $addParam = '';

  if (defined($query->param('Page')))
  {
    ($masterTable, $subDirTable, $fileTable, $fileLookup) = &readDAT;
    $page = $query->param('Page');
    if (exists($fileLookup->{$page}))
    {
      $pageInfo = $fileLookup->{$page};
      $masterInfo = (grep {$_->[0] eq $pageInfo->[0]} @$masterTable)[0];
      $subDirInfo = (grep {$_->[0] eq $pageInfo->[1]} @{$subDirTable->{$pageInfo->[0]}})[0];
      $title .= ': ' . $pageInfo->[3];
      if (defined($masterInfo) and $pageInfo->[5] ne 'M')
      {
        $title .= ' (' . $masterInfo->[3];
        if (defined($subDirInfo) and $pageInfo->[5] ne 'S')
        {
          $title .= '/' . $subDirInfo->[3];
        }
        $title .= ')';
      }
      $addParam = '&Sub=' . $pageInfo->[0];
      if ($pageInfo->[1] ne '')
      {
        $addParam .= '#' . $pageInfo->[1];
      }
    }
  }

  &sendHeader;

  print &Frame_Index($title, 'index.pl?Type=Master','index.pl?Type=SubDir' . $addParam, $page);

  &sendFooter;
}
elsif ($query->param('Type') eq 'Master')
{
  ($masterTable, $subDirTable, $fileTable, $fileLookup) = &readDAT;

  &sendHeader;

  print &Frame_MasterHeader;

  $masterPos = 0;

  foreach $master (@$masterTable)
  {
    print &Frame_MasterMaster($master->[3], $master->[1], 'index.pl?Type=SubDir&Sub=' . $master->[0], $master->[4]);

    foreach $subdir (@{$subDirTable->{$master->[0]}})
    {
      print &Frame_MasterSubDir($subdir->[3], $subdir->[1], 'index.pl?Type=SubDir&Sub=' . $master->[0] . '#' . $subdir->[0], $subdir->[4]);
    }

    $masterPos++;
    if ($masterPos < @$masterTable)
    {
      print &Frame_MasterSeparator;
    }
  }

  print &Frame_MasterFooter;

  &sendFooter;
}
elsif ($query->param('Type') eq 'SubDir')
{
  ($masterTable, $subDirTable, $fileTable, $fileLookup) = &readDAT;

  &sendHeader;

  $title = 'Empty SubDir-TOC';

  if (defined($query->param('Sub')))
  {
    $masterInfo = (grep {$_->[0] eq $query->param('Sub')} @$masterTable)[0];
    if (defined($masterInfo))
    {
      $title = 'SubDir-TOC for ' . $masterInfo->[3] . ' of Physics 2000';
    }
  }

  print &Frame_SubDirHeader($title);

  if (defined($masterInfo))
  {
    print &Frame_SubDirMaster($masterInfo->[3], $masterInfo->[1], $masterInfo->[4]);

    foreach $subdir (@{$subDirTable->{$masterInfo->[0]}})
    {
      print &Frame_SubDirSubDir($subdir->[3], $subdir->[1], $subdir->[0], $subdir->[4]);

      foreach $file (@{$fileTable->{$masterInfo->[0]}{$subdir->[0]}})
      {
        if ($file->[3] =~ /R/)
        {
          print &Frame_SubDirFile($file->[2], $file->[0], $file->[3], $fileLookup{$file->[0]}->[0], $fileLookup{$file->[0]}->[1]);
        }
        else
        {
          print &Frame_SubDirFile($file->[2], $file->[0], $file->[3]);
        }
      }

      $subDirPos++;
      if ($subDirPos < @{$subDirTable->{$masterInfo->[0]}})
      {
        print &Frame_SubDirSeparator;
      }
    }
  }

  print &Frame_SubDirFooter;

  &sendFooter;
}
elsif ($query->param('Type') eq 'TOC')
{
  ($masterTable, $subDirTable, $fileTable, $fileLookup) = &readDAT;

  &sendHeader;

  print &TOC_Header;

  $masterPos = 0;

  foreach $master (@$masterTable)
  {
    print &TOC_Master($master->[2], $master->[1], $master->[0], $master->[4]);

    foreach $subdir (@{$subDirTable->{$master->[0]}})
    {
      print &TOC_SubDir($subdir->[2], $subdir->[1], $master->[0], $subdir->[0], $subdir->[4]);

      foreach $file (@{$fileTable->{$master->[0]}{$subdir->[0]}})
      {
        if ($file->[3] =~ /R/)
        {
          print &TOC_File($file->[1], $file->[0], $fileLookup{$file->[0]}->[0], $fileLookup{$file->[0]}->[1], $file->[3]);
        }
        else
        {
          print &TOC_File($file->[1], $file->[0], $master->[0], $subdir->[0], $file->[3]);
        }
      }
    }

    $masterPos++;
    if ($masterPos < @$masterTable)
    {
      print &TOC_Separator;
    }
  }

  print &TOC_Footer;

  &sendFooter;
}

##############################################################################
# sendFooter()
#
# sends information for bottom of page, such as comment lines for copyright
# information, is called after the </HTML> tag is sent.
#
##############################################################################
sub sendFooter
{
  print '<!--', $COPYRIGHT, '-->', "\n";
}

##############################################################################
# sendHeader()
#
# send CGI header and information
#
##############################################################################
sub sendHeader
{
  print $query->header();
  print '<!--This page was dynamically created by ', scalar(fileparse($0)), ' V ', $VERSION, '.', $VERSION_SUB, ' (', $VERSION_TYPE, ') as of ', $VERSION_DATE, '-->', "\n";
  print '<!--Creation time: ', scalar(gmtime), ' GMT-->', "\n";
  print '<!--Created for ', $query->user_agent(), ' on ', $query->remote_host(), '-->', "\n";
}
