#!/usr/bin/perl
    #
    # This program demonstrates directory listing.
    # It is often used for File Managers, or simply browsing server files.
    #
    # Features: Sort lexically, and seperate folders from files.
    #
    use CGI qw/:standard/;
    
    ## Defining variables:
    ## $dirpath retrieve its value from the input called "path".
    ## @FolderContent, @Folders, @Files are array variables to be used later on.

    ##$dirbase = "/home/homepages/stevenschoir/Notes/";
    $dirbase = "/opt/apache/htdocs/stevenschoir/Notes/";
    $dirend = param('path') if param('path');

    ## Fix for browsing sub folders
    if (index($dirend,"..") > -1){
	 $dirend="";
    }

    $dirpath = "$dirbase/$dirend";

    use vars qw/@FolderContent, @Folders, @Files/;
    
    ## Change current path/folder to $dirpath.

    chdir $dirpath;

    ## Open folder, preparing it for reading.
    unless (opendir DIR,'./') { print "Cannot open directory $dirpath: $!\n"; exit; }
    	## Collect all objects in the folder into an array variable.
    	## Also sort them lexically.
    	@FolderContent = sort readdir DIR;
    closedir DIR;
    
    ## Go through each objects collected.
    foreach $FolderContent(@FolderContent) {
    	## If this object is a folder...
    	if (-d $FolderContent)   {
    		## Put it in the @Folders array variable.
    		push(@Folders, $FolderContent);
    	## If it is not a folder (so it is a file).
    	} else {
    		## Put it in the @Files array variable.
    		push(@Files, $FolderContent);
    	}	
    }

    @dirlist = split("/",$dirpath);
    $lastdir = "";
    foreach $dirlist(@dirlist){
		$lastdir=$dirlist;
	}


    @headerdata;
    $headerfile = "/opt/apache/htdocs/stevenschoir/header.shtml";
    open(HEADERDATAFILE, $headerfile) || die &error_file;
		@headerdata = <HEADERDATAFILE>;
    close(HEADERDATAFILE);

	
	
    
    ## Print header and HTML title (to be viewable in browser).

    print "Content-type: text/html\n\n";
    foreach $headerdata(@headerdata){
	print "$headerdata";
	}

      

    print "\n<div id=\"content\">\n";

    print "\n<div class=\"center\">\n";

    print "\n<a href=\"cgi-bin/dirlistNotes.pl?path=\">Back to the Main Notes Folder</a>\n";
	
    print"\n<table class=\"online_music\">
          <tr>
            <td class=\"top\"> 
              $lastdir
            </td>
          </tr>\n";

    ## Go through each Folders Objects.

	print "<tr> <td>";



    foreach $Folders(@Folders) {
    	## Print out the folders first.


	## Check to Omit . and ..
	if($Folders ne "." && $Folders ne ".."){
	    if($dirend ne ""){
		print "\n<a href=\"cgi-bin/dirlistNotes.pl?path=$dirend/$Folders\"><b>$Folders</b></a><br />\n";
	    }else {
		print "\n<a href=\"cgi-bin/dirlistNotes.pl?path=$Folders\"><b>$Folders</b></a><br />\n";	
	    }
	}




    }
    ## Then go through each Files Objects.
    foreach $Files(@Files) {
    	## Print out the files.
	
	
	print "<a"; 

	if($dirend ne ""){
		print " href=\"Notes/$dirend/$Files\" ";
	}else {
		print " href=\"$Files\" ";	
	}
	
	if(index(lc($Files),".pdf") > -1){
		print " type=\"application/pdf\" ";
	}
	
	if(index(lc($Files),".mus") > -1){
		print " type=\"application/finale\" ";
	}
	
	if(index(lc($Files),".mid") > -1){
		print " type=\"audio/midi\" ";
	}
	
	if(index(lc($Files),".mp3") > -1){
		print " type=\"audio/mp3\" ";
	}

	print ">$Files</a><br />\n";

    }
	
print "     </td>
          </tr>
</table>
</div>
</div>
</body>
</html>";	


sub error_file {
	print "
<HTML>
<HEAD>
	<TITLE>404 File not found</TITLE>
</HEAD>
<BODY>
<H1>404 File not found</H1>
</BODY></HTML>
";
}
