scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

acpi (973B)


      1 #!/bin/bash
      2 
      3 STATE=mem
      4 BATTCRITIC=9
      5 BATTWARNING=12
      6 ACPLUGGED="$(cat $(du -a /sys/devices |grep 'AC0/online' |awk '{print $2}'))"
      7 LIDSTATE="$(cat /proc/acpi/button/lid/LID/state |awk '{print $2}')"
      8 
      9 export DISPLAY=:0
     10 export XAUTHORITY=~/.Xauthority
     11 
     12 warning() {
     13 	pct="$1"
     14 	L=" OK "
     15 
     16 	xmessage -center -buttons "$L" -default "$L" "WARNING: battery level is ${pct}%" &
     17 
     18 	for i in $(seq 3); do
     19 		play -qn synth 00:00.05 sine $(echo "800 + (30 * $i)" |bc)
     20 	done
     21 }
     22 
     23 suspend() {
     24 	echo -n $STATE > /sys/power/state
     25 }
     26 
     27 getbattery() {
     28 	enow=$(cat /sys/class/power_supply/BAT0/energy_now)
     29 	efull=$(cat /sys/class/power_supply/BAT0/energy_full)
     30 	vnow=$(cat /sys/class/power_supply/BAT0/voltage_now)
     31 
     32 	r=$(echo "($enow * 100 / $vnow) * 100 / ($efull * 100 / $vnow)" |bc)
     33 	echo $r
     34 }
     35 
     36 main() {
     37 	[ $LIDSTATE = "closed" ] && suspend
     38 	[ $ACPLUGGED -eq 1 ] && exit
     39 
     40 	t=$(getbattery)
     41 	if [ $t -le $BATTCRITIC ]; then
     42 		suspend
     43 	elif [ $t -le $BATTWARNING ]; then
     44 		warning $t
     45 	fi
     46 }
     47 
     48 main "$@"