#!/bin/sh
###############################################################################
# dwm-battery-status
#
# Returns a battery status string.
#
# -----------------------------------------------------------------------------
# DWM Scripts - wrapper for dwm/dmenu/slock
#   (C) 2007-2009 Gerardo García Peña
#   Programmed by Gerardo García Peña
#
#   This program 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 2 of the License, or (at your option)
#   any later version.
#
#   This program 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.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc., 51
#   Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
###############################################################################

. dwm-config-read

#------------------------------------------------------------------------------
# POWERSAVE IMPLEMENTATION
#------------------------------------------------------------------------------

bts_powersave_check()
{
  which powersave > /dev/null 2>&1
  return $?
}

bts_powersave()
{
  # get bat status and save it into temporal file
  powersave -b > "$BATSTAT"

  # build the message
  grep "AC.*offline" "$BATSTAT" 1>&2
  if grep "AC.*offline" "$BATSTAT" > /dev/null 2>&1; then
    if grep "Battery" "$BATSTAT" > /dev/null 2>&1; then
      grep Battery "$BATSTAT"         \
        | sed -e 's#\([0-9]\) %\?#\1%#' \
              -e 's# remaining.*$##'  \
              -e 's#minutes#min#'
    elif grep "Remaining percent" "$BATSTAT" > /dev/null 2>&1; then
      P=`grep "Remaining percent" "$BATSTAT" | sed 's#^[^0-9]*\([0-9]*\)[^0-9]*$#\1#'`
      M=`grep "Remaining minutes" "$BATSTAT" | sed 's#^[^0-9]*\([0-9]*\)[^0-9]*$#\1#'`
      echo "$M min $P%"
    else
      error "powersave syntax not recognized."
    fi
  else
    if grep "Battery" "$BATSTAT" > /dev/null 2>&1; then
      if ! grep "100 %" "$BATSTAT" > /dev/null 2>&1; then
        grep Battery "$BATSTAT" \
          | sed 's#^[^0-9]*\([0-9]\+\) %.*$#Charging \1%#'
      fi
    elif grep "Remaining percent" "$BATSTAT" > /dev/null 2>&1; then
      P=`grep "Remaining percent" "$BATSTAT" | sed 's#^[^0-9]*\([0-9]*\)[^0-9]*$#\1#'`
      M=`grep "minutes until" "$BATSTAT" | sed 's#^[^0-9]*\([0-9]*\)[^0-9]*$#\1#'`
      echo "Charging $M min $P%"
    else
      error "powersave syntax not recognized."
    fi
  fi
}

#------------------------------------------------------------------------------
# ACPI IMPLEMENTATION
#------------------------------------------------------------------------------

bts_acpi_check()
{
  which acpi > /dev/null 2>&1
  return $?
}

bts_acpi()
{
  # get bat status and write it to temp file
  acpi -V > "$BATSTAT"

  # build the message
  if grep "AC Adapter.*off-line" "$BATSTAT" > /dev/null 2>&1; then
    grep Battery "$BATSTAT"     \
      | sed -e 's#^[^,]*, ##'   \
            -e 's# remaining##'
  else
    if ! grep "100%" "$BATSTAT" > /dev/null 2>&1; then
      grep Battery "$BATSTAT" \
        | sed 's#^.* \([0-9]*%\).*$#Charging \1#'
    fi
  fi
}

#==============================================================================
# MAIN PROGRAM
#==============================================================================

# create temp file
BATSTAT="`mktemp $DSW_TMP/batstat.XXXXXXXXX`"

if bts_powersave_check; then
  bts_powersave
else
  if bts_acpi_check; then
    bts_acpi
  else 
    log "No battery status software (powersave/acpi)"
  fi
fi

# delete temp
rm -f -- "$BATSTAT"

exit 0
