#!/bin/sh
#
# By TheOe
#
# Version 0.1 - December 2013
#

CECLOG=/tmp/cec.log
CECDEV=
CECCLIENT="/usr/bin/cec-client -d 8 -t r -f $CECLOG $CECDEV"
SVDRPSEND="/usr/bin/svdrpsend.sh"

log(){
    echo "ACTION:  $1" >> $CECLOG
}

case "${1}" in
        start)

                . /etc/init.d/rc.functions

                # cec traffic to command
                tail -f $CECLOG |
                while read; do
                        if echo "$REPLY" | grep -q ">> 01:8f"; then
                                echo "Power On"
                                #Wakeup Server
                                if [ "$NETWORK_SERVER_WAKEUP" = "1" -a -n "$NETWORK_SERVER_MAC" ]; then
                                        wol $NETWORK_SERVER_MAC
                                fi
                                #Restart VDR
                                if [ -f /etc/init.d/vdr ]; then
                                        /etc/init.d/vdr restart
                                fi
                        elif echo "$REPLY" | grep -q ">> 0f:36"; then
                                echo "Power Off"
                                #Stop VDR
                                if [ -f /etc/init.d/vdr ]; then
                                        /etc/init.d/vdr stop
                                fi
                        elif echo "$REPLY" | grep -q ">> 01:44:00"; then
                                $SVDRPSEND HITK Ok
                        elif echo "$REPLY" | grep -q ">> 01:44:01"; then
                                $SVDRPSEND HITK Up
                        elif echo "$REPLY" | grep -q ">> 01:44:02"; then
                                $SVDRPSEND HITK Down
                        elif echo "$REPLY" | grep -q ">> 01:44:03"; then
                                $SVDRPSEND HITK Left
                        elif echo "$REPLY" | grep -q ">> 01:44:04"; then
                                $SVDRPSEND HITK Right
                        elif echo "$REPLY" | grep -q ">> 01:44:08"; then
                                $SVDRPSEND HITK Back
                        elif echo "$REPLY" | grep -q ">> 01:44:45"; then
                                $SVDRPSEND HITK Menu
                        elif echo "$REPLY" | grep -q ">> 01:44:44"; then
                                $SVDRPSEND HITK Play
                        elif echo "$REPLY" | grep -q ">> 01:44:46"; then
                                $SVDRPSEND HITK Pause
                        elif echo "$REPLY" | grep -q ">> 01:44:4c"; then
                                $SVDRPSEND HITK FastFwd
                        elif echo "$REPLY" | grep -q ">> 01:44:4b"; then
                                $SVDRPSEND HITK FastRew
                        fi
                done
        ;;

        stop)
                # Kill the right proceses
                log "Doing nothing"
        ;;

        restart)
                $0 stop
                $0 start
        ;;

        init)
                before=vdr
                start=1
        ;;

        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac

exit 0