#!/usr/bin/perl -w
# scripts -- 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::scripts;

no lib '.';

use strict;
use warnings;
use autodie;

use Path::Tiny;

use lib "$ENV{'LINTIAN_ROOT'}/lib/";
use Lintian::Collect;
use Lintian::Util qw(strip);

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

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

    my $scripts;

    foreach my $path ($info->sorted_index) {
        next unless $path->is_regular_file and $path->is_open_ok;

        # skip lincity data files; magic: #!#!#!
        next if $path->size >= 6 && magic($path, 6) eq '#!#!#!';

        my $interpreter = get_interpreter($path);

        # no shebang => no script
        next unless defined $interpreter;

        # remove comment, if any
        (my $nocomment = $interpreter) =~ s/^\#.*//;

        unless (length $nocomment) {
            $scripts .= "$interpreter $path" . NEWLINE;
            next;
        }

        $scripts .= 'env' . SPACE
          if $nocomment =~ s,^/usr/bin/env\s+,,;

        # get base command without options
        $nocomment =~ s/\s++ .++ \Z//xsm;
        $scripts .= "$nocomment $path" . NEWLINE;
    }

    my $scriptspath = "$dir/scripts";
    path($scriptspath)->spew($scripts // EMPTY);

    my $maintainer;

    for my $path ($info->control_index(EMPTY)->children) {
        next unless $path->is_open_ok;

        # skip anything other than maintainer scripts
        next unless $path =~ m/^(?:(?:pre|post)(?:inst|rm)|config)$/;

        # allow elf binary
        if ($path->size >= 4 && magic($path, 4) eq "\x7FELF") {
            $maintainer .= "ELF $path" . NEWLINE;
            next;
        }

        # check for hashbang
        my $interpreter = get_interpreter($path) // EMPTY;

        # get base command without options
        $interpreter =~ s/\s++ .++ \Z//xsm;

        $maintainer .= "$interpreter $path" . NEWLINE;
    }

    my $controlpath = "$dir/control-scripts";
    path($controlpath)->spew($maintainer // EMPTY);

    return;
}

sub magic {
    my ($path, $count) = @_;
    my $magic;

    my $fd = $path->open;
    die "Could not read $count bytes from $path"
      unless read($fd, $magic, $count) == $count;
    close($fd);

    return $magic;
}

sub get_interpreter {
    my ($path) = @_;
    my ($interpreter, $magic);
    my $fd = $path->open;
    if (read($fd, $magic, 2) and $magic eq '#!' and not eof($fd)) {
        $interpreter = <$fd>;
        strip($interpreter);
    }
    close($fd);
    return $interpreter;
}

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
