#!/usr/bin/bash
# Battery torture test.  It repeatedly plays a video, reads files in the
# root filesystem and the MMC card, and downloads from a (local) web server.
# NOTE: create /home/user/bin/videoplayer, a symlink to /usr/bin/video-player.

# Copyright (c) 2006 by James F. Carter (jimc@math.ucla.edu>
# 2006-08-27, for Maemo-2.0 (Nokia 770)

# 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 can find the GNU General Public License in your Maemo distribution
# as /usr/share/common-licenses/GPL-2; if it's missing, write to the 
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA 02111-1307, USA.


# Directory where temporary files are placed.
webdir=/tmp/battest.d
mkdir -p $webdir

# Starts the video player.  $1 = name of file to play.  If it's not already
# running, there will be a delay and it will miss the message with its file
# name.  Too bad.
function videostart () {
    local Name Exec
    eval `grep -v '\[' /usr/share/dbus-1/services/video-player.service`
		# There is no reply.  Path ($Exec) may not have hyphens or dots,
		# but it does: /usr/bin/video-player .  Use my replacement.
    Exec=/home/jimc/bin/videoplayer
    dbus-send --type=method_call --dest=$Name $Exec $Name.mime_open \
	string:file://$1
}

# Runs the graphics processor, DSP, and sound card.  This program will 
# start the video player by itself, or will re-use an existing instance.
function videoplayer () {
    local failed=3
    local rep=0
    local videolim=60
    local Name Exec dbmpid awkpid slppid t
    local fifo=$webdir/video-fifo
    local tmp=$webdir/video-signal
    local video=$HOME/MyDocs/.videos
    local videos=`echo $video/*.avi`
    if [ -z "$videos" ] ; then 
	echo "No videos in $video, exiting"
	exit 4
    fi
		# Start the video player and give a little time for that.
    videostart ${videos[0]}
    sleep 5
    while [ $failed -gt 0 ] ; do
      for video in $videos ; do
	rep=$((rep+1))
	echo "video   #$rep $video"
	start=`date +%s`
		# Start the video player, for real.
	videostart $video
		# Wait for the video to finish playing.
	rm -f $fifo
	mkfifo $fifo
		# Idiots, there is no service file for osso_media_server.
	Name=com.nokia.osso_media_server.video
	dbus-monitor "type=signal,interface=$Name" > $fifo &
	dbmpid=$!
	awk '{print} $0 ~ /end_of_stream/ {exit}' < $fifo > $tmp &
	awkpid=$!
	(sleep $videolim ; kill $awkpid 2> /dev/null ) &
	slppid=$!
	trap "kill $dbmpid $awkpid $slppid ; exit" INT TERM PIPE
	wait $awkpid
	kill $dbmpid $slppid 2> /dev/null
	t=`date +%s`
	t=$((t+10-start))
	if [ $videolim -lt $t ] ; then
	    echo "video   raising time limit from $videolim to $t"
	    videolim=$t
	fi
	sleep 3
	if grep 'playing' $tmp > /dev/null ; then : ; else
	    failed=$((failed-1))
	    echo "video failed to start, $failed lives remain"
	fi
      done
    done
}

# Keeps the memory etc. circulating.  
# $1 = directory to be thrashed.  $2 = disposition command.  
# $3 = redirect output here.  
# I've had command lines fail and I suspect that xargs has a low limit
# on the number of bytes in a command line.
function rummage () {
    local rep=0
    sleep 1
    while [ 0 -eq 0 ] ; do
	rep=$((rep+1))
	echo "rummage #$rep in $1"
	find $1 -xdev \( -type d ! -perm -05 -prune \) \
	    -o -type f -perm -04 -print0 | \
	    xargs -0 -n 50 -s 1024 $2 > $3 &
	trap "kill $! ; exit" INT TERM PIPE
	wait
    done
}

function cobweb () {
    local rep=0
    local url=http://fafnir/maemo/dists/mistral/cache/binary-armel/
    local urlbs=`echo $url | sed -e 's/\//\\\\\//g'`
    sleep 1
    rm -rf $webdir
    mkdir $webdir
    cd $webdir
	# Retrieve the index of the package cache.
	# -q=quiet -k=convert-links -nd=no directories
    wget -q -k -nd $url
    index=`eval echo *.html`
    if [ -z "$index" ] ; then
	echo "Can't find index file in $url, cobweb giving up"
	return 4
    fi
		# A. Ensure that each <a> is unique on a line.
		# B. Retain only links to the same directory; get rid of
		#	links to remote sites, of which there are many.
    sed -e 's/</#</g' $index | tr '#' '\012' | \
	    sed -e '/<a *href="'$urlbs'/!d' | sort -u > index.urls
		# Download those files.
    while [ 0 -eq 0 ] ; do
	rep=$((rep+1))
	echo "cobweb  #$rep in $url"
	    # wget args: -q=quiet -O=write to file -i=read URLs from file
	wget -q -O /dev/null --force-html -i index.urls &
	trap "kill $! ; exit" INT TERM PIPE
	wait
    done
}

# Invokes a background process.  $1 = its priority, rest is the command line.
function bkgd {
    local prty=$1
    shift
    $@ &
    renice $prty $! > /dev/null
    bkgdpid="$bkgdpid $!"
}

export DBUS_SESSION_BUS_ADDRESS=unix:path=/tmp/session_bus_socket

trap 'kill $bkgdpid ; exit' INT TERM PIPE

echo "Did you remember to turn the display to full brightness?"
echo -n "Starting " ; date

bkgd 10 rummage / cat /dev/null 

bkgd 19 rummage /media/mmc1 "grep -l Bill_Gates" /dev/null 

# wget takes a lot of CPU time.  So put it at a low priority.
bkgd 19 cobweb 

bkgd 0 videoplayer 

wait
rm -rf $webdir

