#!/bin/bash

set -em #x

#===========TWEAKABLE VARIABLES=============
# Sensor info. Unless you have the same motherboard I do you'll need to change
# these.
SENSOR=/sys/bus/i2c/devices/0-0290/temp1_input # CPU sensor file
DIVISOR=1000 # divide the value of the sensor file by this

# Test parameters. Everything is set to be rather fast; more of a proof of
# concept than an intensive test.
INTERVAL=2 # seconds between readings
SAMPLES=60 # number of samples to get
COOLDOWN=60 # wait this long between tests
ALL_TESTS='idle bzip2 burnBX burnMMX burnP5 burnP6 burnK6 burnK7 cpuburnin '\
'mencoder prime95 gpg glxgears' # list of all the tests

# Gnuplot graph info. More stuff to tweak is down near the end.
G_YRANGE="[30:80]" # min and max temperature values
G_TERMINAL="png size 800,600" # terminal ("output type and info")
G_LINESTYLE="linespoints lw 3" # plot line style


#=============TEST FUNCTIONS===============
test_idle(){
  sleep 999999d # just about forever
}
test_bzip2(){
  bzip2 < /dev/urandom > /dev/null
}
test_burnBX(){
  dchroot burnBX
}
test_burnMMX(){
  dchroot burnMMX
}
test_burnP5(){
  dchroot burnP5
}
test_burnP6(){
  dchroot burnP6
}
test_burnK6(){
  dchroot burnK6
}
test_burnK7(){
  dchroot burnK7
}
test_cpuburnin(){
  cpuburn-in 999999 # run pretty much forever
}
test_mencoder(){
  # just a couple high-quality options (not all of them)
  mencoder -rawvideo on:fps=24:w=480:h=320 -nosound -ovc lavc -lavcopts \
  vcodec=mpeg4:mbd=2:mv0:trell:cbp /dev/urandom -o /dev/null
}
test_prime95(){
  mprime -t
}
test_gpg(){
  echo -e "blah\nblah" | \
  gpg --passphrase-fd 0 --symmetric < /dev/urandom > /dev/null
}
test_glxgears(){
  glxgears
}

# log the temperatures for a command
# $1 is command to run
# $2 is name of test (for graph)
log(){
  if [ "$testnum" ] ; then
    ((++testnum))
  else
    testnum=0
  fi
  echo "Running test: $1"
  # set the name
  echo "$2" > test$testnum.name
  # start the test
  $1 &> test$testnum.log &
  # read temperature
  for ((i=0 ; i < $SAMPLES ; ++i)) ; do
    temp=$(($(cat $SENSOR) / $DIVISOR))
    elapsed=$((i * INTERVAL))
    echo $elapsed $temp | tee -a test$testnum.data
    sleep $INTERVAL
  done
  # stop test
  kill %%
  echo -n "Test complete. "
  # foreground to let the test complete peacefully
  # (if it hasn't already died)
  fg %% || :
  # cool down
  echo "Sleeping $COOLDOWN for cooldown."
  sleep $COOLDOWN
}

# really fancy argument parsing
if [ ! "$1" ] ; then
  cat << EOF
Usage: ./temptests.sh [ 'all' | 'none' | list-of-tests ]

Tests are defined in the "TEST FUNCTIONS" section. Read the script file. To run
one or more tests, list them as arguments (without the "test_" part.  To run all
the tests, specify 'all' instead of a list. To run no tests but still re-plot
any available data from a previous run, specify 'none'. This is useful when
tweaking the gnuplot output.

WARNING:
This script should be run in an empty directory. It makes and removes files
without checking first.

Before this script will work, you'll need to look at the top of this script and
change some of the variables in the "TWEAKABLE VARIABLES" section.

You also might need to look at the "TEST FUNCTIONS" section where each test is
defined. Each test is intended to run infinitely; the script kills the test
at the appropriate time. If the test has already died for any reason the script
will quit. So, check each function to see if it'll run on your system. If you
don't want to run a test at all, remove it from the ALL_TESTS variable near the
top.
EOF
  exit 1
elif [ "$1" = "none" ] ; then
  : # do nothing
else
  rm -f *.name *.data *.log
  if [ "$1" = "all" ] ; then
    enabled_tests=$ALL_TESTS
  else
    enabled_tests="$@"
  fi
  for testname in $enabled_tests ; do
    log test_$testname $testname
  done
fi


echo -n "All tests complete. Graphing... "
GPC=gpc # gnuplut commands file
cat > $GPC << EOF
set title "CPU Temperature"
set terminal $G_TERMINAL
set yrange $G_YRANGE
set output "graph.png"
set xlabel "Elapsed Time (seconds)"
set ylabel "Temperature (C)"
set ytics 5
set grid
set key outside
set pointsize 0.2
set lmargin 10
set rmargin 20
EOF


# Ugly. 
echo -n "plot 'test0.data' using 1:2 title '$(cat test0.name)' with $G_LINESTYLE" >> $GPC
testnum=1
while name=$(cat "test$testnum.name" 2>/dev/null) ; do
  echo -n ", 'test$testnum.data' using 1:2 title '$name' with $G_LINESTYLE" >> $GPC
  ((++testnum))
done
echo >> $GPC

gnuplot $GPC
echo done

exit 0
