package Dev2;

use strict;

# -----

sub directory_pulldown ($$)
{
  my ($q, $whereami) = @_;

  if (!open(PULL, '<contriblist')) {
    warn "contriblist: $!\n";
    return '';
  }

  my (@list, %labels, @prev);
  my $prev = '';
  push(@list, '/');
  while (my $dir = <PULL>) {
    chomp($dir);
    next if ($dir =~ m!/OLD$!);
    $dir = '/' . $dir . '/';
    my $dirsave = $dir;
    push(@list, $dir);

    my $levels = ($dir =~ tr!/!/!) - 2;
    $dir =~ s!^.*/([^/]+)/$!'&nbsp;' x ($levels * 3) . "/$1"!e;
    $labels{$dirsave} = $dir;
  }
  close(PULL);

  $whereami =~ s!/OLD/$!/!;

  my $ae = $q->autoEscape(0);
  my $text = $q->div({ -align => 'right' }, join('',
	$q->start_form( -method => 'GET', -action => '/contrib' ),
	$q->popup_menu( -name => 'jump', -values => \@list, -labels => \%labels, -default => $whereami ),
	$q->submit( -value => 'Jump' ),
	$q->end_form
	));
  $q->autoEscape($ae);
  return $text;
}

# -----

sub commify ($)
{
  my $text = reverse $_[0];
  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  return scalar reverse $text;
}

# -----

sub parse_index ($$)
{
  my ($index_name, $filename) = @_;
  my %index;

  if (!open(INDEX, "<$filename")) {
    warn "$filename: $!\n";
    return ();
  }

  my $dir = $filename;
  $dir =~ s![^/]+$!!;

  my ($flag, $name, $line) = (0, '', '');
  while (<INDEX>) {
    next if (/^\s*$/);
    if (/^([^\s]+)\s+([^\s].+)$/) {
      my ($tname, $tline) = ($1, $2);
      $index{$name} = $line if ($name && $name ne $index_name && (not $name =~ /\.README$/) && -e "$dir$name");
      $name = $tname;
      $line = $tline;
    } elsif (/^\s+([^\s].+)$/) {
      $line .= ' ';
      $line .= $1;
    }
  }
  $index{$name} = $line if ($name && $name ne $index_name && (not $name =~ /\.README$/) && -e "$dir$name");
  close(INDEX);

  return \%index;
}

# -----

1;
