#!/usr/bin/perl -w
# diffstat -- lintian collection script for source packages

# 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.

# This could be written more easily in shell script, but I'm trying
# to keep everything as perl to cut down on the number of processes
# that need to be started in a lintian scan.  Eventually all the
# perl code will be perl modules, so only one perl interpreter
# need be started.

package Lintian::coll::diffstat;

no lib '.';

use strict;
use warnings;
use autodie;

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

use Path::Tiny;

use Lintian::Util qw(get_dsc_info safe_qx);

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

sub collect {
    my ($pkg, undef, $dir) = @_;

    my $dscpath = "$dir/dsc";
    die 'diffstat invoked with wrong dir argument'
      unless -f $dscpath;

    my $data = get_dsc_info($dscpath);
    my $ver = $data->{'version'};

    my $patchpath = "$dir/debian-patch";
    unlink($patchpath)
      if -e $patchpath
      or -l $patchpath;

    $ver =~ s/^\d://; #Remove epoch for this

    my $diffpath = "$dir/${pkg}_${ver}.diff.gz";
    return
      unless -f $diffpath;

    my $contents = safe_qx('gunzip', '--stdout', $diffpath);
    path($patchpath)->spew($contents);

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

    my @command = ('diffstat',  '-p1', $patchpath);
    $loop->run_child(
        command => [@command],
        on_finish => sub {
            my ($pid, $exitcode, $stdout, $stderr) = @_;
            my $status = ($exitcode >> 8);

            if ($status) {
                my $message = "Command @command exited with status $status";
                $message .= ": $stderr" if length $stderr;
                $future->fail($message);
                return;
            }

            $future->done($stdout);
        });

    # will raise an exception when failed
    my $diffstat = $future->get;

    # remove the last line;
    chomp $diffstat;
    my @lines = split(/\n/, $diffstat);
    pop @lines;
    $diffstat = EMPTY;
    $diffstat .= $_ . NEWLINE for @lines;

    # copy all lines except the last
    path("$dir/diffstat")->spew($diffstat);

    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
