#!/usr/bin/env perl
#
#   Copyright 2014 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use warnings;

open(PACKAGES, "<Packages");

my @packages = ();
my %depends = ();
my %priority = ();
my %essential = ();
my %size = ();

sub add_depends {
	my ($package, $depends) = @_;
	my $deps = [];
	my @vdeps = split(",", $depends);
	for my $i (@vdeps) {
		$i =~ qr,([0-9a-z-+.]+),;
		push @$deps, $1;
	}
	$depends{$package} = $deps;
}

my $package;
while (<PACKAGES>) {
	if (/^Package: ([0-9a-z-+.]+)/) {
		$package = $1;
		push @packages, $package;
	}
	if (/^Depends: (.*)/) {
		add_depends($package, $1);
	}
	if (/^Priority: (.*)/) {
		$priority{$package} = $1;
	}
	if (/^Essential: yes/) {
		$essential{$package} = "yes";
	}
	if (/^Size: (.*)/) {
		$size{$package} = $1;
	}
}

close(PACKAGES);

open(SOURCES, "<Sources");

my @sources = ();
my %binaries = ();
my %csource = ();
my %bdeps = ();
my %ssize = ();

sub add_binaries {
	my ($package, $binaries) = @_;
	my $bb = [];
	my @vbb = split(", ", $binaries);
	for my $i (@vbb) {
		$i =~ qr,([0-9a-z-+.]+),;
		push @$bb, $1;
		$csource{$i} = $package;
	}
	$binaries{$package} = $bb;
}

sub add_bdeps {
	my ($package, $bdeps) = @_;
	my $bd = [];
	my @vbd = split(", ", $bdeps);
	for my $i (@vbd) {
		$i =~ qr,([0-9a-z-+.]+),;
		push @$bd, $1;
	}
	$bdeps{$package} = $bd;
}

my $files_start = 0;

while (<SOURCES>) {
	if (/^Package: ([0-9a-z-+.]+)/) {
		$package = $1;
		push @packages, $package;
	}
	if (/^Binary: (.*)/) {
		add_binaries($package, $1);
	}
	if (/^Build-Depends: (.*)/) {
		add_bdeps($package, $1);
	}
	if ($files_start) {
		if (/^ [a-f0-9]+ ([0-9]+)/) {
			$ssize{$package} += $1;
		} else {
			$files_start = 0;
		}
	}
	if (/^Files:/) {
		$files_start = 1;
		$ssize{$package} = 0;
	}
}

close(SOURCES);

my @pp = ();

my @visit = ();

my @vsource = ();

for my $i (keys %essential) {
	push @visit, $i;
}
push @visit, "build-essential";

while (@visit) {
	my $n = pop @visit;
	next if grep /^\Q$n\E$/, @pp;
	push @pp, $n;
	my $source = $csource{$n};
	if ($source and !grep /^\Q$source\E$/, @vsource) {
		push @vsource, $source;
		for my $b (@{$bdeps{$source}}) {
			if (!grep /^\Q$b\E$/, @pp && !grep /^\Q$b\E$/, @visit) {
				push @visit, $b;
			}
		}
	}
	my $l = $depends{$n};
	for my $d (@$l) {
		if (!grep /^\Q$d\E$/, @pp && !grep /^\Q$d\E$/, @visit) {
			push @visit, $d;
		}
	}
}

my $tsize = 0;

for my $i (@pp) {
	$size{$i} = 0 if (!defined($size{$i}));
	print "$size{$i} $i\n";
	$tsize += $size{$i};
}

for my $i (@vsource) {
	$ssize{$i} = 0 if (!defined($ssize{$i}));
	print "$ssize{$i} S:$i\n";
	$tsize += $ssize{$i};
}

print "$tsize Total\n";
