#!/usr/bin/perl -w

=head1 NAME

dh_yang - Track YANG import dependencies

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;

init();

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmp=tmpdir($package);
    my $yang_files=`find $tmp -name *.yang`;

    ## TODO - Use proper yang parser for dependencies
    ## Check Yang compiles!?
    my @yang_deps;
    foreach my $file (split /\n/, $yang_files) {
	my $import_lines = `grep "import " $file`;
	foreach my $line (split /\n/, $import_lines) {
	    if ($line =~ /^\s+import\s(\S+)\s+{/) {
		push @yang_deps, "$1-yang";
	    }
	}
    }

    addsubstvar($package, "yang:Depends", join(", ", @yang_deps));
}
