#!/usr/local/bin/perl

use 5.040.2;
# 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 

# mié 12 ago 2026 23:15:02 CEST
# mié 12 ago 2026 21:15:09 GMT
# version 5.040.2

# Selfactualization -+- Maslow, 1956 (ethic, aestetic and cultural) 
# mié 27 may 2026 12:36:00 CEST
# mié 27 may 2026 10:36:06 GMT  

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;

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

my $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 $regex = qr/load average: (\d+)[\,|\.](\d+)/;

my ($cpuaverage, $cpuload, $index, $temp, $msg, $person);
say "·" x $tab[$magic], "$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 =~ /$regex/i;   # first \, is spanish add \. to english  && capture
    $cpuload = "$1.$2";         # format = computer
#    if ($cpuload =~ /0[\.|\,]/){
    if ( $cpuload =~ m/0[\.|\,]0[0-8]/ ){   # aquí debería entrar: 0.00 0.01 0.02 y 0.03  /* razonable para tanto core */
	# nothing to say: best state of art 
	print "\x{2B1C}";                                                     # WHITE
    }elsif ( $cpuload =~ m/0[\.|\,][0-4]\d/ ){
	# mostly ok
	print "\x{1F44D}";                # hand doing 'up'
    }else{
#	 print "\x{1F915}";
#	 print "\x{1F47E}";    # marcianito
#	 print "\x{1F3D3}";    # deporte
	 print "\x{1F43E}";    # huellas
    }
    
    # end of CPUload regex
    print " "  x int( $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 " " . $msg . "🧊";                          # cubito de hielo antiguo
	}
	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{1F387}";           # de la host a la cpu se ven estrellas  🎆
	}elsif ($index <= 90){
	    say " " x $tab[6], $msg, "\x{1F525}";           # flames 
	}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.
  
