#!/usr/bin/perl -w
# bin-pkg-control -- lintian collector script

# Copyright (C) 1998 Christian Schwarz
#
# 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.

use strict;

($#ARGV == 1) or fail("syntax: bin-pkg-control <pkg> <type>");
my $pkg = shift;
my $type = shift;
my (@jobs, $job);

use lib "$ENV{'LINTIAN_ROOT'}/lib";
use Util;
use Lintian::Command qw(spawn reap);

if (-e "control"){
    delete_dir("control") or fail("Cannot remove old control dir");
}

mkdir("control", 0777) or fail("mkdir control: $!");

# The following calls use knowledge of the .deb format for speed

# (replaces dpkg-deb -e)
# extract control files' tarball
spawn({ fail => 'error', out => "control.tar" },
      ['ar', 'p', 'deb', 'control.tar.gz'],
      '|', ['gzip', '-dc']);

$job = { fail => 'error', err => "control-errors" };
push @jobs, $job;
# extract the tarball's contents
spawn($job,
      ["tar", "xf", "control.tar", "-C", "control", '&']);

$job = { fail => 'error',
         out  => "control-index",
         err  => "control-index-errors" };

push @jobs, $job;
# create index of control.tar.gz
spawn($job,
      ["tar", "tvf", "control.tar"],
      '|', ["sort", "-k", "6"], '&');

reap(@jobs);
undef @jobs;
# clean up control.tar
unlink("control.tar") or fail();

# fix permissions
spawn({ fail => 'error' },
      ["chmod", "-R", "u+rX,o-w", "control"]);

exit 0;

