1#!/bin/sh
2# Example Bar Action Script for OpenBSD-current.
3#
4
5print_date() {
6	# The date is printed to the status bar by default.
7	# To print the date through this script, set clock_enabled to 0
8	# in spectrwm.conf.  Uncomment "print_date" below.
9	FORMAT="%a %b %d %R %Z %Y"
10	DATE=`date "+${FORMAT}"`
11	echo -n "${DATE}     "
12}
13
14print_mem() {
15	MEM=`/usr/bin/top | grep Free: | cut -d " " -f6`
16	echo -n "Free mem: $MEM  "
17}
18
19_print_cpu() {
20	printf "CPU: %3d%% User %3d%% Nice %3d%% Sys %3d%% Spin %3d%% Int %3d%% Idle  " $1 $2 $3 $4 $5 $6
21}
22
23print_cpu() {
24	OUT=""
25	# iostat prints each column justified to 3 chars, so if one counter
26	# is 100, it jams up agains the preceeding one. sort this out.
27	while [ "${1}x" != "x" ]; do
28		if [ ${1} -gt 99 ]; then
29			OUT="$OUT ${1%100} 100"
30		else
31			OUT="$OUT ${1}"
32		fi
33		shift;
34	done
35	_print_cpu $OUT
36}
37
38print_cpuspeed() {
39	CPU_SPEED=`/sbin/sysctl hw.cpuspeed | cut -d "=" -f2`
40	printf "CPU speed: %4d MHz  " $CPU_SPEED
41}
42
43print_bat() {
44	BAT_STATUS=$1
45	BAT_LEVEL=$2
46	AC_STATUS=$3
47
48	if [ $AC_STATUS -ne 255 -o $BAT_STATUS -lt 4 ]; then
49		if [ $AC_STATUS -eq 0 ]; then
50			echo -n "on battery (${BAT_LEVEL}%)"
51		else
52			case $AC_STATUS in
53			1)
54				AC_STRING="on AC: "
55				;;
56			2)
57				AC_STRING="on backup AC: "
58				;;
59			*)
60				AC_STRING=""
61				;;
62			esac;
63			case $BAT_STATUS in
64			4)
65				BAT_STRING="(no battery)"
66				;;
67			[0-3])
68		 		BAT_STRING="(battery ${BAT_LEVEL}%)"
69				;;
70			*)
71				BAT_STRING="(battery unknown)"
72				;;
73			esac;
74
75			FULL="${AC_STRING}${BAT_STRING}"
76			if [ "$FULL" != "" ]; then
77				echo -n "$FULL"
78			fi
79		fi
80	fi
81}
82
83# cache the output of apm(8), no need to call that every second.
84APM_DATA=""
85I=0
86while :; do
87	IOSTAT_DATA=`/usr/sbin/iostat -C | grep '[0-9]$'`
88	if [ $I -eq 0 ]; then
89		APM_DATA=`/usr/sbin/apm -alb`
90	fi
91	# print_date
92	print_mem
93	print_cpu $IOSTAT_DATA
94	print_cpuspeed
95	print_bat $APM_DATA
96	echo ""
97	I=$(( ( ${I} + 1 ) % 11 ))
98	sleep 1
99done
100