package Apache::TreeBrowser;

use strict;
use Apache::Constants qw(:common REDIRECT);

my $TREE = make_tree();

sub handler {
    my $r = shift;
    my $path_info = $r->path_info;
    my $path_translated = $r->lookup_uri($path_info)->filename;
    my $current_uri = $r->uri;
    unless ($path_info) {
 	$r->header_out(Location => "$current_uri/");
 	return REDIRECT;
    }
    
    $r->content_type('text/html');
    $r->send_http_header;
    return OK if $r->header_only;
    my($junk, @components) = split "/", $path_info;
    
    # follow the components down
    my($node, $name) = ($TREE, '');
    foreach (@components) {
 	last unless $node->{$_};
 	$name = $_;
 	$node = $node->{$_};
    }
    
    $r->print(<<END);
<HTML>
<HEADER>
<TITLE>$node->{-title}</TITLE>
</HEADER>
<BODY BGCOLOR="white">
<H1>$node->{-title}</H1>

Contents = <b>$node->{-contents}</b>

<H2>Navigation Tree</H2>
END
					
    my $prefix = "../" x @components;
    print $prefix ? 
	qq(<H3><A HREF="$prefix">Tree Root</A></H3>\n) : 
 	qq(<H3><FONT COLOR="red">Tree Root</FONT></H3>);
    
    print_node('', $TREE, $node, $prefix);
    print qq(<A HREF="../">Go up one level</A><P>) if $name;
    
    $r->print(<<END);
Node = <EM>$name</EM><br>
URL = <EM>$current_uri</EM><br>
Path information =<EM>$path_info</EM><br>
Translated path = <EM>$path_translated</EM>
</BODY>
</HTML>
END

    return OK;
}

sub print_node {
    my ($name, $node, $current, $prefix) = @_;
    my (@branches) = grep !/^-/, sort keys %$node;
    if ($name) {
 	# print the node itself
 	print $node != $current ?
 	    qq(<LI><A HREF="$prefix$name/">$name</A></LI>\n) :
 		qq(<LI><FONT COLOR="red">$name</FONT></LI>\n);
 	# print branches underneath it
 	$prefix .= "$name/";
    }
    return unless @branches;
    print "<UL>\n";
    foreach (@branches) {
 	print_node($_, $node->{$_}, $current, $prefix);
    }
    print "</UL>\n";
}

# create a sample tree to browse
sub make_tree {
    local $/;
    my $data = <DATA>;
    eval $data;
}

__DATA__
return {
    -title => 'The Root of All Evil',
    -contents => 'And so it begins...',
    'bark' => {
	-title => 'The Wrong Tree',
	-contents => 'His bark was worse than his bite.',
	'smooth' => {
	    -title => 'Like Butter',
	    -contents => 'As smooth as silk.',
	},
	'rough' => {
	    -title => 'Ruffled',
	    -contents => "Don't get rough with me.",
	    'cork' => {
		-title => 'Corked',
		-contents => "Corks don't grow on trees...or do they?",
	    },
	    'cinnamon' => {
		-title => 'The Cinnamon Tree',
		-contents => 'Little bird, little bird in the cinnamon tree...',
	    },
	}
    },
    'bough' => {
	-title => 'Stealing a Bough',
	-contents => "I've taken a bough of silence.",
	'forked' => {
	    -title => 'Forked Boughs',
	    -contents => 'What lightning and snakes tongues have in common.',
	},
	'straight' => {
	    -title => 'Single Boughs',
	    -contents => 'Straight, but not narrow.',
	},
	'extra' => {
	    -title => 'Take a Bough',
	    -contents => 'Nothing beats that special feeling, 
                              when you are stealing that extra bough!',
	},
    },
    'branch' => {
	-title => 'The Branch Not Taken',
	-contents => 'Branch or be branched.',
	'twig' => {
	    -title => 'Twiggy',
	    -contents => 'Anorexia returns!',
	    'twiglet' => {
		-title => 'The Leastest Node',
		-contents => 'Winnie the Pooh, Eeyore, and Twiglet.',
	    },
	},
	'leaf' => {
	    -title => 'Leaf me Alone!',
	    -contents => 'Look back, Leaf Ericksonn.',
	}
    },
}
