#!/usr/bin/env perl
# ptags_sort: sort the ptags file according to @INC order
#
# If you have a module both under development in your $PROJROOT as well as
# installed system-wide, you most likely want tags to point to the locally
# installed version. A normal sort(1) would sort lines with the same tag
# according to the path sort order, since the path is the second field.
# So here we sort according to @INC order, which is most likely the order you
# want the modules to be found.
use warnings;
use strict;
our $VERSION = '0.03';

sub inc_order {
    my $path  = shift;
    my $order = 0;
    for my $candidate (our @inc) {
        $order++;
        return $order if index($path, $candidate) == 0;
    }
    $order;
}
our @inc = ($ENV{PROJROOT}, grep { !ref } @INC);
my %seen;
while (<>) {
    chomp;
    $seen{$_} = [ split /\t/ ];
}
printf "%s\n", join "\t" => @$_
  for sort { $a->[0] cmp $b->[0] || inc_order($a->[1]) <=> inc_order($b->[1]) }
  values %seen;
