#! /usr/bin/perl -w
#                              -*- Mode: Perl -*- 
# pkg-deptree --- 
# Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com ) 
# Created On       : Wed Jul 16 14:35:20 1997
# Created On Node  : tiamat.datasync.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Sat Jul 19 00:18:54 1997
# Last Machine Used: tiamat.datasync.com
# Update Count     : 40
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# 

use strict;
use diagnostics;
use Carp;
require 5.001;

use Debian::Package::Dependency_List;
use Debian::Package::Package;
use Debian::Package::List;
use Getopt::Long;

package main;

#Handle The auto generated eval line.
use vars qw($running_under_some_shell);


=head1 NAME

pkg-deptree - A Package dependency pretty printing tool.

=cut

=head1 SYNOPSIS

 usage: pkg-deptree [options] [reg-exp [reg-exp ...]]
 where the options are:

=over 4

=item --nocheck-recommends 

=item --check-recommends Check Recommends too. [OFF]

=item --nocheck-suggests

=item --check-suggests   Check Suggests too.   [OFF]

=item --installed-packages <Package-file-for-installed-packages> 

=back

=cut

=head1 DESCRIPTION

This utility prints out a dependency tree of installed packages,
optionally taking a list of space separated regular expressions on the
command line to limit the starting points. If no regular expressions
are given, then all installed packages shall be displayed.

 The default is to assume that the list of installed packages may be
 derived from the file I</var/lib/dpkg/status>, but the user may
 override this by providing a I<Packages> file listing all the
 packages that are assumed to be installed.

=cut 

sub main {
  my $installed;
  my $candidates;
  my $ret;
  my $recommends = 0;
  my $suggests = 0;
  my $consistent = 1;
  my $usage = '';
  my $MYNAME = '';
  my $list = '';
  my $installed_packages = '';
  
  ($MYNAME     = $0) =~ s|.*/||;


  $usage= <<EOUSAGE;
 usage: $MYNAME [options] <Package-file-for-new-packages>
 where the options are:
 --nocheck-recommends
 --check-recommends   Check the Recommends field as well            [OFF]
 --nocheck-suggests
 --check-suggests     Check the Suggests field as well              [OFF]
 --installed-packages <Package-file-for-installed-packages> 
EOUSAGE
  
  $ret = GetOptions("check-recommends!"   => \$recommends,
                    "check-suggests!"     => \$suggests,
		    "installed-packages=s" => \$installed_packages);
  die "$usage" unless $ret;
  
  ######################################################################
  #                     Phase One: Gather data                         #
  ######################################################################

  # Installed file (default value taken from status file)
  if (-f $installed_packages) {
    $installed = Debian::Package::New->new('filename' => $installed_packages);
    $candidates = Debian::Package::New->new('filename' => $installed_packages);
  }
  else {
    $installed = Debian::Package::Installed->new();
    $candidates = Debian::Package::Installed->new();
  }
  
  $candidates->get_dependency_information();

  ######################################################################
  #                 Phase Two: Check dependencies                      #
  ######################################################################

  # This sets Types which will show up as critical errors. Does not
  # change what errors are recorded. 
  $candidates->set_fatal_failure_on_types('Type List' =>
					  "Pre-Depends Depends Conflict");
  
  # Check Pre-Dependencies
  $candidates->check_relations('Consistent' => $consistent,
			       'Installed' => $installed,
			       'Field' => 'Pre-Depends');
  # Check Dependencies
  $candidates->check_relations('Consistent' => $consistent,
			       'Installed' => $installed,
			       'Field' => 'Depends');
  # Check Recommendations
  $candidates->check_relations('Consistent' => $consistent,
			       'Installed' => $installed,
			       'Warn'       => 'True',
			       'Field' => 'Recommendations')
    if $recommends;
  # Check Suggestions
  $candidates->check_relations('Consistent' => $consistent,
			       'Installed'  => $installed,
			       'Warn'       => 'True',
			       'Field'      => 'Suggestions')
    if $suggests;
  
  ######################################################################
  #                Phase Four: Gather ordering data                    #
  ######################################################################
  
  # Order Pre-Dependencies
  $candidates->order('Field' => 'Pre-Depends');
  # Order Dependencies
  $candidates->order('Field' => 'Depends');

  ######################################################################
  #                    Phase Five: Do ordering                         #
  ######################################################################
  
  # Get ordering info and do topological sorting
  my $order_string = $candidates->get_ordering();

  my @ordered_list = split(' ', $order_string);
  my %Done = ();
  my $pkg = '';
  
  foreach $pkg (@ordered_list) {
    next if $Done{$pkg} && $Done{$pkg} > 1;
    next unless $candidates->{$pkg}->{'Package'};
    if ($#ARGV > -1) {
      my $match = '';
      for (@ARGV) {
	if ($pkg =~ m/$_/) {
	  $match = 1;
	  last;
	}
      }
      next unless $match;
    }

    print_depends('Candidates' => $candidates,
		  'Package' => $pkg,
		  'Level' => 0,
		  'Done' => \%Done);
  }
  exit 0;
}

sub print_depends{
  my %params = @_;
  my $targets = '';
  my @targets = ();
  my $pkg = '';
  
  print "  " x $params{'Level'}, "$params{'Package'}\n";
  if (defined $params{'Done'}->{$params{'Package'}}) {
    $params{'Done'}->{$params{'Package'}} += 1;
  }
  else {
    $params{'Done'}->{$params{'Package'}} = 1;
  }

  return if $params{'Done'}->{$params{'Package'}} > 1;

  
  $targets = 
    $params{'Candidates'}->{' _Targets'}->is_target('Type' => "All",
						    'Target' =>
						    $params{'Package'});
  return unless $targets;
  
  @targets = split(' ', $targets);
  foreach $pkg (@targets) {
    next unless $params{'Candidates'}->{$pkg}->{'Package'};
    
    print_depends('Candidates' => $params{'Candidates'},
		  'Package' => $pkg,
		  'Level' => $params{'Level'} + 1,
		  'Done' => $params{'Done'});
  }
}

=head1 CAVEATS

This is very inchoate, at the moment, and needs testing.

=cut

=head1 BUGS

None Known so far.

=cut

=head1 AUTHOR

Manoj Srivastava <srivasta@debian.org>

=cut

&main::main();

    
