#!/usr/bin/perl -w
# strings -- lintian collection script

# Copyright © 2009, 2010 Raphael Geissert <atomo64@gmail.com>
# 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::strings;

no lib '.';

use strict;
use warnings;
use autodie;

use File::Basename;
use IO::Async::Loop;
use IO::Async::Process;
use Path::Tiny;

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

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

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

my $helper = locate_helper_tool('coll/strings-helper');

sub collect {
    my ($pkg, $type, $dir) = @_;
    my ($info, @manual);

    my $stringdir = "$dir/strings";
    path($stringdir)->remove_tree
      if -d $stringdir;

    # If we are asked to only remove the files stop right here
    if ($type =~ m/^remove-/) {
        return;
    }

    $info = Lintian::Collect->new($pkg, $type, $dir);

    # The directory is required, even if it would be empty.
    mkdir("$dir/strings");

    chdir("$dir/unpacked");

    my $prehelper;
    foreach my $bin ($info->sorted_index) {
        my $filename = $bin->name;
        my $finfo;
        next if not $bin->is_file or $filename =~ m,^usr/lib/debug/,;
        $finfo = $info->file_info($filename);
        next unless $finfo =~ m/\bELF\b/o;

        if ($bin =~ m/[:\n\r]/) {
            # Do these "interesting cases" manual
            push @manual, $filename;
            next;
        }

        # append output from strings to prehelper
        $prehelper
          .= safe_qx('strings', '--all', '--print-file-name', '--',$filename);
    }

    if (length $prehelper) {
        my $loop = IO::Async::Loop->new;
        my $future = $loop->new_future;
        my $stderr;

        my $process = IO::Async::Process->new(
            command => [$helper, $stringdir],
            stdin => { from => $prehelper },
            stderr => { into => \$stderr },
            on_finish => sub {
                my ($self, $exitcode) = @_;
                my $status = ($exitcode >> 8);

                if ($status) {
                    my $message
                      = "Non-zero status $status from $helper $stringdir";
                    $message .= COLON . NEWLINE . $stderr
                      if length $stderr;
                    $future->fail($message);
                    return;
                }

                $future->done("Done with $helper $stringdir");
                return;
            });

        $loop->add($process);

        # will raise an exception in case of failure
        $future->get;
    }

    # Fall back to the safe but slower method for files with "special"
    # names.
    foreach my $file (@manual) {
        my $strdir = $dir . '/strings/' . File::Basename::dirname($file);

        # create the dir if needed.
        path($strdir)->mkpath
          unless -d $strdir;

        my $originpath = "$dir/unpacked/$file";
        my $uncompressed = safe_qx('strings', '-a', '--', $originpath);

        gzip($uncompressed, "$dir/strings/$file.gz");
    }

    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
