#!/bin/sh
# powerOn v0.1
# Tim Kleingeld - Sep 2004

# Tool to turn on your vcr/stb.
# First, ensure irlock is installed
# Next, extract your remote codes to /hack/remoteCodes. Something like
# echo 'dumpobj /Component/Ir/TivoFormat/30035' | tivosh > /hack/remoteCodes
# Where 30035 is your remote's id in the tivo database.
# If it doesn't have PowerToggle or PowerOn codes, you'll have to capture
# these yourself. See: http://minnie.tuhs.org/twiki/bin/view/IR/WebHome

# What's missing: 
#  - Auto extraction of remote codes by traversing mfs & objects
#  - Setting the channel to what the TiVo thinks it should be

codes=/hack/remoteCodes
irlock=/hack/bin/irlock
irtest=/sbin/irtest
tty=/dev/ttyS0

if [ ! -f $codes -o ! -f $irlock ]; then
  echo "You must have both $codes and $irlock" 1>&2
  exit 1
fi

if grep -q PowerOn $codes; then
	# We have a PowerOn code - just use it without bothering to check
	$irlock $irtest -t $tty -- `grep PowerOn $codes | cut -d= -f2`
	exit 0
fi

if grep -q PowerToggle $codes; then
	# We have a PowerToggle... cool...
	if grep tuning:lock /var/log/kernel | tail -1 | grep -q 'tuning:lock 0'; then
		$irlock $irtest -t $tty -- `grep PowerToggle $codes | cut -d= -f2`
  fi
fi
