#!/usr/bin/perl
#
# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
# Copyright 2014 Vivien Didelot <vivien@didelot.org>
# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
#
# Licensed under the terms of the GNU GPL v3, or any later version.

use strict;
use warnings;
use utf8;
use Getopt::Long;
use open IO => ':encoding(UTF-8)';

# Thresholds in percent: the -w and -c options, else the block properties
# warning_threshold and critical_threshold, else the resources
# i3xrocks.cpu-usage.warning_threshold and i3xrocks.cpu-usage.critical_threshold.
my $t_warn = $ENV{'warning_threshold'} // `xrescat i3xrocks.cpu-usage.warning_threshold 50`;
my $t_crit = $ENV{'critical_threshold'} // `xrescat i3xrocks.cpu-usage.critical_threshold 80`;
my $cpu_usage = -1;

sub help {
    print "Usage: cpu_usage [-w <warning>] [-c <critical>]\n";
    print "-w <percent>: warning threshold, shown in i3xrocks.warning (default 50)\n";
    print "-c <percent>: critical threshold, shown in i3xrocks.critical.color (default 80)\n";
    exit 0;
}

GetOptions("help|h" => \&help,
           "w=i"    => \$t_warn,
           "c=i"    => \$t_crit);

# Get CPU usage
$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
open (MPSTAT, 'mpstat 1 1 |') or die;
while (<MPSTAT>) {
    if (/^Average.*\s+(\d+\.\d+)/) {
        $cpu_usage = 100 - $1; # 100% - %idle
        last;
    }
}
close(MPSTAT);

$cpu_usage eq -1 and die 'Can\'t find CPU information';

# Get color values
my $label_color = $ENV{'label_color'} || `xrescat i3xrocks.label.color #7B8394`;
my $value_color = $ENV{'color'} || `xrescat i3xrocks.value.color #D8DEE9`;
my $value_font = $ENV{'font'} || `xrescat i3xrocks.value.font "Source Code Pro Medium 13"`;
my $value_icon = $ENV{'icon'} || `xrescat i3xrocks.label.cpu `;
if ($cpu_usage >= $t_crit) {
    $value_color = $ENV{'critical_color'} || `xrescat i3xrocks.critical.color "#BF616A"`;
} elsif ($cpu_usage >= $t_warn) {
    $value_color = $ENV{'warning_color'} || `xrescat i3xrocks.warning "#EBCB8B"`;
}

# Print short_text, full_text
binmode STDOUT, ":utf8";
printf "<span font_desc='%s' color='%s'>%s</span><span font_desc='%s' color='%s'> %02d%%</span>\n", $value_font, $label_color, $value_icon, $value_font, $value_color, $cpu_usage;
printf "<span font_desc='%s' color='%s'> %02d%%</span>\n", $value_font, $value_color, $cpu_usage;

# Left click only: scrolling over the bar should not open anything.
if (($ENV{'button'} // '') eq '1') {
    # gnome-system-monitor takes --class=floating_window up to GTK 3 (42), ignores
    # it in 46 and rejects it from 48 on. GTK 4 builds take their window class
    # from argv[0], so also start it as floating_window, and retry without
    # --class when it is rejected. The window floats in all of them.
    my $action = `xrescat i3xrocks.action.cpu-usage 'bash -c "(exec -a floating_window /usr/bin/gnome-system-monitor --class=floating_window) || exec -a floating_window /usr/bin/gnome-system-monitor"'`;
    system("/usr/bin/i3-msg -q exec '$action'");
}

exit 0;
