#!/usr/bin/perl

# debi:  Install current version of deb package
# debc:  List contents of current version of deb package
#
# debi and debc originally by Christoph Lameter <clameter@debian.org>
# Copyright Christoph Lameter <clameter@debian.org>
# The now defunct debit originally by Jim Van Zandt <jrv@vanzandt.mv.com>
# Copyright 1999 Jim Van Zandt <jrv@vanzandt.mv.com>
# Modifications by Julian Gilbey <jdg@debian.org>, 1999-2003
# Copyright 1999-2003, Julian Gilbey <jdg@debian.org>
#
# 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, see <https://www.gnu.org/licenses/>.

use 5.008;
use strict;
use warnings;
use Getopt::Long qw(:config bundling permute no_getopt_compat);
use File::Basename;
use filetest 'access';
use Cwd;
use Dpkg::Control;
use Dpkg::Changelog::Parse qw(changelog_parse);
use Dpkg::IPC;

my $progname = basename($0, '.pl');    # the '.pl' is for when we're debugging
my $modified_conf_msg;

sub usage_i {
    print <<"EOF";
Usage: $progname [options] [.changes file] [package ...]
  Install the .deb file(s) just created, as listed in the generated
  .changes file or the .changes file specified.  If packages are listed,
  only install those specified packages from the .changes file.
  Options:
    --no-conf or      Don\'t read devscripts config files;
      --noconf          must be the first option given
    -a<arch>          Search for .changes file made for Debian build <arch>
    -t<target>        Search for .changes file made for GNU <target> arch
    --debs-dir DIR    Look for the changes and debs files in DIR instead of
                      the parent of the current package directory
    --multi           Search for multiarch .changes file made by dpkg-cross
    --upgrade         Only upgrade packages; don't install new ones.
    --check-dirname-level N
                      How much to check directory names:
                      N=0   never
                      N=1   only if program changes directory (default)
                      N=2   always
    --check-dirname-regex REGEX
                      What constitutes a matching directory name; REGEX is
                      a Perl regular expression; the string \`PACKAGE\' will
                      be replaced by the package name; see manpage for details
                      (default: 'PACKAGE(-.+)?')
    --with-depends    Install packages with their depends.
    --tool TOOL       Use the specified tool for installing the dependencies
                      of the package(s) to be installed.
                      (default: apt-get)
    --help            Show this message
    --version         Show version and copyright information

Default settings modified by devscripts configuration files:
$modified_conf_msg
EOF
}

sub usage_c {
    print <<"EOF";
Usage: $progname [options] [.changes file] [package ...]
  Display the contents of the .deb or .udeb file(s) just created, as listed
  in the generated .changes file or the .changes file specified.
  If packages are listed, only display those specified packages
  from the .changes file.  Options:
    --no-conf or      Don\'t read devscripts config files;
      --noconf          must be the first option given
    -a<arch>          Search for changes file made for Debian build <arch>
    -t<target>        Search for changes file made for GNU <target> arch
    --debs-dir DIR    Look for the changes and debs files in DIR instead of
                      the parent of the current package directory
    --list-changes    only list the .changes file
    --list-debs       only list the .deb files; don't display their contents
    --multi           Search for multiarch .changes file made by dpkg-cross
    --check-dirname-level N
                      How much to check directory names:
                      N=0   never
                      N=1   only if program changes directory (default)
                      N=2   always
    --check-dirname-regex REGEX
                      What constitutes a matching directory name; REGEX is
                      a Perl regular expression; the string \`PACKAGE\' will
                      be replaced by the package name; see manpage for details
                      (default: 'PACKAGE(-.+)?')
    --help            Show this message
    --version         Show version and copyright information

Default settings modified by devscripts configuration files:
$modified_conf_msg
EOF
}

if    ($progname eq 'debi') { *usage = \&usage_i; }
elsif ($progname eq 'debc') { *usage = \&usage_c; }
else { die "Unrecognised invocation name: $progname\n"; }

my $version = <<"EOF";
This is $progname, from the Debian devscripts package, version 2.23.4+deb12u2
This code is copyright 1999-2003, Julian Gilbey <jdg\@debian.org>,
all rights reserved.
Based on original code by Christoph Lameter and James R. Van Zandt.
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of
the GNU General Public License, version 2 or later.
EOF

# Start by setting default values
my $debsdir;
my $debsdir_warning;
my $check_dirname_level = 1;
my $c                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           