#!/usr/bin/env perl

use strict;
use warnings;

our %sshs;

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


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

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

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

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

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

    if (($comm[0] eq "ssh-add") or ($comm[0] eq "add-ssh-keys")) {
        if (not exists($sshs{"KEY-MANAGEMENT"})) {
            $sshs{"KEY-MANAGEMENT"} = 0;
        }
        $sshs{"KEY-MANAGEMENT"}++;
        next;
    }

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

    if (exists $comm[1]) {
        if (not exists($sshs{$comm[1]})) {
            $sshs{$comm[1]} = 0;
        }
        $sshs{$comm[1]}++;
    }
}

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