#!/bin/sh
#
# This file is part of the battery-stats package.
# Copyright (C) 2016 Petter Reinholdtsen <pere@hungry.com>
#
# 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

set -e

get_logline() {
    secstamp=$(date +%s)
    stamp=$(date +"%Y/%m/%d %H:%M:%S")

    aconline=$(cat /sys/class/power_supply/AC/online)
    if [ 1 = "$aconline" ]; then
	state=2
    else
	state=1
    fi

    for f in /sys/class/power_supply/BAT*; do
	energy_now=$(cat $f/energy_now) # uWh
	energy_full=$(cat $f/energy_full) # uWh
	percent=$( (echo scale=2; echo "100 * $energy_now / $energy_full") | bc -l)

	# FIXME figure out how to calculate minutes remining the same
	# way libacpi calculate it.
	# Ideas:
	if false; then
	    power_now=$(cat $f/power_now) # uW
	    voltage_now=$(cat $f/voltage_now) # uV
	    rate=$(( 1000000 * $power_now / $voltage_now))
	    if [ 0 = $rate ] ; then
		minremaining=0
	    else
		minremaining=$(( 60 * $energy_now / $rate ))
	    fi

            # 10^6 * uW / uV  = uA
	    remaining_capasity=$((1000000 * $energy_now / $voltage_now ))
            # 10^6 * uA / uW = 1 / mV
	    minremaining=$((1000000 * $remaining_capasity / $power_now))
	fi
	break
    done

    # Use '-' for minutes remaining, as this value isn't used by the
    # graph drawing system.
    echo $secstamp $percent $state $stamp -
}

usage() {
    cat <<EOF
Usage: $0 [-o logfile]
EOF
}

PROGRAM_NAME=$(basename $0)
logfile="/var/log/battery-stats"
sample_interval_secs=$((10 * 60)) # 10 minutes
syslog=false

TEMP=$(getopt -n $PROGRAM_NAME --options o:i:V1hsF:Ib: \
    --longoptions output:,interval:,version,once,help,syslog,\
flush:,ignore-missing-battery,battery-num: \
    -- "$@")
eval set -- "$TEMP"
while true; do
        case "$1" in
            -o|--output)
		logfile="$2"; shift 2; continue
                ;;
	    -i|--interval)
		sample_interval_secs="$2"; shift 2; continue
		;;
	    -V|--version)
		echo "Version: someversion"
		exit 1
		;;
	    -1|--once)
		sample_interval_secs=0; shift; continue ;;
	    -h|--help)
		usage; shift; exit 0 ;;
	    -s|--syslog)
		# FIXME Ignored
		syslog=true; shift; continue ;; 
	    -F|--flush)
		# FIXME Ignored
		shift 2; continue ;;
	    -I|--ignore-missing-battery)
		# FIXME Ignored
		shift ; continue ;;
	    -b|--battery-num)
		# FIXME Ignored
		shift 2; continue ;;
	    --) # no more arguments to parse
		break
		;;
            *)
                printf "Unknown option %s\n" "$1"
                exit 1
                ;;
	esac
done

while true; do
    if [ "$logfile" ] ; then
	get_logline >> $logfile
    else
	get_logline
    fi
    if [ 0 -lt $sample_interval_secs ] ; then
	sleep $sample_interval_secs
    else
	break
    fi
done
