#!/usr/bin/perl


$header1='<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>My All File List</title>';
$header2='<meta name="GENERATOR" content="Microsoft FrontPage 6.0"></head>  <body><p><font color="#FF0000">';

$header3='<font color="#FF0000"><font size="7">All File List</font></p>';

$bottom='</body></html>';

$rootDir;
$counter = 0;

sub ForeachFile
{
	$fileDir  = $_[0];
	$filename = $_[1];
    if  ($filename ne "unPublished.htm" )
    {
		#print "fileDir=$fileDir, filename=$filename\n";
        print '<tr>';
        my $str = '<td width=20>' . $counter . '</td>  <td> <a href="http://www.staroceans.net/'. $fileDir . '">' . $filename . '</a> </td>';
        print $str;
        print '</tr>';
        $counter++;
    }
}


sub ForeachDir
{	
	$dir =  $_[0];	
 	$dirSrc = $rootDir . '/' . $dir;
	if ($dir ne "personal")
	{
		if (opendir(DIR, $dirSrc))
		{
		    #print "openDir $dirSrc\n";
		    while ($file = (readdir(DIR)))
		    {	
		        if ($file ne "." && $file ne "..")
		        {
		          	$subSrc = $dir . '/' . $file;
		            $subDir = $rootDir . '/' . $subSrc; 
		           
		            if (-d $subDir)
		            {					
		                ForeachDir($subSrc);
		            }
		            else
		            {
		                if (-f $subDir)
		                {
		                    ForeachFile($subSrc, $file);                     
		                }
		                else
		                {
		                    print "error unexpected! param= $dir, subSrc=$subSrc, dirSrc=$dirSrc, file =$file, subDir=$subDir\n";
		                }
		            }
		        }
		    }
		    closedir($dh);
		}
	}
}


if ($#ARGV == 0)
{
	$rootDir = $ARGV[0];

	if ( -d $rootDir )
	{
	    print "$header1 $header2 $header3";
	    print '<table border="1" cellpadding="5" cellspacing="5" width="100%">';
	    print '<tr> <th> file index </th>  <th> file name </th>   </tr>';	    

	    if (opendir($dh, $rootDir))
	    {
			foreach my $file (readdir($dh))
			{
				if ($file ne "." && $file ne "..")
				{ 
				    $subDir = $rootDir . '/' . $file;
				    if (-d $subDir)
				    {
				        ForeachDir($file);
				    }
				    else
				    {
				        if (-f $subDir)
				        {		               
				        	ForeachFile($file, $file);		             
				        }
				        else
				        {
				            print "error unexpected! file =$file, subDir=$subDir\n";
				        }
				    }
				}
			}
		}
	    print '</table>';
	    print $bottom;
	}
}

