#! /bin/sh

REFRESH=5

SMAPI_PATH="/sys/devices/platform/smapi"

if [ ! -d $SMAPI_PATH ] ; then
  echo "Your kernel doesn't seem to support SMAPI."
  exit 1
fi

while getopts "r:" OPTION ; do
  case $OPTION in
    r) REFRESH="$OPTARG" ;;
  esac
done

while true ; do 
  BAT=0
  BAT_MAX=`ls -ld $SMAPI_PATH/BAT* | tail -n 1 | sed -e "s#^.*/##g" -e "s/^BAT//"`
  clear
  while [ $BAT -le $BAT_MAX ] ; do
    if [ `cat $SMAPI_PATH/BAT$BAT/installed` = 1 ] ; then
      echo "BAT$BAT:"
      for SYSFILE in manufacturer model state remaining_percent start_charge_thresh stop_charge_thresh remaining_running_time remaining_running_time_now remaining_capacity last_full_capacity design_capacity cycle_count ; do
        echo "$SYSFILE: `cat $SMAPI_PATH/BAT$BAT/$SYSFILE`"
      done
      if ! `which bc >/dev/null` ; then
        echo "Please install bc for capacity calculation."
      else
        echo "capacity: $(echo "scale=1; `cat $SMAPI_PATH/BAT$BAT/last_full_capacity`*100/`cat $SMAPI_PATH/BAT$BAT/design_capacity`" | bc -q)%"
      fi
      echo
    fi
    BAT=$((BAT+1))
  done
  echo -n "Press Ctrl-C to stop."
  sleep $REFRESH
done
