#!/usr/bin/perl

# $OpenBSD: check-indirect-dependencies,v 1.4 2007/01/16 18:15:53 sturm Exp $
# Copyright (c) 2006 Nikolay Sturm <sturm@openbsd.org>.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
# ``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 OPENBSD
# PROJECT 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.

# reduce the amount of direct dependencies by pointing out those which
# are pulled in as indirect dependencies

use strict;
use warnings;
use Cwd;

my $cwd = getcwd;
my $mainport;
my $make = $ENV{MAKE} || "make";
my $portsdir = $ENV{PORTSDIR} || "/usr/ports";

sub check_build_deps()
{
	my %directdeps;
	my %indirectdeps;

	print "===> Checking build dependencies\n";
	open(my $builddirdepends, "-|", "$make build-dir-depends") or
	    die "Cannot execute '$make build-dir-depends': $!\n";
	while (<$builddirdepends>) {
		chomp;
		my ($port, $builddep) = split;
		my @dirstack;
		$mainport = $port if not defined $mainport;
		next if $port ne $mainport;

		chdir("$portsdir/$builddep") or
		    die "Cannot chdir to $portsdir/$builddep\n";
		open(my $rundirdepends, "-|", "$make run-dir-depends") or
		    die "Cannot execute '$make run-dir-depends': $!\n";
		while (<$rundirdepends>) {
			chomp;
			my ($innerport, $rundep) = split;
			next if $innerport eq $rundep;

			if (not @dirstack) {
				push @dirstack, $mainport, $innerport, $rundep;
			} elsif ($dirstack[-1] eq $innerport) {
				push @dirstack, $rundep;
			} else {
				while ($dirstack[-1] ne $innerport) {
					pop @dirstack;
				}
				push @dirstack, $rundep;
			}

			if (exists $directdeps{$rundep}) {
				print "probably unneeded dependency: $rundep\n";
				print join(' -> ', @dirstack) . "\n\n";
			}
			$indirectdeps{$rundep} = [ @dirstack ];
		}
		close $rundirdepends;

		if (exists $indirectdeps{$builddep}) {
			print "probably unneeded dependency: $builddep\n";
			print join(' -> ', @{$indirectdeps{$builddep}});
			print "\n\n";
			next;
		}
		$directdeps{$builddep} = 1;
	}
	close $builddirdepends;

	chdir($cwd) or die "Cannot chdir to $cwd\n";
}

sub check_run_deps()
{
	my %directdeps;
	my %indirectdeps;
	my @dirstack;

	print "===> Checking run dependencies\n";
	open(my $rundirdepends, "-|", "$make run-dir-depends") or
	    die "Cannot execute '$make run-dir-depends': $!\n";
	while (<$rundirdepends>) {
		chomp;
		my ($port, $rundep) = split;
		$mainport = $port if not defined $mainport;
		next if $port eq $rundep;

		if (not @dirstack) {
			push @dirstack, $port, $rundep;
		} elsif ($dirstack[-1] eq $port) {
			push @dirstack, $rundep;
		} else {
			while ($dirstack[-1] ne $port) {
				pop @dirstack;
			}
			push @dirstack, $rundep;
		}

		if ($port eq $mainport) {
			if (exists $indirectdeps{$rundep}) {
				print "probably unneeded dependency: $rundep\n";
				print join(' -> ', @{$indirectdeps{$rundep}});
				print "\n\n";
				next;
			}
			$directdeps{$rundep} = 1;
		} else {
			if (exists $directdeps{$rundep}) {
				print "probably unneeded dependency: $rundep\n";
				print join(' -> ', @dirstack) . "\n\n";
			}
			$indirectdeps{$rundep} = [ @dirstack ];
		}
	}
	close $rundirdepends;
}

check_build_deps();
check_run_deps();
