#!/usr/bin/perl

use Modern::Perl;

sub global_nav_gen {
    my $baseuri = pop;
    $baseuri = "" unless($baseuri);
    my $nav = '';
    if ( -f "order.txt" ) {
        open (my $fh, "<", "order.txt");
        while (<$fh>) {
            chop;
            my ($link, $linktext ) = split;
            $linktext = $link unless($linktext);
            $nav .= "[" . $baseuri . "/" . $link . "](" . $linktext . ")\n";
            if ( -d $link && -r "$link/order.txt" ) {
                chdir ($link);
                $nav .= global_nav_gen($baseuri . "/" . $link);
                chdir ("..");
            }
        }
    }
    return $nav;
}

print global_nav_gen();
