#!/usr/bin/env bash

## Send a status message that includes the time, date, CPU usage and memory
## usage to a desktop notification daemon that conforms to the freedesktop.org
## Desktop Notifications Specification.
##
## Usage: status [<expire-time-in-milliseconds>]
##
## Dependencies: GNU awk, GNU coreutils, GNU sed, libnotify, procps, sysstat
##
## I run this script every half hour using systemd unit files and I run it
## manually using a window manager key binding. This script, along with my
## window manager's window switcher, eliminates the need for a taskbar.

set -euo pipefail
shopt -s inherit_errexit

die() { echo "${0##*/}: $1" >&2; exit 1; }

[[ " $* " =~ ' --help ' ]] && sed -n 's/^## \?//p' "$0" && exit
(( $# > 1 )) && die 'invalid argument'

expire_time=${1:-0}
datetime=$(date '+%-I:%M %p, %a, %b %-d')
cpu=$(mpstat 1 1 | awk 'END { print 100 - $12 }')
memory=$(free -h | awk 'NR == 2 { print $3 }')
message=$datetime$'\n'"CPU: $cpu%"$'\n'"Mem: $memory"
notify-send -t "$expire_time" "$message"
