#!/usr/bin/perl -w
# file-info -- lintian collection script

# Copyright © 1998 Richard Braakman
# Copyright © 2019 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::coll::file_info;

no lib '.';

use strict;
use warnings;
use autodie;

use IO::Async::Loop;
use IO::Async::Process;
use Path::Tiny;
use Try::Tiny;

use lib "$ENV{'LINTIAN_ROOT'}/lib";

use Lintian::Collect;
use Lintian::Util qw(locate_helper_tool);

use constant EMPTY => q{};
use constant COLON => q{:};
use constant NEWLINE => qq{\n};

sub collect {
    my ($pkg, $type, $dir) = @_;
    my $info = Lintian::Collect->new($pkg, $type, $dir);

    chdir("$dir/unpacked");

    my $loop = IO::Async::Loop->new;

    my @generatecommand = (
        'xargs', '--null','--no-run-if-empty', 'file',
        '--no-pad', '--separator',EMPTY, '--print0','--'
    );
    my $generatedone = $loop->new_future;

    my $generate = IO::Async::Process->new(
        command => [@generatecommand],
        stdin => { via => 'pipe_write' },
        stdout => { via => 'pipe_read' },
        on_finish => sub {
            # ignore failures; file returns non-zero on parse errors
            # output then contains "ERROR" messages but is still usable

            $generatedone->done('Done with @generatecommand');
            return;
        });

    my $helperpath = locate_helper_tool('coll/file-info-helper');
    my $helperdone = $loop->new_future;
    my $helpererrors;

    my $helper = IO::Async::Process->new(
        command => $helperpath,
        stdin => { via => 'pipe_write' },
        stdout => { via => 'pipe_read' },
        stderr => { into => \$helpererrors },
        on_finish => sub {
            my ($self, $exitcode) = @_;
            my $status = ($exitcode >> 8);

            if ($status) {
                my $message = "Command $helperpath exited with status $status";
                $message .= COLON . NEWLINE . $helpererrors
                  if length $helpererrors;
                $helperdone->fail($message);
                return;
            }

            $helperdone->done('Done with $helperpath');
            return;
        });

    my @compresscommand = ('gzip', '--best', '--no-name', '--stdout');
    my $compressdone = $loop->new_future;
    my $compresserrors;

    my $compress = IO::Async::Process->new(
        command => [@compresscommand],
        stdin => { via => 'pipe_write' },
        stdout => { via => 'pipe_read' },
        stderr => { into => \$compresserrors },
        on_finish => sub {
            my ($self, $exitcode) = @_;
            my $status = ($exitcode >> 8);

            if ($status) {
                my $message
                  = "Command @compresscommand exited with status $status";
                $message .= COLON . NEWLINE . $compresserrors
                  if length $compresserrors;
                $compressdone->fail($message);
                return;
            }

            $compressdone->done('Done with @compresscommand');
            return;
        });

    $generate->stdout->configure(
        on_read => sub {
            my ($stream, $buffref, $eof) = @_;

            if (length $$buffref) {
                $helper->stdin->write($$buffref);
                $$buffref = EMPTY;
            }

            $helper->stdin->close_when_empty
              if $eof;

            return 0;
        },
    );

    $helper->stdout->configure(
        on_read => sub {
            my ($stream, $buffref, $eof) = @_;

            if (length $$buffref) {
                $compress->stdin->write($$buffref);
                $$buffref = EMPTY;
            }

            $compress->stdin->close_when_empty
              if $eof;

            return 0;
        },
    );

    my $resultspath = "$dir/file-info.gz";
    open(my $fh, '>', $resultspath)
      or die "Could not open file '$resultspath': $!";

    $compress->stdout->configure(
        on_read => sub {
            my ($stream, $buffref, $eof) = @_;

            if (length $$buffref) {
                print {$fh} $$buffref;
                $$buffref = EMPTY;
            }

            close($fh)
              if $eof;

            return 0;
        },
    );

    $loop->add($generate);
    $loop->add($helper);
    $loop->add($compress);

    foreach my $path ($info->sorted_index) {
        next unless $path->is_file;

        $generate->stdin->write("$path\0");
    }

    $generate->stdin->close_when_empty;
    Future->needs_all($generatedone, $helperdone, $compressdone)->get;

    return;
}

collect(@ARGV) unless caller;

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et
