#!/usr/bin/perl
#
# $FreeBSD$
#
# Written to speed-up INDEX file generation.  The new scheme
# basically visits each port once and writes out each port's
# build-depends and run-depends as a list of directories.  This
# script goes back in and maps the directories back to pkgnames,
# fixes up the build-depends and run-depends list, and writes
# out the new INDEX file.

require 5.002;

# Helper function to map a directory to a pkgname.
sub by_path {
    my ($name, $port) = @_;

  # If a direct mapping exists, then use it.
    return $by_path{$name} if (defined $by_path{$name});

  # Make sure we have /usr/ports at the beginning.
    $name =~ s!^$pwd!/usr/ports!o;
    return $by_path{$name} if (defined $by_path{$name});

  # Collapse all the '..' sequences.
    my @f = split('/', $name), @p = ();
    foreach (@f) { (/\.\./) ? pop(@p) : push(@p, $_); }
    $name = join('/', @p);
    return $by_path{$name} if (defined $by_path{$name});

    print STDERR "make_index: $port: no entry for $name\n";
    return undef;
}

# This routine replaces what used to be the time-consuming
# recursive 'depends-list' and 'package-depends' targets.
sub recurse {
    my $pkg = shift(@_);
    return if $pkg->{checked};

  # build-depends = build-depends + recursive list of run-depends
  #     for each build-depends
    my @deps = ();
    foreach $name (@{$pkg->{bdep}}) {
        recurse($index{$name});
	push(@deps, @{$index{$name}->{rdep}});
    }    
    $pkg->{bdep} = uniqify(@{$pkg->{bdep}}, @deps);

  # same as above except for run-depends this time
    @deps = ();
    foreach $name (@{$pkg->{rdep}}) {
	recurse($index{$name});
	push(@deps, @{$index{$name}->{rdep}});
    }
    $pkg->{rdep} = uniqify(@{$pkg->{rdep}}, @deps);
    $pkg->{checked} = 1;
}

# Given one or more lists as arguments return the set
# of unique elements among them.
sub uniqify {
    my %seen = ();
    my @unique = grep {! $seen{$_}++} (@_);
    return \@unique;
}

# Save where we are so that we can map all directories formed
# from ${PORTSDIR} to their canonical location '/usr/ports/...'.
chomp($pwd = `pwd`);

# Read each line of output generated by the 'index' target.
while (<>) {
    chomp;
    my @f = split(/\|/);

  # Force to canonical form.
    $f[1] =~ s!^$pwd!/usr/ports!o;
    $f[4] =~ s!^$pwd!/usr/ports!o;

  # Save directory -> pkgname relationship.
  # Note: $f[0] gets clobbered by the splice below so we'll save
  # it to a new $name first.
    $by_path{$f[1]} = $name = $f[0];

  # Create a hash table of the infomation we need about this port.
    my $pkg = {
	'bdep'		=> [split(/ /, $f[7])],
	'rdep'		=> [split(/ /, $f[8])],
	'rest'		=> join('|', splice(@f, 9)),
	'text'		=> join('|', splice(@f, 0, 7))
    };
    $index{$name} = $pkg;

  # This is a cheap way of preserving the order of the entries.
    push(@names, $name);
}

# For each port perform the mapping between directory and pkgnames.
foreach $name (keys %index) {
    my $pkg = $index{$name};
  # first the build dependencies
    if (@{$pkg->{bdep}}) {
	my @bdep = map { by_path($_, $name) } @{$pkg->{bdep}};
	$pkg->{bdep} = \@bdep;
    }
  # and now the run dependencies
    if (@{$pkg->{rdep}}) {
	my @rdep = map { by_path($_, $name) } @{$pkg->{rdep}};
	$pkg->{rdep} = \@rdep;
    }
}

# With all that done we're finally ready to write out the new
# INDEX file one port at a time.
foreach $name (@names) {
    my $pkg = $index{$name}; 
    if (exists $pkg->{'PRINTED'}) {
        print STDERR "Warning: Duplicate INDEX entry: $name\n";
    } else {
	recurse($pkg);
	print "$pkg->{text}|";
	print join(' ', sort(@{$pkg->{bdep}})) if @{$pkg->{bdep}};
	print "|";
	print join(' ', sort(@{$pkg->{rdep}})) if @{$pkg->{rdep}};
	print "|$pkg->{rest}\n";
	++$pkg->{'PRINTED'};
    }
}
2'>dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron6/files/node-fetch-2.6.1'>dependabot/npm_and_yarn/devel/electron6/files/node-fetch-2.6.1</option>
<option value='dependabot/npm_and_yarn/devel/electron6/files/serve-10.1.2'>dependabot/npm_and_yarn/devel/electron6/files/serve-10.1.2</option>
<option value='gnome-3.22'>gnome-3.22</option>
<option value='gnome-3.24'>gnome-3.24</option>
<option value='gnome-3.26'>gnome-3.26</option>
<option value='gnome-3.28'>gnome-3.28</option>
<option value='gnome-3.32'>gnome-3.32</option>
<option value='gnome-3.36'>gnome-3.36</option>
<option value='gstreamer'>gstreamer</option>
<option value='gstreamer-1.16'>gstreamer-1.16</option>
<option value='gstreamer0.10-removal'>gstreamer0.10-removal</option>
<option value='main'>main</option>
<option value='master'>master</option>
<option value='mate-1.16' selected='selected'>mate-1.16</option>
<option value='mate-1.18'>mate-1.18</option>
<option value='mate-1.20'>mate-1.20</option>
<option value='mate-1.22'>mate-1.22</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD GNOME current development ports (https://github.com/freebsd/freebsd-ports-gnome)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/about/?h=mate-1.16'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/?h=mate-1.16'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/refs/?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang/slib/Makefile?h=mate-1.16'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/tree/lang/slib/Makefile?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/lang/slib/Makefile?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/diff/lang/slib/Makefile?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/stats/lang/slib/Makefile?h=mate-1.16'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang/slib/Makefile'>
<input type='hidden' name='h' value='mate-1.16'/><input type='hidden' name='id' value='411336584c9b8781ac886204181693547b679103'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>lang</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang/slib?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>slib</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang/slib/Makefile?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103'>Makefile</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/lang/slib/Makefile?h=mate-1.16&amp;id=411336584c9b8781ac886204181693547b679103&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/lang/slib/Makefile?h=mate-1.16&amp;id=548fb3aa90c3aa083ababe735f40362612ceb3f3'>- Fix path in guile.init. Fixes lang/slib-guile installation</a></td><td>pav</td><td><span title='2007-01-25 00:19:10 +0800'>2007-01-25</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+2</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/lang/slib/Makefile?h=mate-1.16&amp;id=8c499db0fe595383ba8b2370f12a6090a3527a6f'>- Update to 3a4</a></td><td>clsung</td><td><span title='2007-01-16 18:26:17 +0800'>2007-01-16</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/lang/slib/Makefile?h=mate-1.16&amp;id=3ac259ba37b471f2b32cb1a50dba3d2daa1db705'>- Update to 3a3</a></td><td>mnag</td><td><span title='2006-02-20 23:18:21 +0800'>2006-02-20</span></td><td>1</td><td><span class='deletions'>-11</span>/<span class='insertions'>+11</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/lang/slib/Makefile?h=mate-1.16&amp;id=59628af44ccbc6b022b4eefac64f1121bd1b4cd9'>- Update to 3a2</a></td><td>tdb</td><td><span title='2006-02-06 21:54:32 +0800'>2006-02-06</span></td><td>1</td><td><span class='deletions'>-2</span>/<span class='insertions'>+1</span></td></tr>