#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: pkg_info,v 1.41 2007/01/29 10:57:36 bernd Exp $
#
# Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;
use OpenBSD::PackageInfo;
use OpenBSD::PackageName;
use OpenBSD::Getopt;
use OpenBSD::Error;

package OpenBSD::PackingElement;
sub dump_file
{
}

sub hunt_file
{
}

sub sum_up
{
	my ($self, $rsize) = @_;
	if (defined $self->{size}) {
		$$rsize += $self->{size};
	}
}

package OpenBSD::PackingElement::FileBase;
sub dump_file
{
	my ($item, $opt_K) = @_;
	if ($opt_K) {
		print '@', $item->keyword(), " ";
	}
	print $item->fullname(), "\n";
}

sub hunt_file
{
	my ($item, $h, $pkgname, $l) = @_;
	my $fname = $item->fullname();
	if (defined $h->{$fname}) {
		push(@{$h->{$fname}}, $pkgname);
		push(@$l, $pkgname);
	}
}

package main;

my $total_size = 0;
my $pkgs = 0;

sub find_pkg
{
	my ($pkgname, $code) = @_;
	require OpenBSD::PackageLocator;
	my $repo = OpenBSD::PackageRepository::Installed->new();
	my $pkg;

	$pkg = $repo->find($pkgname);
	if (defined $pkg) {
		&$code($pkgname, $pkg);
		return;
	}
	if (OpenBSD::PackageName::is_stem($pkgname)) {
		my @l = sort (OpenBSD::PackageName::findstem($pkgname, $repo->available()));
		if (@l != 0) {
			for my $p (@l) {
				&$code($p, $repo->find($p));
			}
			return;
		}
	}
				
	$pkg = OpenBSD::PackageLocator->find($pkgname);
	if (defined $pkg) {
		&$code($pkgname, $pkg);
		$pkg->close_now();
		$pkg->wipe_info();
	}
}

sub printfile
{
	my $filename = shift;
	local $_;

	open my $fh, '<', $filename or return;
	while(<$fh>) {
		print;
	}
	close $fh;
}

sub print_description
{
	my $dir = shift;
	local $_;

	open my $fh, '<', $dir.DESC or return;
	$_ = <$fh> unless -f $dir.COMMENT;
	while(<$fh>) {
		print;
	}
	close $fh;
}

sub get_line
{
	open my $fh, '<', shift or return "";
	my $c = <$fh>;
	chomp($c);
	close $fh;
	return $c;
}

sub get_comment
{
	my $d = shift;
	return get_line(-f $d.COMMENT? $d.COMMENT : $d.DESC);
}

sub find_by_spec
{
	my $pat = shift;

	require OpenBSD::PkgSpec;

	return sort(OpenBSD::PkgSpec::match($pat, installed_packages()));
}

sub filter_files
{
	require OpenBSD::PackingOld;
	require OpenBSD::PackingList;

	my $search = shift;
	my @result = ();
	for my $arg (@_) {
		find_pkg($arg, 
		    sub {
		    	my ($pkgname, $handle) = @_;

			my $plist = $handle->plist(\&OpenBSD::PackingList::FilesOnly);
				
			$plist->visit('hunt_file', $search, $pkgname, \@result);
		    });
	}
	return @result;
}

my $path_info;

sub add_to_path_info
{
	my ($path, $pkgname) = @_;

	$path_info->{$path} = [] unless 
	    defined $path_info->{$path};
	push(@{$path_info->{$path}}, $pkgname);
}

sub find_by_path
{
	my $pat = shift;

	if (!defined $path_info) {
		require OpenBSD::PackingOld;
		require OpenBSD::PackingList;

		$path_info = {};
		for my $pkg (installed_packages(1)) {
			my $plist =
				OpenBSD::PackingList->from_installation($pkg,
				    \&OpenBSD::PackingList::ExtraInfoOnly);
			add_to_path_info($plist->{extrainfo}->{subdir}, 
			    $plist->pkgname());
			if ($plist->has('pkgpath')) {
				for my $p (@{$plist->{pkgpath}}) {
					add_to_path_info($p->{name}, 
					    $plist->pkgname());
				}
			}
		}
	}
	if (defined $path_info->{$pat}) {
		return @{$path_info->{$pat}};
	} else {
		return ();
	}
}

