#!/usr/bin/env perl

use strict;
use warnings;

our %cds;

open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";


sub hashValueDescendingNum {
    $cds{$b} <=> $cds{$a};
}

while (<HISTORY>) {
    my $command = $_;

    if ($command =~ m/^$/) {
        next;
    }

    my @comm = split(' ', $command);

    if ($comm[0] eq "sudo") {
        shift @comm;
    }

    if (not $comm[0] eq "cd") {
        next;
    }

    if (exists $comm[1]) {
        if (not exists($cds{$comm[1]})) {
            $cds{$comm[1]} = 0;
        }
        $cds{$comm[1]}++;
    } else {
        if (not exists($cds{"HOME"})) {
            $cds{"HOME"} = 0
        }
        $cds{"HOME"}++;
    }
}

foreach my $key (sort hashValueDescendingNum (keys(%cds))) {
    my $value = sprintf "%8s", $cds{$key};
    print "$value\t$key\n";
}
