#!/usr/bin/perl -w
use strict;

package MDS;
use mmCGI;

sub dprinthash
{
    my $h = shift;
    print "<p><span style='background-color:#DDFFDD'>";
    foreach my $k ( keys( %{$h} ) ) {
        print "$k: ";
        ref( $h->{$k} ) =~ /ARRAY/ and do {
            dprintarrayshort($h->{$k});
            next;
        };
                
        print defined( $h->{$k} ) ? "$h->{$k}\n" : "<b>(undef)</b>\n";
    }
    print "</span>";
}

sub dprintarrayshort
{
    my $a = shift;
    my $t = '[';
    foreach my $k ( @{$a} ) {
        print "$t$k";
        $t = '|';
    }
    print "]\n";
}



#use Exporter   ();
#@ISA         = qw(Exporter);
#@EXPORT      = qw(&MDS);

# wrapper
# cgi  .. mainframe
# cgi 'empty' 
# cgi 'top' 
# cgi 'dir' num
# cgi 'info' num file
# cgi 'source' num file
sub MDS
{
    my $Dirs = shift;
    my $Opts = { 'title' => 'MDS', 'charset' => 'ISO-8859-1', 
                 'raw_head' => '', 
                 'fields' => [] };#[ 'about' => 'About: ',
#                               'modifications' => 'Modifications: ' ] };
    $Opts = ParseOpts( $Opts, @_ );

    $Opts->{ 'back' } = $Opts->{ 'back' } || $ENV{ 'HTTP_REFERER' };
    $Opts->{ 'script' } = $Opts->{ 'script' } || $ENV{ 'SCRIPT_NAME' } || '.';

    eval {

    if( $#ARGV == - 1)
    {
        require 'MDSMainFrame.pl';
        MainFrame( $Opts );
    }
    elsif( $ARGV[0] eq 'info' )
    {
        require 'MDSInfo.pl';
        Info( $Opts, $Dirs, $ARGV[1], $ARGV[2] );
    }
    elsif( $ARGV[0] eq 'list' )
    {
        require 'MDSList.pl';

        List( $Opts, $Dirs, $ARGV[1], $ARGV[2] );
    }
    elsif( $ARGV[0] eq 'dir' )
    {
        require 'MDSDir.pl';
        Dir( $Opts, $Dirs, $ARGV[1] );
    }
    elsif( $ARGV[0] eq 'top' )
    {
        require 'MDSTop.pl';
        Top( $Opts, $Dirs );
    }
    elsif( $ARGV[0] eq 'Contents' )
    {
        require 'MDSContents.pl';
        Contents( $Opts, $Dirs, $ARGV[1], 'Contents.m' );
    }
    elsif( $ARGV[0] eq 'contents' )
    {
        require 'MDSContents.pl';
        Contents( $Opts, $Dirs, $ARGV[1], 'contents.m' );
    }
    elsif( $ARGV[0] eq 'empty' )
    {
        Empty( $Opts );
    }
    elsif( $ARGV[0] eq 'offline' )
    {
        require 'MDSOffline.pl';

        Offline( $Opts, $Dirs, $ARGV[1], $ARGV[2] );
    }
    elsif( $ARGV[0] eq 'buildhash' )
    {
        require 'MDSHash.pl';

        BuildHash( $Opts, $Dirs, $ARGV[1] );
    }
    else
    {
        Head( $Opts );
        print "Syntax error";
        exit
    }
};

if( $@ )
{
    if( defined( $ENV{ 'REQUEST_METHOD' } ) )
    {
        print "<span style=\"color: maroon\">$@</span>\n";
    }
    else
    {
        print STDERR $@;
    }
}

}

# HREF 
# ==============================================================================

sub HrefTop { return "$_[0]->{'script'}?top"; }

sub HrefEmpty { return "$_[0]->{'script'}?empty"; }

sub HrefDir { my ($Opts, $Dirs, $o ) = @_;
              return "$Opts->{'script'}?dir+" . $o/2; }

sub HrefCont { my ($Opts, $Name, $DirNum ) = @_;
               return "$Opts->{'script'}?$Name+$DirNum"; }

sub HrefInfo { my ($Opts, $DirNum, $Name ) = @_;
               return "$Opts->{'script'}?info+$DirNum+$Name"; }

sub HrefList { my ($Opts, $DirNum, $Name ) = @_;
               return "$Opts->{'script'}?list+$DirNum+$Name"; }


# ==============================================================================

sub Empty
{
    Head( $_[0] );
    print "</body>\n</html>\n";
}

sub ParseOpts
{
    my $Opts = shift;
    my $Key;

    while( $#_ > 0 )
    {    
        $Key = shift;
        $Opts->{ $Key } = shift;
    }
    return $Opts;
}



sub Head
{
    my $Opts = shift;

    # Pre-line
#    if( defined( $ENV{ 'REQUEST_METHOD' } ) )
#    {
        CGIPrintHead( $Opts );
#    }

    # Head
    print "<html>\n<head>\n";
    print "  <meta HTTP-EQUIV=\"Content-Type\" " .
        "CONTENT=\"text/html;charset=$Opts->{ 'charset' }\">\n";
    print "  <title>$Opts->{ 'title' }</title>\n";
    print "$Opts->{ 'raw_head' }</head>\n";
}

sub Crypt
{
    my $Data = shift;

    open FH, '-|', "/home.dokt/xmatousm/public_html/mds/DEScrypt " .
        "$main::Opts->{'passwd'} $Data" or
            do { print "Cannot crypt"; exit; };

    $Data = <FH>;
    close FH;
    chomp $Data;
    return $Data;
}

1;
