#!/usr/bin/perl -w

use strict;
require 'MDSDir.pl';
require 'MDSTop.pl';
require 'MDSInfo.pl';
require 'MDSList.pl';
require 'MDSContents.pl';
require 'MDSMainFrame.pl';

sub HrefTop { return "top.html"; }

sub HrefEmpty { return "empty.html"; }


sub HrefDir { my ($Opts, $Dirs, $o ) = @_;
              my  $root = $Opts->{'root'} || '';
              return "$root$Dirs->[$o]/index.html"; }

sub HrefCont { my ($Opts, $Name, $DirNum ) = @_;
               $MDS::Cont = $Name;
               return $MDS::Back . $MDS::Dirs->[$DirNum*2] ."/contents.html"; }

sub HrefInfo { my ($Opts, $DirNum, $Name ) = @_;
               $Name =~ s/\.m$//;
               push @MDS::Files, $Name;
               return $MDS::Back . $MDS::Dirs->[$DirNum*2] . "/$Name.html"; }

sub HrefList { my ($Opts, $DirNum, $Name ) = @_;
               $Name =~ s/\.m$//;
             return $MDS::Back . $MDS::Dirs->[$DirNum*2] . "/list/$Name.html"; }

sub Offline
{
    my ( $Opts, $Dirs, $Pass, $Output ) = @_[0..3];

    $MDS::Dirs = $Dirs;

    my $CSS = $Opts->{'css'};
    my $LCSS = $Opts->{'listcss'};

    die if( $Pass ne $Opts->{'passwd'} );

    $Output = $Output || './mds_offline';
    $Output =~ s/\/$//;
    
    print "Placing all to \"$Output/\"\n";

    mkdir $Output;
    $|  = 1;

    # empty page
    print STDOUT "Generating $Output/empty.html\n";
    open FH, ">", "$Output/empty.html" or die "cannot write";
    select FH;
    Empty( $Opts );
    close FH;

    # main frame
    print STDOUT "Generating $Output/index.html\n";
    open FH, ">", "$Output/index.html" or die "cannot write";
    select FH;
    MainFrame( $Opts );
    close FH;

    #top
    print STDOUT "Generating $Output/top.html\n";
    open FH, ">", "$Output/top.html" or die "cannot write";
    select FH;
    Top( $Opts, $Dirs );
    close FH;

    my ( $Row, $File );
    
    for( my $o = 0; $o < $#{$Dirs}; $o += 2 )
    {
        $Opts->{'root'} = '';
        $File = "$Output/" . HrefDir( $Opts, $Dirs, $o );
        print STDOUT "Generating $File\n";
        mkdir "$Output/$MDS::Dirs->[$o]";
        mkdir "$Output/$MDS::Dirs->[$o]/list";
        my $Back = "/" . $MDS::Dirs->[$o];
        $Back =~ s/\/[^\/]+/\.\.\//g;
        $Opts->{'root'} = $Back;

        defined( $CSS ) and $Opts->{'css' } = $Back . $CSS;
        defined( $LCSS ) and $Opts->{'listcss' } = "$Back../$LCSS";

        open FH, ">", "$File" or die "cannot write \"$File\"";
        select FH;

        @MDS::Files = ();
        $MDS::Cont = undef;
        $MDS::Back = $Back;

        Dir( $Opts, $Dirs, $o/2 );
        close FH;

        $MDS::Back = '';
        
        my @Files1 = @MDS::Files;
        defined( $MDS::Cont ) and do {
            print STDOUT "   contents\n";
            $File = "$Output/" . HrefCont( $Opts, $MDS::Cont, $o/2);
            $MDS::Back = $Back;
            
            open FH, ">", $File or die "cannot write \"$File\"";
            select FH;
            Contents( $Opts, $Dirs, $o/2, $MDS::Cont . ".m" );
            close FH;
        };

        print STDOUT "   info [";

        foreach my $f (@Files1 )
        {
            print STDOUT ".";
            $MDS::Back = '';
            $File = "$Output/" . HrefInfo( $Opts, $o/2, $f );
            $MDS::Back = $Back;

            open FH, ">", $File or die "cannot write \"$File\"";
            select FH;
            Info( $Opts, $Dirs, $o/2, $f . ".m" );
            close FH;
        }
        print STDOUT "]\n";
 
        print STDOUT "   list [";
        foreach my $f (@Files1 )
        {
            print STDOUT ".";
            $MDS::Back = '';
            $File = "$Output/" . HrefList( $Opts, $o/2, $f );
            $MDS::Back = $Back;

            open FH, ">", $File or die "cannot write \"$File\"";
            select FH;
            List( $Opts, $Dirs, $o/2, $f . ".m" );
            close FH;
        }
        print STDOUT "]\n";
   }
}

1;
