die "You need a view context for this!\n" if `cleartool pwv -s` =~ /\*\* NONE \*\*/;

foreach $line (<>)
{
    next unless $line =~ s/Elem (\d+) catalogued in multiple directory elements \((\d+), (\d+)\)/$1,$2,$3/;
    chomp (($elem,$nsdir1,$nsdir2) = split(',',$line));
    push (@{$elem_by_elem{pathname($elem)}},pathname($nsdir1),pathname($nsdir2));
    push (@{$elem_by_nsdir{pathname($nsdir1)}}, pathname($elem));
    push (@{$elem_by_nsdir{pathname($nsdir2)}}, pathname($elem));
}

print "1. By element: listng of multiple parent directories\n\n";
foreach $elem (sort keys %elem_by_elem)
{
    print ("Elem $elem\n");
    foreach $nsdir (@{$elem_by_elem{$elem}})
    {
        print "\t$nsdir\n";
    }
}

print "\n\n\n2. By directory: listng of problem elements\n\n";
foreach $nsdir (sort keys %elem_by_nsdir)
{
    print ("Directory $nsdir\n");
    foreach $elem (@{$elem_by_nsdir{$nsdir}})
    {
        print "\t$elem\n";
    }
}

sub pathname 
{
    return $pathname{$_[0]} if defined $pathname{$_[0]};
    chomp ($pathname{$_[0]} = `cleartool describe -short dbid:$_[0]`);
    $pathname{$_[0]} =~ s/\@\@$/  \(dbid $_[0]\)/;
    return $pathname{$_[0]};
}
