#!/usr/bin/perl -w
#
# $Revision: 2.3 $ $Date: 2012-07-26 16:54:28-04 $
# $Source: /src/www/sitemap/dtree/RCS/dtree2htm,v $
# $Host: sys7.com $
# $UUID: 40f0e798-4f5d-3be4-89fb-b26006be8901 $
#
#<dtree2htm: accept find output, write a simple HTML filetree
# Adapted from dtree sed script:
#    s,^.$,,
#    /^$/d
#    s,[^/]*/\([^/]*\)$,+--\1,
#    s,[^/]*/,|   ,g

use strict;
use URI::Escape;

my $file;
my $orig;
my $safe;
my $url;

print "<pre>\n";

while (<>)
{
    chomp;
    next unless length;
    $orig = $_;

    # Handles the directory part of the filename.

    if (m![^/]*/([^/]*)$!) {
        $file = $1;
        s,[^/]*/([^/]*)$,+--[#FILE#],s;
    }
    else {
        next;
    }

    # Handles the base part of the filename.  After this, $_ holds the
    # indentation plus basename and $orig holds the complete path.

    s ,[^/]*/,|   ,sg;

    # Safely encode the filename, keeping slashes.  We could kill the
    # leading "./", but that should be for the input to handle.

    $safe = uri_escape("$orig");
    $safe =~ s!\%2F!/!g;

    $url = "<a href=\"$safe\">$file</a>";
    s!\[#FILE#\]!$url!;
    print "$_\n";
}

print "</pre>\n";
exit (0);
