#!/usr/bin/perl
# unpack-binpkg-l1 -- lintian unpack script (binary packages level 1)
#
# syntax: unpack-binpkg-l1 <base-dir> <deb-file>
#
# Note that <deb-file> must be specified with absolute path.

# 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., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

use strict;

($#ARGV == 1) or die "syntax: unpack-binpkg-l1 <base-dir> <deb-file>";
my $base_dir = shift;
my $file = shift;

# import perl libraries
use lib "$ENV{'LINTIAN_ROOT'}/lib";
use Pipeline;
use Util;

# stat $file
(my @stat = stat $file) or fail("$file: cannot stat: $!");

# create directory in lab
print "N: Creating directory $base_dir ...\n" if $::verbose;
mkdir("$base_dir", 0777) or fail("mkdir $base_dir: $!");
mkdir("$base_dir/control", 0777) or fail("mkdir $base_dir/control: $!");
mkdir("$base_dir/fields", 0777) or fail("mkdir $base_dir/fields: $!");
symlink($file,"$base_dir/deb") or fail("symlink: $!");

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

# (replaces dpkg-deb -e)
# extract control files' tarball
pipeline((sub { exec 'ar', 'p', $file, 'control.tar.gz' }),
	 (sub { exec 'gzip', '-dc' }),
	 "$base_dir/control.tar") == 0
    or fail();

# extract the tarball's contents
spawn("tar", "xf", "$base_dir/control.tar", "-C", "$base_dir/control") == 0
    or fail();

# create index of control.tar.gz
pipeline((sub { exec "tar", "tvf", "$base_dir/control.tar" }),
	 "$base_dir/control-index") == 0
    or fail();

# clean up control.tar
unlink("$base_dir/control.tar") or fail();

# fix permissions
spawn("chmod", "-R", "u+rX,o-w", "$base_dir/control") == 0
    or fail();

# (replaces dpkg-deb -c)
# create index file for package
pipeline((sub { exec "dpkg-deb", "--fsys-tarfile", $file }),
	 (sub { exec "tar", "tfv", "-" }),
	 (sub { exec "sed", "-e", "s/^h/-/" }),
	 "$base_dir/index") == 0
    or fail();

# get package control information
my $data = (read_dpkg_control("$base_dir/control/control"))[0];
$data->{'source'} or ($data->{'source'} = $data->{'package'});

# create control field files
for my $field (keys %$data) {
    my $field_file = "$base_dir/fields/$field";
    open(F,">$field_file") or fail("cannot open file $field_file for writing: $!");
    print F $data->{$field},"\n";
    close(F);
}

# create symlink to source package
symlink("../../source/$data->{'source'}","$base_dir/source") or fail("symlink: $!");

exit 0;
