# This file is part of the graph-includes package
#
# (c) 2005 Yann Dirson <ydirson@altern.org>
# Distributed under version 2 of the GNU GPL.

package graphincludes::project::uniqueincludes;
use strict;
use warnings;

use base qw(graphincludes::project::default);

use File::Spec::Functions qw(splitpath);

use graphincludes::params;

sub filelabel {
  my $self = shift;
  my ($file) = @_;
  $file =~ s/^$self->{PFXSTRIP}// if defined $self->{PFXSTRIP};
  (undef, my $dirname, my $filename) = splitpath($file);
  $filename =~ s!\.[^.]*$!!;	# strip suffix
  return $filename;
}

sub getdeps {
  my $self = shift;
  my ($graph) = @_;

  @ARGV = map {$_->{LABEL}} $graph->get_nodes();
  while (<>) {
    if (m!^\s*#\s*include\s*[<"](.+)[>"]!) {
      my $dstfile = $1;

      # strip dirs
      (undef, undef, my $dstnode) = splitpath($dstfile);

      $dstnode =~ s!\.[^.]*$!!;        # strip suffix

      if (grep { $self->filelabel($_->{LABEL}) eq $dstnode } $self->{GRAPHS}[0]->get_nodes) {
	$self->{GRAPHS}[0]->record_edge ($ARGV, $dstnode);
      } else {
	$self->record_missed_dep ($ARGV, $dstfile);
      }
    }
  }
}

1;
