#!/usr/bin/perl
#
# $Revision: 1.2 $ $Date: 2025-12-12 06:59:45-05 $
# $Source: /home/vogelke/projects/html-dir/RCS/dir2web,v $
# $Host: furbag.home.arpa $
# $UUID: 15a85598-4bc3-44c7-83c0-18789b54d7ea $
#
#<dir2web: HTML equivalent of dtree sed script
#          usage: find . -print | sort | dir2web
#
# ORIGINAL SED:
#    s,^.$,,
#    /^$/d
#    s,[^/]*/\([^/]*\)$,+--\1,
#    s,[^/]*/,|  ,g

use Modern::Perl;
use URI::Escape;
use File::Basename;

my $myname = basename($0);
$myname =~ s/\.\w*$//;    # strip any extension

my $rcsinfo = version();
print "<!-- Generated by $rcsinfo -->\n";
print "<pre>\n";

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

    # Handles the directory part of the filename.

    my $file;
    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.

    my $safe = uri_escape("$orig");
    $safe =~ s#\%2F#/#g;
    $safe =~ s#^\./##;

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

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

# -------------------------------------------------------------------------
sub version {
    use version; our $VERSION = qv((qw$Revision: 1.2 $)[-1]);
    my $DATE = join(" ", (qw$Date: 2025-12-12 06:59:45-05 $)[-2, -1]);
    return "$myname $VERSION $DATE";
}
