package GimpSite::Util;

use strict;
require IO::File;
# require GimpSite;

sub Settings ($$) {
    my ($fh, $args) = @_;

    my $next = '';

    while (my $line = <$fh>) {
        chomp $line;
        if ($line =~ /^--USE--\s*(.*)$/) {
            $next = $1;
            last;
        }

        if ($line =~ /([:\.])=/) {
            my $act = $1;
            my ($key, $value) = split (/\s*.=\s*/, $line, 2);
            if ($act eq ':') {
                $args->{$key} = $value;
            } elsif ($act eq '.') {
                $args->{$key} .= $value;
            }
        }
    }

    return $next;
}

sub Navtree ($$$@) {
    my $page = shift;
    my $node = shift;
    my $level = shift;
    my @pathparts = @_;

  # how to get to root - ICK!
    my $up = '../' x ((scalar @pathparts)
                    - ($pathparts[$#pathparts] =~ /\.html$/ ? 1 : 0)
                    - ($node->{'path'} eq '' ? 1 : 0));

    my $path = $up.$node->{'path'};
    $path =~ s/^$/./; # current dir
    $path .= '/' unless $path =~ /\.html$/; # handle directories

    my $next = shift @pathparts || '';

    $path =~ s/\/+/\//g;

    my $ninfo = {
        path => $path,
        text => $node->{'tpart'},
        used => 0,
        here => 0,
        level => $level,
    };

    my $nav = '';

    if ($node->{'path'} eq $next) {
        $ninfo->{'used'} = 1;
        $ninfo->{'here'} = 1 unless scalar @pathparts;

        $nav .= $page->render_cmd ( '-nav_link-', $ninfo );

        $level++;

        foreach my $child (@{$node->{'children'}}) {
            $nav .= &Navtree ($page, $child, $level, @pathparts);
        }

    } else {
        $nav .= $page->render_cmd ( '-nav_link-', $ninfo );
    }

    return $nav;
}

1;

#EOF
