#!/usr/bin/perl
#==========================================#
# Author: Marius Feraru aka AltBlue #
# E-Mail: altblue@tuiasi.ro #
# URL : http://www.cs.tuiasi.ro/altblue/ #
# http://bofh.mednet.ro/altblue/ #
#==========================================#
# v0.3
$index = 'index.html';
$version = '0.3';
$filemode = 0644;
$dirmode = 0755;
unless($ARGV[0] && -d $ARGV[0] && -W $ARGV[0]) {
print <
ERROR: directory not found or not writeable.
Usage:
[1m$0[0m [[1mv[0m][[1mr[0m][[1mf[0m]
[1mv[0m: verbose
[1mr[0m: recursive
[1ms[0m: set proper public modes for files (644) and directories (755)
[1mf[0m: force (ignores old index.html files! [1muse w/ care![0m)
USAGE
exit 1;
}
$verbose = $ARGV[1] =~ /v/ ? 1 : 0;
$recursive = $ARGV[1] =~ /r/ ? 1 : 0;
$force = $ARGV[1] =~ /f/ ? 1 : 0;
$chmod = $ARGV[1] =~ /s/ ? 1 : 0;
$directory = $ARGV[0];
verbose("[1mHTML Indexer[0m [1mv.$version[0m by [1mAltBlue[0m ");
#### a 644 rights for index files would be proper i suppose ;->
umask 0113;
if($recursive) {
abhindex($directory);
abtree($directory);
} else {
abhindex($directory);
}
exit 0;
#### Routines....
#### The Real Indexer ;->
sub abhindex {
local $dir = $_[0];
local $totalsize = 0;
local @files=();
local @dirs=();
local @attr=();
local $decoder;
if (-f "$dir/$index") {
if($force) {
verbose("'$dir/$index' already exists. overwriting.");
} else {
verbose("'$dir/$index' already exists. skipping.");
return undef;
}
}
chmod $dirmode, "$dir" if $chmod;
if(opendir(DIR, $dir)) {
verbose("'$dir' succesfully opened. Processing...");
foreach (readdir(DIR)) {
next if $_ eq '.' || $_ eq '..' || $_ eq $index;
if(-d "$dir/$_") {
push(@dirs,$_);
} else {
push(@files,$_);
}
}
closedir(DIR);
} else {
verbose("ERROR: couldn't open '$dir' : $!");
return undef;
}
if(open(INDEX,">$dir/$index")) {
verbose("'$dir' processing completed. writing index file.");
print INDEX "\n\n\tAltBlue Indexer: $dir\n\n\n\n\t$dir index
\n\t
\n\n\n\n\t
\n";
foreach(@dirs) {
$decoder = abdecode($_);
print INDEX "\t\t- $_ [DIR]\n";
}
foreach(@files) {
chmod $filemode, "$dir/$_" if $chmod;
$decoder = abdecode($_);
@attr = stat("$dir/$_");
$totalsize += $attr[7];
print INDEX "\t\t
- $_ [$attr[7] bytes]\n";
}
local $nofiles = $#files + 1;
print INDEX "\t
\n
Total: $nofiles files - $totalsize bytes
\n\n\n\t
\n\n\n\t
\n\t\t
\n\t\t\t\n\t\t\t\tThis page was produced by a web utility engineered by \n\t\t\t\tAltBlue <altblue\@tuiasi.ro>\n\t\t\t\n\t\t\n\t
\n\n\n\n";
close(INDEX);
verbose("'$dir/$index' was written.");
} else {
verbose("ERROR: couldn't open index file : $!");
return undef;
}
}
##### Traversing the directory structure....
sub abtree {
local($dir) = shift;
local($path);
unless (opendir(DIR, $dir)) {
warn "Can't open $dir\n";
closedir(DIR);
return;
}
foreach (readdir(DIR)) {
next if $_ eq '.' || $_ eq '..';
$path = "$dir/$_";
if (-d $path) {
abhindex($path);
abtree($path);
}
}
closedir(DIR);
}
sub verbose {
print "@_\n" if $verbose;
}
###### Decode strange characters
sub abdecode {
local $string = $_[0];
$string =~ s/ /%20/g;
$string =~ s/\?/%3F/g;
$string =~ s/&/%26/g;
$string =~ s/:/%3A/g;
$string =~ s/\+/%2B/g;
return $string;
}