print "###################################################\n";

$inDir = "Applications:datamind:";  
$contentsfile = "Applications:datamind:Contents.m";  

$sep = ':';				#Uncomment this on a macintosh
$cur = '';
$up = ':';

#$sep = '/';			#Uncomment this on a unix box
#$cur = '.';
#$up = '..';

chdir $inDir;				  #current directory

open (CFILE, $contentsfile) or die "Error: cannot access file $contents";
@stat = stat($contentsfile);
read CFILE, $contents, $stat[7];
close CFILE;

$latex_manual = "";

#launches the recursive routine
treatFolder($inDir,'General purpose commands');

open (CFILE, ">$contentsfile") or die "Error: cannot access file $contents";
print CFILE $contents;
close CFILE;

sub treatFolder
{
    my ($folder,$section) = @_;
	my @subFoldList = ();
	my @classNames = ();
	
    #$folder - is the directory which is currently evaluated
    #$section - the title of the section  (I am going to change it)

    $constr = 0;

    #reads the file in the current  directory and saves them in a variable
    opendir (FOLDER, $folder) or die "Error: directory $folder not available";
    my @files = readdir FOLDER;
    closedir FOLDER;

	foreach $aFile (@files)
    {
		$lFile = $folder.$aFile;
		
		#if it is a directory add it to the subdirectories and class list
		if (-d $lFile) 
		{
		    @subFoldList = ( @subFoldList, $lFile.$sep);
		    @classNames = ( @classNames, $aFile);
	
		}
		else  #when it is a possible method
		{
			if ($aFile =~ m/(.+)\.m/)
			{
			    $name = $1;
			    $lname = $name.":".(" "x(13-length($name)));
			    
			    print "analyzing   $lFile\n";         #let's show what we are processing

	    		open (AFILE, $lFile) or die "Error: cannot access file $lFile";
	    		
				while (<AFILE>)
				{				
					if (m/\@purpose\s+(.+)/)   
					{
						$purp = $1;
						$contents =~s/$name:.*/$lname$purp/;
					}   
		
				}
		     	close AFILE; 
			}
	
		}
		
    }
    
    
    my $n=0;
    
    foreach my $aSub (@subFoldList)
    {
	    if (!($aSub =~ /private/i) and !($classNames[$n] =~ /\./))
	    {
		#if it is a directory of a matlab class
		    if ($classNames[$n] =~ /^\@/)
		    {
			    $class = substr $classNames[$n], 1;
			    #go into recursion
			    print "now entering  $classNames[$n]\n";
			    &treatFolder($aSub, "Class $class");
		    }
		    else
		    {   

			#go in  in any case to see if there is  something
			print "now entering  $classNames[$n]\n";
			    &treatFolder($aSub, ucfirst ($classNames[$n]));
		    }
	    }
	    $n++;
    }
}
