#!/usr/local/bin/perl

use 5.040;
# use Encode;    # para que length imprima longitud en bytes y no chars

# Version mar 30 abr 2024 13:40:15 CEST
# Especial monitores curvos y anchos
# Versión vie 09 ago 2024 02:07:28 CEST
# Escala logarítmica con $cpuload < 0.5 y bug $cpuload 

no warnings 'all';   # wide chars

die "Wrong OS" if ( $^O !~ /x/i );
for (qw( tput uptime sensors )){
    die "$_ required. Stop" unless (length `which $_` > 1);
}

my $lines = `tput lines`;
my $cols = `tput cols`;
chomp $lines;
chomp $cols;

my (@tab, $tick);

my $magic = 9;    # 0..8
my $MAGIC_CPU = 4;  # every 0.25, one space

$tick = length (" 80ºC 19,20 \x{1F198}");                 # example de cuánto ocupa cada mensaje   <= 14
# $limit = int($cols / $tick);
$tab[$_] = int ( $_ * ($cols - $tick) / $magic ) for (0 .. $magic);

my ($cpuaverage, $cpuload, $index, $temp, $msg, $person);
say "·" x $tab[$magic-1], "$cols columns $lines lines"; #  "$lines lines $cols columns";
while (1){
    ### PERSONALIZACION ###
    $person = scalar( localtime() );
    if ( $person =~ /\s08\:/ || $person =~ /\s20\:/ ){ print "ATENCIÓN: Tomar medicinas. "; } 

    $cpuaverage = `uptime`;
    $cpuaverage =~ /load average: (\d+)[\,|\.](\d+)/;  # first \, is spanish; change to \. to english
    $cpuload = "$1.$2";         # format = computer
     if ($cpuload =~ /0\.0/){
	 if ($cpuload eq '0.00'){
	     # nothing to say 
	     print "\x{2B1C}";
	 }else{
	     # mostly ok
	     print "\x{1F44D}";
	 }
     }else{
#	 print "\x{1F915}";
	 print "\x{1F47E}";    # marcianito
     }
    
    # end of CPUload regex
    print " "  x int( 1+$MAGIC_CPU * $cpuload ) ;   # cpuload > 0.00 normally now???

    $temp = `sensors`;                      # Begin TEMPERATURE sensor 
    if ( $temp =~ /Package id 0\:  \+(\d+)\.\d+(.+C)\s/ ){         # i.e. temperature 65.0°C (celsius) no resolution by decimals ( .0 )
	$index = $1 ;  # composite index (integer) -> worst case uno + dos
	$msg = "$1$2 $cpuload";              # main info
	if ($index <= 30){
	    say " " x $tab[0], $msg, "\x{2705}";            # ok - muy bien
	}elsif ($index <= 40){
	    say " " x $tab[1], $msg, "\x{1F192}";           # cool - bien
	}elsif ($index <= 50){
	    say " " x $tab[2], $msg, "\x{1F4F6}";           # up - algo está en marcha
	}elsif ($index <= 60){
	    say " " x $tab[3], $msg, "\x{1F3C1}";           # race - algo tira de la cpu(s) 
	}elsif ($index <= 70){
	    say " " x $tab[4], $msg, "\x{1F419}";           # octopus - lo mismo, con fuerza
	}elsif ($index <= 80){
	    say " " x $tab[5], $msg, "\x{1F4DB}";           # ignition - se acerca a punto crítico 
	}elsif ($index <= 90){
	    say " " x $tab[6], $msg, "\x{1F525}";           # you should HALT the system - sugessted reboot, at least, 'shutdown -r now'
	}else{
	    say " " x $tab[7], $msg, "\x{1F198}";           # sos - STOP: Do you consider clean the dust and test fans inside the computer?
	}
    }else{
	say " " x $tab[8], "Error de sensor";
    }	
    sleep 60;    
}

exit 3;

__END__
  Util**3
El tema sobre el que gira la versión 2 es que haya 
  pocas variaciones pequeñas y solo se desplace por
  variaciones grandes.
  
