#!/usr/bin/perl

use strict;
use warnings;

use Debian::Debhelper::Dh_Lib;
use Debian::PkgJs::Utils;
use Debian::PkgJs::Version;

my $DOCFILES =
  qr/^(?:(?:contributing|security)\.md|readme(?:\.m(?:arkdown|d))?)$/i;

my $CHANGELOGS = qr/^(?:change(?:log(?:\.md)?|s(?:\.md)?)|history(?:\.md)?)$/i;

init();

exit 0 if $ENV{DEB_BUILD_OPTIONS} and $ENV{DEB_BUILD_OPTIONS} =~ /\bnodoc\b/;

my $components = root_components_list() // {};
my ( $main, $docFiles );
{
    opendir my $dir, 'debian';
    unless ( -e 'debian/docs'
        or grep { /(?:^|\.)docs$/ } readdir $dir )
    {
        $main = pjson( main_package() )->{name};
    }
    closedir $dir;
}

if ( -e 'debian/nodejs/docs' ) {
    $docFiles = getDocFiles('debian/nodejs/docs');
}
else {
    $docFiles = getDocFiles();
}
install_doc_files($docFiles);

sub getDocFiles {
    my ($fixedList) = @_;
    my $res;
    if ($fixedList) {
        my @pList = open_file($fixedList);
        foreach my $pattern (@pList) {
            my @list = glob($pattern);
          D: foreach my $docFile (@list) {
                foreach my $cmp ( keys %$components ) {
                    $cmp =~ s#/+$##;
                    if ( $docFile =~ m#^\Q$cmp\E/# ) {
                        push @{ $res->{ $components->{$cmp} } }, $docFile;
                        last D;
                    }
                }
                push @{ $res->{$main} }, $docFile if $main;
            }
        }
    }
    else {
        my %cmps = %$components;
        $cmps{ main_package() } = $main if $main;
        foreach my $cmp ( keys %cmps ) {
            $cmp =~ s#/+$##;
            my $dir;
            opendir( $dir, $cmp ) or next;
            my @changelogs;
            push @{ $res->{$cmp} }, map { "$cmp/$_" } grep {
                push @changelogs, $_
                  if $_ =~ $CHANGELOGS;
                $_ =~ $DOCFILES
            } readdir $dir;
            if ( $cmp ne $main and @changelogs ) {
                push @{ $res->{$cmp} }, map { "$cmp/$_" } @changelogs;
            }
        }
    }
    return $res;
}

sub install_doc_files {
    my ($tree) = @_;
    my $pkg = $ARGV[0];
    unless ($pkg) {
        my @pkgs = grep { /\w/ } getpackages();
        if ( @pkgs != 1 ) {
            @pkgs = grep { /^node-/ } @pkgs;
            if ( @pkgs != 1 ) {
                die "Set package as argument of $0";
            }
        }
        $pkg = $pkgs[0];
    }
    my $tmp = "debian/$pkg/usr/share/doc/";
    doit( qw(mkdir -p), $tmp ) unless -e $tmp;
    foreach my $cmp ( keys %$tree ) {
        my @docFiles = @{ $tree->{$cmp} };
        my $vpkg     = 'node-' . normalize_name($components->{$cmp}||$main);
        doit( 'mkdir', "$tmp/$vpkg" ) if !-e "$tmp/$vpkg";
        foreach my $doc (@docFiles) {
            if ( -d $doc ) {
                print_and_doit( qw(cp -a --reflink=auto), $doc, "$tmp/$vpkg/" );
            }
            else {
                print_and_doit( qw(install -m 644), $doc, "$tmp/$vpkg/" );
            }
        }
        if($cmp ne main_package) {
            print_and_doit( qw(ln -s), "../$pkg/copyright", "$tmp/$vpkg/" )
        }
    }
}
__END__
=pod

=head1 NAME

dh_nodejs_autodocs - automatically install components doc files

=head1 SYNOPSIS

  override_dh_installdocs:
      dh_installdocs
      dh_nodejs_autodocs

For multiple binary packages:

  override_dh_installdocs:
      dh_installdocs
      dh_nodejs_autodocs node-foo

=head1 DESCRIPTION

dh_nodejs_autodocs automatically install README.md, CONTRIBUTING.md,... for
each root component in its C</usr/share/doc/node-name> directory.

If no C<debian/*docs> is found, it does the same for the main component.

=head1 COPYRIGHT AND LICENSE

Copyright Yadd E<lt>yadd@debian.orgE<gt>

Licensed under GPL-2+ (see /usr/share/common-licenses/GPL-2)

=cut
