#!/usr/bin/perl -w

use strict;
use FileHandle;

sub Contents
{
    my ( $Opts, $Dirs, $DirNum, $File ) = @_[0..3];

    $DirNum >= 0 and defined( $Dirs->[ $DirNum * 2 + 1 ] ) or
        do { Head($Opts); print "Wrong \"$DirNum\", possible attack!\n"; exit;};

    my $Dir = $Dirs->[ $DirNum * 2 + 1 ];

    $File =~ /^\w+.m$/ or $File =~ /^private\/\w+.m$/ or
        do { Head($Opts); print "Wrong \"$File\", possible attack!\n"; exit; };
    
    my $fh = new FileHandle;

    $fh->open( $Dirs->[$DirNum * 2 + 1] . $File ) or do { 
        Head($Opts); print "Cannot open \"$Dirs->[$DirNum * 2 + 1]$File\"\n"; 
        exit; };

    $Opts->{'title'} = $File;

    my $rh = $Opts->{'raw_head'};

    if( defined( $Opts->{'css'} ) )
    {
        if( ref( $Opts->{'css'} ) ne 'ARRAY' ) 
        { $Opts->{'css'} = [ $Opts->{'css'} ]; }

        foreach my $css ( @{$Opts->{'css'}} )
        {
            $Opts->{'raw_head'} .=
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css\">\n";
        }
    }

    Head( $Opts );
    $Opts->{'raw_head'} = $rh;

    my $Row = $fh->getline();
    chomp $Row;
    $Row =~ s/\%//;
    print "<body>\n<h1 class=\"function\">$Row</h1>\n";
    print "<pre>";

    while( $Row = $fh->getline() )
    {
        $Row =~ s/\%//;
        chomp $Row;

        if( $Row =~ /^\s*(\S*)\s*-/ )
        {
            my $fce = $1;
            #print "\n$dir|$fce\n";
            if( $fce !~ /\.m$/ ) { $fce .= '.m'; }
            if( -e $Dir . $fce )
            {
                my $href = "\"" . HrefInfo( $Opts, $DirNum, $fce ) . 
                    "\" target=\"mdsbody\"";
                $Row =~ s/(^\s*)(\S*)/$1\<a href = $href\>$2\<\/a\>/;
            }
        }
        print "$Row\n";
    }

    print "</pre>\n</body></html>";
    $fh->close();
}

1;
