#!/usr/bin/perl -w # # $Revision: 1.4 $ $Date: 2009/11/03 02:55:20 $ # $UUID: 9e643f19-60d3-3d8b-9f9b-26eb0c259d16 $ # # read output from "ls -lR $PWD", write something resembling a sitemap. use File::Basename; use POSIX qw(strftime); use Fcntl ':mode'; use strict; my $myname = basename($0); $myname =~ s/\.\w*$//; # strip any extension my $docroot = '/doc/html/htdocs'; my $dir; my $file; my $path; # full pathname my $href; my $ftype; my $k; my ($mode, $links, $usr, $group, $size, $day, $when); # Header print <<'EndHeader'; Sitemap
EndHeader

# TOC
while (<>) {
    chomp;
    next if /^total/;

    # blank line?
    if (length() == 0) {
        print "\n";
        next;
    }

    # is this a directory name at the top of a list?
    if (m!^(/.*):$!) {
        $path = $dir = $1;
        $dir =~ s!$docroot!!;
        $ftype = filetype("$path");
        print "$ftype $dir\n";
    } 

    # is this a regular file listing with a pipe symbol?
    # -rw-r--r-- 1 user user 11936 17-Dec-1996 19:02:39| file...
    else {
        $k = index($_, '|');
        ($mode, $links, $usr, $group, $size, $day, $when) =
            split(/\s+/, substr($_, 0, $k));
        $file = substr($_, $k+2);
        $ftype = filetype("$path/$file");
        $href = "$file";

        # print everything
        ## printf "%10s %3d %8s %8s %8d %11s %8s %s\n",
        ##     $mode, $links, $usr, $group, $size, $day, $when, $href;

        # print just size, date, and link.
        ## printf "  %s %8d %11s %8s %s\n", $ftype, $size, $day, $when, $href;

        # print just filetype and link.
        print "  $ftype $href\n";
    }
}

# Footer
$_ = strftime("%a, %d %b %Y %T %z", localtime(time()));
my $vers = version();

print <<"EndFooter";

Generated by $vers $_
EndFooter exit(0); # ------------------------------------------------------------------------- # return a version string. sub version { my $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); return "$myname $VERSION\n"; } # ------------------------------------------------------------------------- sub filetype { my ($path) = @_; my ($mode) = (lstat($path))[2]; return '-' unless $mode; # in case stat failed S_ISDIR($mode) && return '[DIR]'; S_ISREG($mode) && return '[TXT]'; return '[UNK]'; }