--- /dev/null
+#!/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");
+open(SOURCES, "<Sources");
+
+my @packages = ();
+my %depends = ();
+my %priority = ();
+my %essential = ();
+
+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";
+ }
+}
+
+close(PACKAGES);
+close(SOURCES);
+
+my @pp = ();
+
+my @visit = ();
+
+for my $i (keys %essential) {
+ push @visit, $i;
+}
+push @visit, "build-essential";
+
+while (@visit) {
+ my $n = pop @visit;
+ next if grep /^$n$/, @pp;
+ push @pp, $n;
+ my $l = $depends{$n};
+ for my $d (@$l) {
+ if (!grep /^$d$/, @pp && !grep /^$d$/, @visit) {
+ push @visit, $d;
+ }
+ }
+}
+
+for my $i (@pp) {
+ print "$i\n";
+}