#!/usr/bin/perl -w

# dwm-status.pl
#
# 05/26/07 23:08:31 CEST Dobrica Pavlinusic <dpavlin@rot13.org>

use strict;
use POSIX qw/strftime/;
use File::Slurp;
use Time::HiRes;
use Data::Dump qw/dump/;

my $dt = 3;
my $acpi_every = 10;

my $disk_blk_size = 512;

my $debug = shift @ARGV;
my $awesome = 0;

$|=1;

sub proc2hash {
	my $f = shift;
	return unless -f $f;
	open(my $fh, '<', $f) || die "can't open $f: $!";
	my $h;
	while(<$fh>) {
		chomp;
		my ( $key, $value ) = split(/:\s+/, $_, 2);
		$value =~ s/ m[AVW]h*$//;
		$h->{$key} = $value;
	}
	warn "$f ",dump( $h ) if ( $debug );
	return $h;
}

my $proc_acpi_battery;
sub find_proc_acpi {
	my ( $path, $check ) = @_;

	if ( -e "/proc/acpi/$path" ) {
		opendir(my $items, "/proc/acpi/$path") || die "can't open /proc/acpi/$path: $!";
		foreach my $item ( readdir( $items ) ) {
			$check->( $item ) && last;
		}
	}
}

my $proc_acpi_thermal_zone_temperature;
find_proc_acpi('thermal_zone', sub {
	my $thm = shift;
	return if ( ! -e "/proc/acpi/thermal_zone/$thm/temperature" );
	warn "# using thermal zone $thm\n" if ( $debug );
	$proc_acpi_thermal_zone_temperature = "/proc/acpi/thermal_zone/$thm/temperature";
});

find_proc_acpi('battery', sub {
	my $bat = shift;
	return unless $bat =~ /\w+/;

	warn "# testing battery $bat\n" if ( $debug );

	if ( proc2hash( "/proc/acpi/battery/$bat/info" )->{present} eq 'yes' ) {
		$proc_acpi_battery = "/proc/acpi/battery/$bat";
		warn "using $proc_acpi_battery to monitor battery\n";
	}
});

sub unit {
	my $v = shift;

	warn "unit( $v )\n" if ($debug);

	my @units = qw/b k M G/;
	my $o = 0;

	while ( ( $v / 10000 ) >= 1 ) {
		$o++;
		$v /= 1024;
	}

	if ( $v >= 1 ) {
		return sprintf("%d%s", $v, $units[$o]);
	} elsif ( $v == 0 ) {
		return '';
	} else {
		return sprintf("%.1f%s", $v, $units[$o]);
	}
}

my ( $lrx, $ltx ) = ( 0, 0 );
my ( $ld_r, $ld_w ) = ( 0, 0 );
my $bat = '';

my $i = 0;
my $sys_fs = '/sys/class/power_supply/BAT0';

while ( 1 ) {
	my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());

	if ( $i % $acpi_every == 0 ) {
		my $sysfs_path = glob "$sys_fs/*_full";
		if ( $sysfs_path ) {

			my $full = read_file( $sysfs_path );
			$sysfs_path =~ s/_full/_now/;
			my $now = read_file( $sysfs_path );
			$bat = sprintf("%2d%% | ", $now * 100 / $full );

		} elsif ( $proc_acpi_battery ) {

			my $state = proc2hash( "$proc_acpi_battery/state" );

			if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) {
				my $info = proc2hash( "$proc_acpi_battery/info" );

				my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};

				my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
				$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );

				warn "time = $time\n" if ($debug);

				my $hh = int( $time );
				my $mm = int( ( $time - $hh ) * 60 );
				my $ss = ( $time * 3600 ) % 60;

				$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
					substr($state->{'charging state'},0,1),
					$pcnt * 100, $hh, $mm, $ss,
					$state->{'present rate'} / 1000
				);
			}
		}
	} else {
		$bat =~ s/!(\|\s)$/ $1/;
	}
	$i++;

	my $load = read_file('/proc/loadavg');
	chomp( $load );
	$load =~ s!\s\d+/\d+.*!!;

	my $temp = '';
	if ( $proc_acpi_thermal_zone_temperature ) {
		$temp = read_file( $proc_acpi_thermal_zone_temperature );
		chomp( $temp );
		$temp =~ s!^.*:\s+!!;
	}

	my $net = read_file('/proc/net/dev');
	my ( $rx, $tx ) = ( 0,0 );

	foreach ( split(/\n/, $net) ) {
		s/^\s+//;
		s/:\s*/ /;
		my @n = split(/\s+/, $_, 17);
		#next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
		next unless $n[1] =~ m{^\d+$};

		warn dump( @n ) if ($debug);
		$rx += $n[1];
		$tx += $n[9];
	}
	warn "rx: $rx tx: $tx\n" if ($debug);

	my $net_rx = ( $rx - $lrx ) / $dt;
	my $net_tx = ( $tx - $ltx ) / $dt;
	( $lrx, $ltx ) = ( $rx, $tx );

	my $disk = read_file('/proc/diskstats');
	my ( $d_r, $d_w ) = ( 0,0 );

	foreach ( split(/\n/, $disk) ) {
		s/^\s+//;
		my @d = split(/\s+/, $_, 17);
		next unless ( $d[2] =~ m/^[sh]d\w$/ );

		warn dump( @d ) if ($debug);
		$d_r += $d[5] * $disk_blk_size;
		$d_w += $d[7] * $disk_blk_size;
	}
	warn "d_r: $d_r d_w: $d_w\n" if ($debug);

	my $d_read = ( $d_r - $ld_r ) / $dt;
	my $d_write = ( $d_w - $ld_w ) / $dt;
	( $ld_r, $ld_w ) = ( $d_r, $d_w );

	my $out = sprintf("%s |%5s D %-5s|%5s > %-5s| %s%s | %s",
		$load,
		unit( $d_read ), unit( $d_write ),
		unit( $net_rx ), unit( $net_tx ),
		$bat, $temp,
		$s,
	);

	print "$out\n";
	if ( $awesome ) {
		open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!";
		print $fh
			$awesome == 3	? qq{mytextbox.text="$out"\n}
					: "0 widget_tell mystatusbar dwm-status text $out\n"
					;
		close($fh);
	}

	sleep $dt;
}

