JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Beware of stolen code for it was written on little endian and 32 bits.
[spectrwm.git] / baraction.sh
1 #!/bin/sh
2
3 _print_cpu() {
4         typeset -R4 _1=${1} _2=${2} _3=${3} _4=${4} _5=${5}
5         echo -n "CPU:${_1}% User${_2}% Nice${_3}% Sys${_4}% Int${_5}% Idle     "
6 }
7
8 print_cpu() {
9         OUT=""
10         # iostat prints each column justified to 3 chars, so if one counter
11         # is 100, it jams up agains the preceeding one. sort this out.
12         while [ "${1}x" != "x" ]; do
13                 if [ ${1} -gt 99 ]; then
14                         OUT="$OUT ${1%100} 100"
15                 else
16                         OUT="$OUT ${1}"
17                 fi
18                 shift;
19         done
20         _print_cpu $OUT
21 }
22
23 print_apm() {
24         BAT_STATUS=$1
25         BAT_LEVEL=$2
26         AC_STATUS=$3
27
28         if [ $AC_STATUS -ne 255 -o $BAT_STATUS -lt 4 ]; then
29                 if [ $AC_STATUS -eq 0 ]; then
30                         echo -n "on battery (${BAT_LEVEL}%)"
31                 else
32                         case $AC_STATUS in
33                         1)
34                                 AC_STRING="on AC: "
35                                 ;;
36                         2)
37                                 AC_STRING="on backup AC: "
38                                 ;;
39                         *)
40                                 AC_STRING=""
41                                 ;;
42                         esac;
43                         case $BAT_STATUS in
44                         4)
45                                 BAT_STRING="(no battery)"
46                                 ;;
47                         [0-3])
48                                 BAT_STRING="(battery ${BAT_LEVEL}%)"
49                                 ;;
50                         *)
51                                 BAT_STRING="(battery unknown)"
52                                 ;;
53                         esac;
54                 
55                         FULL="${AC_STRING}${BAT_STRING}"
56                         if [ "$FULL" != "" ]; then
57                                 echo -n "$FULL"
58                         fi
59                 fi
60         fi
61 }
62
63 while :; do
64         # instead of sleeping, use iostat as the update timer.
65         # cache the output of apm(8), no need to call that every second.
66         /usr/sbin/iostat -C -c 3600 |&  # wish infinity was an option
67         APM_DATA=""
68         I=0
69         while read -p; do
70                 if [ $(( ${I} % 59 )) -eq 0 ]; then
71                         APM_DATA=`/usr/sbin/apm -alb`
72                 fi
73                 if [ $I -gt 2 ]; then
74                         print_cpu $REPLY
75                         print_apm $APM_DATA
76                         echo ""
77                 fi
78                 I=$(( ${I} + 1 ));
79         done
80 done