#!/usr/bin/perl

# $OpenBSD: check-conflicts,v 1.8 2014/02/08 13:50:58 espie Exp $
# Copyright (c) 2000-2005
# Marc Espie.  All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Neither the name of OpenBSD nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ITS AUTHOR AND THE OpenBSD project ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# check all packages in the current directory, and report conflicts which
# are not apparent in @pkgcfl.

use strict;

use File::Spec;
use File::Path;
use OpenBSD::PackageInfo;
use OpenBSD::PackingList;
use OpenBSD::AddCreateDelete;
use OpenBSD::PkgCfl;

package OpenBSD::PackingElement;
sub register
{
}

sub known_page
{
}

sub add_extra_manpage
{
}

package OpenBSD::PackingElement::FileBase;

my $pkg_list = {};
my $seen = {};

sub register 
{
	my ($self, $all_conflict, $all_deps, $pkgname) = @_;

	my $file = File::Spec->canonpath($self->fullname);
	# build one single list for each pkgnames combination
	if (exists $all_conflict->{$file}) {
		$pkg_list->{$all_conflict->{$file}}{$pkgname} ||=
		    [@{$all_conflict->{$file}}, $pkgname ];
		$all_conflict->{$file} = 
		    $pkg_list->{$all_conflict->{$file}}{$pkgname};
	} elsif (exists $seen->{$file}) {
		$pkg_list->{$seen->{$file}}{$pkgname} ||=
		    [ @{$seen->{$file}}, $pkgname ];
		$all_conflict->{$file} = 
		    $pkg_list->{$seen->{$file}}{$pkgname};
		delete $seen->{$file};
	} else {
		$pkg_list->{$pkgname} ||= [$pkgname];
		$seen->{$file} = $pkg_list->{$pkgname};
	}
}

package OpenBSD::PackingElement::Depend;
sub register 
{
	my ($self, $all_conflict, $all_deps, $pkgname) = @_;
	if (defined $self->{def}) {
		push @{$all_deps->{$pkgname}}, $self->{def};
	}
}

package OpenBSD::PackingElement::Manpage;

sub is_dest
{
	my $self = shift;
	return $self->name =~ m/man\/cat[^\/]+\/[^\/]+\.0$/o;
}

sub dest_to_source
{
	my $self = shift;
	my $v = $self->name;
	$v =~ s/(man\/)cat([^\/]+)(\/[^\/]+)\.0$/$1man$2$3.$2/;
	return $v;
}

sub known_page
{
	my ($self, $h) = @_;
	$h->{File::Spec->canonpath($self->fullname)} = 1;
}

sub add_extra_manpage
{
	my ($self, $known, $plist) = @_;
	if ($self->is_source) {
		my $dest = $self->source_to_dest;
		my $fullname = $self->cwd."/".$dest;
		my $file = File::Spec->canonpath($fullname);
		if (!$known->{$file}) {
			OpenBSD::PackingElement::Manpage->add($plist, $dest);
			$known->{$file} = 1;
		}
	}
	if ($self->is_dest) {
		my $src = $self->dest_to_source;
		my $fullname = $self->cwd."/".$src;
		my $file = File::Spec->canonpath($fullname);
		if (!$known->{$file}) {
			OpenBSD::PackingElement::Manpage->add($plist, $src);
			$known->{$file} = 1;
		}
	}
}

package main;

my $cache = {};
my $cache2 = {};
my $cache3 = {};
my $cache4 = {}; 

sub add_more_man
{
	my $plist = shift;
	my $knownman = {};
	$plist->known_page($knownman);
	$plist->add_extra_manpage($knownman, $plist);
}

sub direct_conflict
{
	my ($conflicts, $pkg, $pkg2) = @_;

	return $cache3->{$pkg}{$pkg2} //= $conflicts->{$pkg}->conflicts_with($pkg2);
}

sub has_a_conflict
{
	my ($conflicts, $deps, $pkg, $pkg2) = @_;
	return $cache4->{$pkg}{$pkg2} //= find_a_conflict($conflicts, $deps, $pkg, $pkg2);
}

sub find_a_conflict
{
	my ($conflicts, $deps, $pkg, $pkg2) = @_;
	return 0 if $pkg eq $pkg2;
	
	if (defined $conflicts->{$pkg} && direct_conflict($conflicts, $pkg, $pkg2)) {
		return 1;
	}
	if (defined $deps->{$pkg}) {
		for my $dep (@{$deps->{$pkg}}) {
		    if (has_a_conflict($conflicts, $deps, $dep, $pkg2)) {
			    return 1;
		    }
		}
	}
	if (defined $deps->{$pkg2}) {
		for my $dep (@{$deps->{$pkg2}}) {
			if (has_a_conflict($conflicts, $deps, $pkg, $dep)) {
				return 1;
			}
		}
	}
	return 0;
}

sub compute_true_conflicts
{
	my ($l, $conflicts, $deps) = @_;
	# create a list of unconflicting packages.
	my $l2 = [];
	for my $pkg (@$l) {
		my $keepit = 0;
		for my $pkg2 (@$l) {
			next if $pkg eq $pkg2;
			if (!(has_a_conflict($conflicts, $deps, $pkg, $pkg2) ||
			    has_a_conflict($conflicts, $deps, $pkg2, $pkg))) {
				$keepit = 1;
				last;
			}
		}
		if ($keepit) {
		    push(@$l2, $pkg);
		}
	}
	return $l2;
}