our ($opt_c, $opt_D, $opt_d, $opt_f, $opt_I, $opt_i, $opt_k, $opt_K, $opt_L, 
    $opt_m, $opt_p, $opt_Q, $opt_q, $opt_R, $opt_r, $opt_s, $opt_v, $opt_h,
    $opt_l, $opt_a, $opt_M, $opt_U, $opt_A, $opt_S, $opt_P);
my $terse = 0;
my $exit_code = 0;
my $error_e = 0;
my @sought_files;

sub print_info
{
	my ($pkg, $handle) = @_;
	unless (defined $handle) {
		print STDERR "Error printing info for $pkg: no info ?\n";
	}
	if ($opt_I) {
		my $l = 20 - length($pkg);
		$l = 1 if $l <= 0;
		print $pkg, " "x$l, get_comment($handle->info()), "\n";
	} else {
		if ($terse) {
			print $opt_l, $pkg, "\n" unless $opt_q;
		} else {
			print $opt_l, "Information for ", $pkg, "\n\n" unless $opt_q;
		}
		if ($opt_c) {
			print $opt_l, "Comment:\n" unless $opt_q;
			print get_comment($handle->info()), "\n";
			print "\n";
		}
		if ($opt_R && -f $handle->info().REQUIRED_BY) {
			print $opt_l, "Required by:\n" unless $opt_q;
			printfile($handle->info().REQUIRED_BY);
			print "\n";
		}
		if ($opt_d) {
			print $opt_l, "Description:\n" unless $opt_q;
			print_description($handle->info());
			print "\n";
		}
		if ($opt_M && -f $handle->info().DISPLAY) {
			print $opt_l, "Install notice:\n" unless $opt_q;
			printfile($handle->info().DISPLAY);
			print "\n";
		}
		if ($opt_U && -f $handle->info().UNDISPLAY) {
			print $opt_l, "Deinstall notice:\n" unless $opt_q;
			printfile($handle->info().UNDISPLAY);
			print "\n";
		}
		if ($opt_i && -f $handle->info().INSTALL) {
			print $opt_l, "Install script:\n" unless $opt_q;
			printfile($handle->info().INSTALL);
			print "\n";
		}
		if ($opt_k && -f $handle->info().DEINSTALL) {
			print $opt_l, "De-Install script:\n" unless $opt_q;
			printfile($handle->info().DEINSTALL, \*STDOUT);
			print "\n";
		}
		if ($opt_r && -f $handle->info().REQUIRE) {
			print $opt_l, "Require script:\n" unless $opt_q;
			printfile($handle->info().REQUIRE, \*STDOUT);
			print "\n";
		}
		my $plist;
		if ($opt_f || $opt_L || $opt_s || $opt_S) {
			require OpenBSD::PackingOld;
			require OpenBSD::PackingList;

			if ($opt_f || $opt_s || $opt_S) {
				$plist = $handle->plist();
			} else {
				$plist = $handle->plist(\&OpenBSD::PackingList::FilesOnly);
			}
			Fatal "Bad packing list" unless defined $plist;
		}
		if ($opt_L) {
			print $opt_l, "Files:\n" unless $opt_q;
			$plist->visit('dump_file', $opt_K);
			print "\n";
		}
		if ($opt_s) {
			my $size = 0;
			$plist->visit('sum_up', \$size);
			print "Size: " unless $opt_q;
			print "$size\n";
			$total_size += $size;
			$pkgs++;
		}
		if ($opt_S) {
			print "Signature: " unless $opt_q;
			print $plist->signature(), "\n";
		}
		if ($opt_P) {
			require OpenBSD::PackingList;

			my $plist = $handle->plist(
			    \&OpenBSD::PackingList::ExtraInfoOnly);
			print "Pkgpath:\n" unless $opt_q;
			print $plist->{extrainfo}->{subdir}, "\n";
		}

		if ($opt_f) {
			print $opt_l, "Packing list:\n" unless $opt_q;
			$plist->write(\*STDOUT);
			print "\n";
		}
		print $opt_l, "\n" unless $opt_q || $terse;
	}
}

