#!/bin/bash

echo 0 > /sys/class/graphics/fbcon/cursor_blink
setterm -blank force </dev/tty0 >/dev/tty0
startdate=`date +%s`

resample_method="speex-float-1"
eval `grep -o 'resample_method=[^ ]*' /proc/cmdline`

sample_rate=44100
eval `grep -o 'sample_rate=[^ ]*' /proc/cmdline`

test_case=paplay
eval `grep -o 'test_case=[^ ]*' /proc/cmdline`

echo "default-sample-rate = $sample_rate" >> /etc/pulse/daemon.conf
echo "alternate-sample-rate = $sample_rate" >> /etc/pulse/daemon.conf
echo "resample-method = $resample_method" >> /etc/pulse/daemon.conf

mount /boot
. /boot/battery-eater/tests/$test_case.sh
umount /boot

pulseaudio --start
sleep 5
pulsepid=`pidof pulseaudio`

while :; do
    now=`date +%s`
    read energy_now < /sys/class/power_supply/BAT1/energy_now
    pulsetimes=`cut -d ' ' -f 14,15 /proc/$pulsepid/stat`

    echo $(( $now - $startdate )) $energy_now $pulsetimes >> report.txt
    if [ "$energy_now" -lt 1000000 ] ; then
        mount /boot
	cp report.txt /boot/battery-eater/report-$startdate.txt
	umount /boot
	sync
	poweroff
	exit
    fi
    eat_battery
done