sub compute_problems
{
	my ($ui, $h, $conflicts, $deps, $current, $name2path) = @_;

	my $c = {};
	my $c2 = {};

	my $total = scalar(keys %$h);
	my $i =0;
	while (my ($key, $l) = each %$h) {
		$ui->progress->show(++$i, $total);
		if (!defined $c->{$l}) {
			my %s = map {($_, 1)} @$l;
			$c->{$l} = [sort keys %s];
			$c2->{$l} = join(',', @{$c->{$l}});
		}
		my $hv = $c2->{$l};
		$l = $c->{$l};
		next if @$l == 1;
		if (!defined $cache->{$hv}) {
			$cache->{$hv} = compute_true_conflicts($l, $conflicts, $deps);
		}
		my $result = $cache->{$hv};
		if (@$result != 0) {
			my $newkey = join(',', 
			    map {$current->{$_} ? "$_!($name2path->{$_})" : 
			    "$_($name2path->{$_})" } @$result);
			if (@$result == 1) {
				$newkey.="-> was ".join(',', @$l);
			}
			push(@{$cache2->{$newkey}}, $key);
		}
	}
}

my $filehash={};
my %dirhash=();
my $conflicts={};
my $dephash={};
my $name2path={};
my $current={};

our ($opt_d, $opt_e, $opt_p, $opt_v);

sub handle_plist
{
	my ($ui, $filename, $plist) = @_;
	if (!defined $plist) {
		$ui->errsay("Error reading #1", $filename);
		return;
	}
	if (!defined $plist->pkgname) {
		$ui->errsay("Invalid package #1", $filename);
		return;
	}
	$name2path->{$plist->pkgname} = $plist->fullpkgpath;
	$ui->say("#1 -> #2", $filename, $plist->pkgname) if $ui->verbose;
	$plist->forget;
	$conflicts->{$plist->pkgname} = 
	    OpenBSD::PkgCfl->make_conflict_list($plist);
	if ($opt_e) {
		add_more_man($plist);
	}
	$plist->register($filehash, $dephash, $plist->pkgname);
}

sub handle_file
{
	my ($ui, $filename) = @_;
	return if -d $filename;
	my $plist = OpenBSD::PackingList->fromfile($filename);
	handle_plist($ui, $filename, $plist);
}

sub handle_portspath
{
	my ($ui, $path) = @_;
	foreach (split(/:/, $path)) {
		handle_portsdir($ui, $_);
	}
}

sub find_current_pkgnames
{
	my ($ui, $dir) = @_;

	my $done = {};
	my @todo = ();
	my $make = $ENV{MAKE} || 'make';

	for my $path (values %$name2path) {
		next if $done->{$path};
		push(@todo, $path);
	}
	my $total = scalar(@todo);
	my $i = 0;
	while (my @l = (splice @todo, 0, 1000)) {
		my $pid = open(my $output, "-|");
		if ($pid == 0) {
			$DB::inhibit_exit = 0;
			chdir($dir) or die "bad directory $dir";
			$ENV{SUBDIR} = join(' ', @l);
			open STDERR, ">", "/dev/null";
			exec { $make } 
			    ("make", 'show=FULLPKGNAME${SUBPACKAGE}', 
				'REPORT_PROBLEM=true', 'ECHO_MSG=:');
			exit(1);
		}
		while (<$output>) {
			$i++;
			$ui->progress->show($i, $total);
			chomp;
			$current->{$_} = 1;
		}
		close($output);
	}
}

sub handle_portsdir
{
	my ($ui, $dir) = @_;
	my $make = $ENV{MAKE} || 'make';

	open(my $input, "cd $dir && $make print-plist-all |");
	my $done = 0;
	while (!$done) {
		my $plist = OpenBSD::PackingList->read($input, sub {
			my ($fh, $cont) = @_;
			local $_;
			while (<$fh>) {
				return if m/^\=\=\=\> /o;
				next unless m/^\@(?:cwd|name|info|man|file|lib|shell|bin|conflict|comment\s+subdir\=)\b/o || !m/^\@/o;
				&$cont($_);
			}
			$done = 1;
		});
		if (defined $plist && $plist->pkgname()) {
			handle_plist($ui, $dir, $plist);
			$ui->progress->working(10);
		}
	}
}

my $ui = OpenBSD::AddCreateDelete::State->new('check-conflicts');
$ui->handle_options('d:ep:', '[-ve] [-d plist_dir] [-p ports_dir] [pkgname ...]');

$ui->progress->set_header("Scanning");
$opt_d = $ui->opt('d');
$opt_p = $ui->opt('p');
$opt_e = $ui->opt('e');
my $portpath;
if ($opt_d) {
	opendir(my $dir, $opt_d);
	my @l = readdir $dir;
	closedir($dir);

	$ui->progress->for_list("Scanning", \@l,
	    sub {
	    	my $pkgname = shift;
		return if $pkgname eq '.' or $pkgname eq '..';
		handle_file($ui, "$opt_d/$pkgname");
	    });
} elsif ($opt_p) {
	handle_portspath($ui, $opt_p);
	$portpath = 1;
} elsif (@ARGV==0) {
	@ARGV=(<*.tgz>);
}

if (@ARGV > 0) {
	$ui->progress->for_list("Scanning", \@ARGV,
	    sub {
		my $pkgname = shift;
		my $true_package = $ui->repo->find($pkgname);
		return unless $true_package;
		my $dir = $true_package->info;
		$true_package->close;
		handle_file($ui, $dir.CONTENTS);
		rmtree($dir);
	    });
}

if (!$portpath && $opt_p) {
	$ui->progress->set_header("Computing current pkgnames");
	find_current_pkgnames($ui, $opt_p);
}
$ui->progress->set_header("File problems");
compute_problems($ui, $filehash, $conflicts, $dephash, $current, $name2path);
for my $cfl (sort keys %$cache2) {
	$ui->say("#1", $cfl);
	for my $f (sort @{$cache2->{$cfl}}) {
		$ui->say("\t#1", $f);
	}
}
