JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix some spaces
[spectrwm.git] / baraction.sh
1 #!/bin/sh
2 #
3 # $scrotwm$
4
5 print_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 scrotwm.conf.  Uncomment "print_date" below.
9         FORMAT="%a %b %d %R %Z %Y"
10         DATE=`date "+${FORMAT}"`
11         echo -n "${DATE}     "
12 }
13
14 print_mem() {
15         MEM=`/usr/bin/top | grep Free: | cut -d " " -f7`
16         echo -n "Free mem: $MEM  "
17 }
18
19 _print_cpu() {
20         typeset -R4 _1=${1} _2=${2} _3=${3} _4=${4} _5=${5}
21         echo -n "CPU:${_1}% User${_2}% Nice${_3}% Sys${_4}% Int${_5}% Idle  "
22 }
23
24 print_cpu() {
25         OUT=""
26         # iostat prints each column justified to 3 chars, so if one counter
27         # is 100, it jams up agains the preceeding one. sort this out.
28         while [ "${1}x" != "x" ]; do
29                 if [ ${1} -gt 99 ]; then
30                         OUT="$OUT ${1%100} 100"
31                 else
32                         OUT="$OUT ${1}"
33                 fi
34                 shift;
35         done
36         _print_cpu $OUT
37 }
38
39 print_apm() {
40         BAT_STATUS=$1
41         BAT_LEVEL=$2
42         AC_STATUS=$3
43
44         if [ $AC_STATUS -ne 255 -o $BAT_STATUS -lt 4 ]; then
45                 if [ $AC_STATUS -eq 0 ]; then
46                         echo -n "on battery (${BAT_LEVEL}%)"
47                 else
48                         case $AC_STATUS in
49                         1)
50                                 AC_STRING="on AC: "
51                                 ;;
52                         2)
53                                 AC_STRING="on backup AC: "
54                                 ;;
55                         *)
56                                 AC_STRING=""
57                                 ;;
58                         esac;
59                         case $BAT_STATUS in
60                         4)
61                                 BAT_STRING="(no battery)"
62                                 ;;
63                         [0-3])
64                                 BAT_STRING="(battery ${BAT_LEVEL}%)"
65                                 ;;
66                         *)
67                                 BAT_STRING="(battery unknown)"
68                                 ;;
69                         esac;
70                 
71                         FULL="${AC_STRING}${BAT_STRING}"
72                         if [ "$FULL" != "" ]; then
73                                 echo -n "$FULL"
74                         fi
75                 fi
76         fi
77 }
78
79 print_cpuspeed() {
80         CPU_SPEED=`/sbin/sysctl hw.cpuspeed | cut -d "=" -f2`
81         echo -n "CPU speed: $CPU_SPEED MHz  "
82 }
83
84 while :; do
85         # instead of sleeping, use iostat as the update timer.
86         # cache the output of apm(8), no need to call that every second.
87         /usr/sbin/iostat -C -c 3600 |&  # wish infinity was an option
88         PID="$!"
89         APM_DATA=""
90         I=0
91         trap "kill $PID; exit" TERM
92         while read -p; do
93                 if [ $(( ${I} % 1 )) -eq 0 ]; then
94                         APM_DATA=`/usr/sbin/apm -alb`
95                 fi
96                 if [ $I -gt 2 ]; then
97                         # print_date
98                         print_mem $MEM
99                         print_cpu $REPLY
100                         print_cpuspeed
101                         print_apm $APM_DATA
102                         echo ""
103                 fi
104                 I=$(( ${I} + 1 ));
105         done
106 done