#!/usr/bin/perl

# $OpenBSD: check-common-dirs,v 1.7 2007/05/13 08:03:47 espie Exp $
# Copyright (c) 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.

# check all packages in the current directory, and report common directory
# issues

use strict;
use warnings;

use File::Spec;
use File::Path;
use File::Basename;
use OpenBSD::PackageLocator;
use OpenBSD::PackageInfo;
use OpenBSD::PackingList;
use OpenBSD::Mtree;

sub register_dir
{
	my ($d, $h) = @_;
	return if defined $h->{$d};
	$h->{$d} = 1;
	register_dir(dirname($d), $h);
}

package OpenBSD::PackingElement;
sub check_common_dirs
{
}

package OpenBSD::PackingElement::FileBase;
use File::Basename;
sub check_common_dirs
{
	my ($item, $t) = @_;
	my $d = File::Spec->canonpath($item->fullname());
	main::register_dir(dirname($d), $t->{need_dirs});
}

package OpenBSD::PackingElement::DirlikeObject;
sub check_common_dirs
{
	my ($item, $t) = @_;
	my $d = File::Spec->canonpath($item->fullname());
	$t->{dirs}->{$d} = 1;
}

package OpenBSD::PackingElement::PkgDep;
sub check_common_dirs
{
	my ($item, $t) = @_;
	$t->{deps}->{$item->{name}} = 1;
}

package OpenBSD::PackingElement::Depend;
sub check_common_dirs
{
	my ($item, $t) = @_;
	$t->{deps}->{$item->{def}} = 1;
}

package OpenBSD::PackingElement::Wantlib;
sub check_common_dirs
{
}

package main;

sub analyze 
{
	my ($plist, $db) = @_;
	my $pkgname = $plist->pkgname();
	$db->{$pkgname} = {
		pkgname => $pkgname,
		missing_deps => {},
		dirs => {}, 
		need_dirs => {}, 
		deps => {},
		problems => {}
	} unless defined $db->{$pkgname};
	my $t = $db->{$pkgname};

	$plist->check_common_dirs($t);
}

sub parent_has_dir
{
	my ($db, $t, $dir) = @_;

	for my $dep (keys %{$t->{deps}}) {
		if (!defined $db->{$dep}) {
		    if (!defined $t->{missing_deps}->{$dep}) {
			    print $t->{pkgname}, ": $dep not found\n";
			    $t->{missing_deps}->{$dep} = 1;
		    }
		    next;
		}
		if ($db->{$dep}->{dirs}->{$dir} || 
		    parent_has_dir($db, $db->{$dep}, $dir)) {
			$t->{dirs}->{$dir} = 1;
			return 1;
		}
	}
	return 0;
}

sub parent_has_problem
{
	my ($db, $t, $dir) = @_;
	for my $dep (keys %{$t->{deps}}) {
		next if !defined $db->{$dep};
		if ($db->{$dep}->{problems}->{$dir}) {
			return 1;
		}
	}
	return 0;
}

sub build_results
{
	my ($db, $mtree) = @_;

	for my $pkgname (keys %$db) {
		my $t = $db->{$pkgname};
		for my $dir (keys(%{$t->{need_dirs}})) {
			next if $t->{dirs}->{$dir};
			next if $mtree->{$dir};
			next if parent_has_dir($db, $t, $dir);
			$t->{problems}->{$dir} = 1;
		}
	}
}

sub show_results
{
	my ($db, $mtree) = @_;

	for my $pkgname (sort keys %$db) {
		my @l=();
		my $t = $db->{$pkgname};
		for my $dir (keys %{$t->{problems}}) {
			next if parent_has_problem($db, $t, $dir);
			push(@l, $dir);
		}
		if (@l != 0) {
			print "$pkgname: ", join(', ', sort @l), "\n";
		}
	}
}

print "Scanning packages\n";
print "-----------------\n";
if (@ARGV==0) {
	@ARGV=(<*.tgz>);
}
my $db = {};
my $mtree = {};

OpenBSD::Mtree::parse($mtree, '/usr/local', '/etc/mtree/BSD.local.dist');
OpenBSD::Mtree::parse($mtree, '/', '/etc/mtree/4.4BSD.dist');
OpenBSD::Mtree::parse($mtree, '/usr/X11R6', '/etc/mtree/BSD.x11.dist');
$mtree->{'/usr/local/lib/X11'} = 1;
$mtree->{'/usr/local/include/X11'} = 1;
$mtree->{'/usr/local/lib/X11/app-defaults'} = 1;

for my $pkgname (@ARGV) {
	print STDERR "$pkgname\n";
	my $true_package = OpenBSD::PackageLocator->find($pkgname);
	next unless $true_package;
	my $dir = $true_package->info();
	$true_package->close();
	my $plist = OpenBSD::PackingList->fromfile($dir.CONTENTS);
	rmtree($dir);
	analyze($plist, $db);
}

build_results($db, $mtree);
show_results($db);