#!/bin/bash
# Maintained by CommandIR Support mini at commandir.com - Comments / Suggestions Welcome.
# Modified for LinHES specifics

echo $@ > /tmp/change_channel.log
. /etc/systemconfig
LOCKFILE=/tmp/lirclock
export PATH=/bin:/usr/bin:/usr/local/bin

function post_channel(){
    if [ -f $MYTHHOME/post_channel_change ]
    then
        echo "CALLING $MYTHHOME/post_channel $TRANSMITTER"
        $MYTHHOME/post_channel $TRANSMITTER
    fi

    }



function change_channel() {
    touch $LOCKFILE
    irsend SET_TRANSMITTERS $TRANSMITTER
    sleep .15
    case $cmd in
        [0-9]*)
            for digit in $(echo $cmd | sed -e 's/./& /g')
            do
                irsend SEND_ONCE $REMOTE_NAME $digit
                sleep $DELAY
            done
            if [ x$SENDKEY != "x" ]
            then
                irsend SEND_ONCE $REMOTE_NAME $SENDKEY
            fi

        ;;

        *)
            irsend SEND_ONCE $REMOTE_NAME $cmd
            ;;
    esac
}

function showhelp(){
        echo "IR Control Script for LIRC
        Includes support for CommandIR transmitter selection and lockfile.
        The remote codes will be specific to the transmitter number  and matched via /etc/systemconfig
        Usage:
        $0 -d delay -c channel -n transmitter_number -s Enter

        -d delay in tenths of a seconds -
        -c channel or cmd
        -n transmitter_number
        -s Key to send after executing change channel command (optional)

        Sends an IR command or sequence of numbers using via emitter TRANSMITTER_NUM.
        DELAY ( in 10th of seconds) is waited between sending IR commands.

        Example:  Change to channel 123 using emitter 2 with a 0.3s pause between digits:
        $0 -n1 -c123 -d3

        "
        exit 1


}

function testvars() {
    if [ x$DELAY = "x" ]
    then
        tdelay="HostTransmitDelay_$TRANSMITTER"
        d=${!tdelay}
        DELAY=$(echo "scale=2; $d / 10" | bc)
    fi


    if [ x$cmd = "x" ]
    then
        echo "-c is mandatory"
        exit 1
    fi


    if [ x$TRANSMITTER = "x" ]
    then
        echo "-n is mandatory"
        exit 1
    fi
      echo "delay is $DELAY"


#     echo "channel or button name is $cmd"
#     echo "Using transmitter $TRANSMITTER"
#     echo "Remote name is $REMOTE_NAME"
#     echo "Will send key $SENDKEY after numbers"
}


#-----------------------------Main Program-----------------------

while getopts ":d:c:n:hs:" opt; do
  case $opt in
    h) showhelp
      ;;
    d) DELAY=$(echo "scale=2; $OPTARG / 10" | bc)
      ;;

    c) cmd=$OPTARG
      ;;

    n) TRANSMITTER=$OPTARG
       tname="HostTransmitproto_$TRANSMITTER"
       REMOTE_NAME=${!tname}
       if [ x$REMOTE_NAME = "xnone" ]
       then
            echo "Please define Transmitters in /etc/systemconfig"
            exit 1
       fi
       if [ x$REMOTE_NAME = "x" ]
       then
        echo "Please define Transmitters in /etc/systemconfig"
        exit 1
       fi

      ;;

    s) SENDKEY=$OPTARG
      ;;

    \?)
      echo
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

testvars
while [ -f $LOCKFILE ]
do
   echo "Waiting for change channel lock file..."
   sleep .1
done

change_channel
post_channel
rm -f /tmp/lirclock