#!/usr/bin/perl -w
#
# Name: dir2ndx.prl
# Param 1 : "DIR"
#
# lists DIR, sorts, writes to INDEX.HTML filename + size + date
# OS-undependent
#
# by Gerd Hoffmann (C)
#
# 081026
#
# todo: Diretory in Listing als solches kennzeichnen
#

use strict;

#################################################
#      !!!    Vars zu aktualisieren     !!!
#################################################
#
my $Author = "Gerd Hoffmann" ;
#my $relPath = "/";    # relative Path: where CSS-File and home file is found
my $relPath = "/../../";    # relative Path: where CSS-File and home file is found
my $Back = "home.html";        # parent file == back file == home file
my $Formate = "formate.css";   # CSS-File
my $Index = "index.html";      # File to create
my $ThisTitle ="Index";
my $ThisFile  ="dir2ndx.prl";
#
#################################################

# uebrige Vars
my ( $i, $dir, $fname, @filelist, $filename, $size, $Sizes, $Count, $mtime );
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
$year += 1900;
$mon += 1;
my $Year = $year;
my $Modified = "$year-$mon-${mday}T$hour:$min:$sec MEZ";

# $dir = "D:\\Eigene Dateien\\perld\\html\\index\\SubDir"  ;
if ( ! defined ( $dir = $ARGV[0] ) ) { $dir = "." } ;
( -d $dir || die "Abort! \"$dir\" does not exist !\n" ) ; 

opendir(IN, $dir) || die "no $dir?: $!";
foreach  $fname (sort (readdir(IN)) ) {
   if ( ($fname =~ /^\.\.?$/ ) || ( $fname =~ $Index ) ) { next } ;
#   $fname = fitfilename( $fname ) ;
   (@filelist) = ( @filelist, $fname );
}
closedir(IN);

open(OUT, ">$Index") || die "Cannot open ${Index}! Abbruch !\n" ;
PrintIndexFirst();
$Count = 0;
$Sizes = 0; 
foreach $fname ( @filelist ) {
   $filename = "$dir\\$fname" ;
   ($size, $mtime) = ( stat( $filename ) )[7,9];
   ($sec, $min, $hour, $mday, $mon, $year) = localtime($mtime);
   print "$fname !1!\n";
   # if ( $fname eq $Index ) { print "$fname !!!\n"; next; } # 'index.html' wird nicht angezeigt, da geoeffnet zum Schreiben
   if (-d $filename) { $size = "&lt;DIR&gt;"; }  else { $Sizes += $size } ;
   my $date = sprintf ( "%02i.%02i.%04i %02i:%02i\n", $mday, $mon+1, $year+1900, $hour, $min ) ;
   PrintIndexRow( $fname, $size, $date );
   ++$Count;   
}

PrintIndexLast() ;

# # #   end main


################################
# PrintIndexFirst()
#
sub PrintIndexFirst {

print OUT <<IndexFirst;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="GENERATOR" content="$ThisFile">
<meta name="Author" content="$Author">
<meta name="date" content="$Modified">
<!-- <meta name="robots" content="noindex"> -->
<meta name="keywords" lang="en" content="Index">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
  <!-- ... Link !  ... -->
<link rel="stylesheet" media="all" href="$relPath$Formate">
<title>$ThisTitle</title>
</head>

<body>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<!-- <td><a href="${relPath}$Back">back</a></td> -->
</tr>
</table>
<p>
<table width="96%" cellpadding="2" cellspacing="0" border="0">
   <tr>
   <th align="left">File-Name</th>
<!--   <th align="left">Size&nbsp;/&nbsp;Dir</th>  -->
   <th align="right">Size&nbsp;&nbsp;&nbsp;</th>
   <th align="left">Date</th>
   </tr>
IndexFirst
# Ende HERE-Dokument
}


################################
# PrintIndexRow()
#
sub PrintIndexRow {
#my $CutName = substr( $LongFileName, 0, 14 );
my ( $LongFileName, $FileSize, $FileDate ) = @_ ;
print OUT <<IndexRow;
   <tr>
   <td align="left" valign="top"><a href="$LongFileName">$LongFileName</a></td>
   <td align="right" valign="top">${FileSize}&nbsp;&nbsp;&nbsp;</td>
   <td align="left" valign="top">$FileDate</td>
   </tr>
IndexRow
# Ende HERE-Dokument
#   <td align="left" valign="top"><a href="$LongFileName">$CutName</a></td>
}


################################
# PrintIndexLast()
#
sub PrintIndexLast {
print OUT <<IndexLast;
</table>
<p>
$Count Files with total size of $Sizes Bytes
</p>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<!-- <td><a href="${relPath}$Back">back</a></td> -->
</tr>
</table>
<p>
<hr>
(C)opyright by $Author $Year
</p>
</body>
</html>
IndexLast
# Ende HERE-Dokument
}


################################
# ltrim()
#
sub ltrim {
   $_[0] =~ /^\s*(.*?)$/ ;
   return ( $1 );
}


################################
# rtrim()
#
sub rtrim {
   $_[0] =~ /^(.*?)\s*$/ ;
   return ( $1 );
}


################################
# rpad()
#
sub rpad {
   return ( $_[0] . " " x ( $_[1] - length( $_[0] )) );
}

__END__


# fits filenames, changes forbidden letters
#sub fitfilename {
#   my $fname = $_[0] ;;  
#   $fname =~ s/Ä/AE/g ;
#   $fname =~ s/ä/ae/g ;
#   $fname =~ s/Ö/OE/g ;
#   $fname =~ s/ö/oe/g ;
#   $fname =~ s/Ü/UE/g ;
#   $fname =~ s/ü/ue/g ;
#   $fname =~ s/ß/ss/g ;
#   $fname =~ s/[\s\$!~+'`´#&]/_/g ; # no err   
#   return $fname ;
#}



