#!/bin/sh
# /usr/local/bin/tvcontrol

# apt-get install cec-utils

# /home/pi/.config/openbox/lxde-pi-rc.xml
# ...
#     <keybind key="XF86HomePage">
#       <action name="Execute">
#         <command>tvcontrol on</command>
#       </action>
#     </keybind>
# ...

print_usage () {
  echo "$0 {on,off,status}"
}

tv_on_as () {
  if [ "$(status)" = "power status: on" ]; then
    echo 'as' | cec-client -s
  else
    echo 'on 0' | cec-client -s
    sleep 20
    echo 'as' | cec-client -s
  fi
}

tv_off () {
  echo 'standby 0' | cec-client -s
}

status () {
  echo 'pow 0' | cec-client -s | grep 'power status:'
}

case "$1" in
  'on') tv_on_as ;;
  'off') tv_off ;;
  'status') status ;;
  *) print_usage ;;
esac