#!/usr/bin/perl -w

use strict;

my $Autor = 'Tjabo Kloppenburg <tklopp@gmx.de>';
my $Zweck = 'Verzeichnistree mit allen Zweigen anzeigen';
my $Datum = '2000-August-04, zwischen 00:00 und 01:20';
my $Usage = "tree.pl [starting_dir]\n";

# --------------------------------------------------------

my $maxebenen = 200;

my $some_dir = $#ARGV >= 0 ? $ARGV[0] : ".";
$some_dir =~ s/\/$//g;

if (! -d "$some_dir") {
    print "\n";
    print " Autor: $Autor\n";
    print " Zweck: $Zweck\n";
    print " Datum: $Datum\n";
    print " Usage: $Usage\n";
    exit;
}

if (!($some_dir =~ /^\/.*/)) {
    chomp(my $cwd = `pwd`);
    $some_dir = "$cwd/$some_dir";
}
print "Zeige Tree von $some_dir:\n";

print "\n";
ScanDir("$some_dir",0);
print "\n";

exit 0;

# --EOP-----------------------------------------------------------------
# Rekursiv aufgerufenes Unterprogramm:

sub ScanDir{
    my ($path,$ebene) = @_;
    $ebene++;
    my  @dirs;

    opendir(DIR, "$path") || die "can't opendir $path: $!";
    @dirs = grep { /[^\.]{1,2}/ && (-d "$path/$_") } readdir(DIR);
    closedir DIR;

    if ($#dirs == -1) {
        return;
    }
    else {
        foreach my $ele (@dirs) {
            if ($ebene > 1) {
                print " " x (4 * ($ebene - 1)) . "|-- ";
            }
            
            if (! ( -l "$path/$ele")) {
                # print "$ele ($ebene)\n";
                print "$ele\n";
                ScanDir("$path/$ele", $ebene);
            }
            else {
                print "$ele (Link)\n";
            }
        }
    }
}

# --EOP-----------------------------------------------------------------
# Ich bin Zeile Nummer 72.