#!/usr/bin/perl -W
# debstats produces statistics of a debian-package system
# © 2004 Pierre Chignac
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

open('status',"< /var/lib/dpkg/status");
$totsize=0;
$max_car=0;
$totpackages=0;
while (<status>) {
    if ($_ =~ 'ok installed') {
    # scan types of installed packages
    $prior=<status>;
    $prior =~ s/Priority:\s+//; $prior =~ s/\n//;
    $section=<status>;
    $section=~ s/Section:\s+//; $section =~ s/\n//;
    $size=<status>;
    $size =~ s/Installed-Size:\s+//; $size =~ s/\n//;
    # calculate sizes
    $totsize+=$size;
    $totpackages+=1;
    $tab_prior{$prior}+=$size;
    $tab_section{$section}+=$size;
    $tab_prior_count{$prior}+=1;
    $tab_section_count{$section}+=1;
    }
}
foreach (keys %tab_prior) {
    if ($max_car<length($_)) { $max_car=length($_); }
}
foreach (keys %tab_section) {
    if ($max_car<length($_)) { $max_car=length($_); }
}
$blank="-"x($max_car-9);
print "$blank Priority Size(Mo)\tPackages\n";
foreach (keys %tab_prior) {
    $blank=" "x($max_car-length($_));
    printf("$blank%s %d\t%d\n", $_, $tab_prior{$_}/1024, $tab_prior_count{$_});
}
$blank="-"x($max_car-8);
print "$blank Section Size(Mo)\tPackages\n";
foreach (keys %tab_section) {
    $blank=" "x($max_car-length($_));
    if (($tab_section{$_}/1024)>6) {
    printf("$blank%s %d\t%d\n", $_, $tab_section{$_}/1024, $tab_section_count{$_});
    }
    else {
    printf("$blank%s %.2f\t%d\n", $_, $tab_section{$_}/1024, $tab_section_count{$_});
    }
}
$blank="-"x($max_car-6);
printf("$blank Total %d\t%d\n", $totsize/1024, $totpackages);
close 'status';