#!/bin/sh
# -*- coding: utf-8 -*-
# Batalarm - An application to check bat
#
# Version 0.1
#
# Authors: Francois TOURDE <fr-om@tourde.org>
#
# Copyright (c) 2008 François TOURDE
#
# batalarm is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# batalarm is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# TODO
#
# Change List
#
# 2008-11-17: First release

# First, the default values:
BAT_CAPACITY=/sys/devices/platform/bq27000-battery.0/power_supply/bat/capacity
BAT_PLUGGED=/sys/devices/platform/bq27000-battery.0/power_supply/bat/online
BAT_ALERT=/usr/share/batalarm/sounds/
SLEEP_INIT=600
#
# Les différents niveaux
THRESHOLDS="100:20:1200: 20:10:600:low 10:5:150:critical 5:0:30:emergency"
[ -f /etc/default/batalarm ] && . /etc/default/batalarm

while true
do
  v=$(cat $BAT_CAPACITY)
  for t in $THRESHOLDS
  do
    t=$(echo $t|sed -e 's/:/ /g')
    max=$(echo $t|awk '{print $1}')
    min=$(echo $t|awk '{print $2}')
    next=$(echo $t|awk '{print $3}')
    file=$(echo $t|awk '{print $4}')

    if [ $v -le $max -a $v -gt $min ]
    then
      SLEEP_VAL=$next
      ALERT_FILE=$file
      break;
    fi
  done

  [ ! -z $ALERT_FILE ] && [ $(cat $BAT_PLUGGED) != 1 ] && aplay ${BAT_ALERT}${ALERT_FILE}".wav"
  sleep $SLEEP_VAL
done
