#!/usr/local/bin/perl

use strict;
use warnings;
use File::HomeDir qw(home);
use constant KILL_PATH => '/bin/kill';
use constant CONFIRM => [
	"No",
	"Yes"
	];

sub confirm
{
	my ($pid, $proc) = @_;
	my $list;
	my $choice;

	for (@{+CONFIRM}) {
		$list .= $_ . "\n";
	}
	$choice = `printf "%s" "$list" | dmenu -i -p "Kill $proc ($pid)?" -l 2 -m 0`;
	chomp $choice;
	if ($choice eq ${+CONFIRM}[1]) {
		return (1);
	}
	return (0);
}

sub action
{
	my ($var, $user) = @_;
	my @split;
	my $pid;
	my $proc;

	chomp $var;
	if (not $var) {
		return;
	}
	@split = split / $user /, $var;
	$pid = $split[0];
	$pid =~ s/\D//g;
	$proc = (split / /, $split[1])[0];
	if (confirm($pid, $proc) == 1) {
		exec(KILL_PATH, '-9', $pid);
	}
	return;
}

sub main
{
	my $choice;
	my $ps;
	my $user;

	$user = getlogin();
	$choice = `ps -U "$user" -o pid,user,comm,time,%cpu,%mem | dmenu -i -l 32 -m 0`;
	action($choice, $user);
	return (0);
}

main();

__END__