set_usage('pkg_info [-AacDdfIiKkLMPpqRrSsUv] [-E filename] [-e pkg-name] [-l str] [-Q query] [pkg-name] [...]');

my $locked;
try { 
	getopts('cDdfhIikKLmpPQ:qRrsSUve:E:Ml:aA',
	    {'e' =>
		    sub {
			    my $pat = shift;
			    my @list;
			    lock_db(1, $opt_q);
			    $locked = 1;
			    if ($pat =~ m/\//) {
				    @list = find_by_path($pat);
			    } else {
				    @list = find_by_spec($pat);
			    }
			    if (@list == 0) {
				    $exit_code = 1;
				    $error_e = 1;
			    }
			    push(@ARGV, @list);
			    $terse = 1;
		    },
	     'h' => sub {	Usage(); },
	     'E' =>
		    sub {
			    require File::Spec;

			    push(@sought_files, File::Spec->rel2abs(shift));

		    }
	}); 
} catchall {
	Usage($_);
};

lock_db(1, $opt_q) unless $locked;

if ($opt_D) {
	$opt_M = 1;
}

unless ($opt_c || $opt_M || $opt_U || $opt_d || $opt_f || $opt_I || $opt_i ||
	$opt_k || $opt_L || $opt_m || $opt_p || $opt_R || $opt_r || $opt_s || 
	$opt_S || $opt_P || $terse) {
	if (@ARGV == 0) {
		$opt_I = $opt_a = 1;
	} else {
		$opt_c = $opt_d = $opt_R = 1;
	}
}

if ($opt_Q) {
	require OpenBSD::PackageLocator;

	my $stemlist = OpenBSD::PackageName::available_stems();
	print "PKG_PATH=$ENV{PKG_PATH}\n" if $opt_v;

	for my $stem (sort keys %$stemlist) {
		if ($stem =~ /\Q$opt_Q\E/i) {
			for my $p (keys %{$stemlist->{$stem}}) {
				my $text = "$p";
				if (is_installed($p)) {
					$text .= " (installed)";
				}
				print $text."\n";
			}
		}
	}

	exit 0;
}

if ($opt_v) {
	$opt_c = $opt_d = $opt_f = $opt_i = $opt_k = $opt_r = $opt_M =
	    $opt_U = $opt_m = $opt_R = $opt_s = $opt_S = 1;
}

if (!defined $opt_l) {
	$opt_l = "";
}

if ($opt_K && !$opt_L) {
	Usage "-K only makes sense with -L";
}

if (@ARGV == 0 && !$opt_a && !$opt_A) {
	Usage "Missing package name(s)" unless $terse || $opt_q;
}

if (@ARGV > 0 && ($opt_a || $opt_A)) {
	Usage "Can't specify package name(s) with -a";
}

if (@ARGV == 0 && !$error_e) {
	@ARGV = sort(installed_packages(defined $opt_A ? 0 : 1));
}

if (@sought_files) {
	my %hash = map { ($_, []) }  @sought_files;
	@ARGV = filter_files(\%hash, @ARGV);
	for my $f (@sought_files) {
		my $l = $hash{$f};
		if (@$l) {
			print "$f: ", join(',', @$l), "\n" unless $opt_q;
		} else {
			$exit_code = 1;
		}
	}
}

for my $pkg (@ARGV) {
	find_pkg($pkg, \&print_info);
}
if ($pkgs > 1) {
	print "Total size: $total_size\n";
}
exit($exit_code);
