#!/bin/sh
# Maintained by CommandIR Support mini at commandir.com - Comments / Suggestions Welcome.
if [ $# -lt 3 ]
then
  echo "IR Control Script for LIRC V2.0
Includes support for CommandIR transmitter selection and lockfile.
  
Usage:  
$0 REMOTE_NAME TRANSMITTER_NUM (CHANNEL_NUMBER | IR_COMMAND) [DELAY]

Sends an IR command or sequence of numbers using REMOTE_NAME via emitter
TRANSMITTER_NUM.  DELAY (in seconds) is waited between sending IR commands.
 
Example:  Change ExpressVu to channel 123 using emitter 2 with a 0.3s pause between digits:
$0 expressvu 2 123 .3
"
  exit 1
fi

if [ $# -eq 4 ]
then
 DELAY=$4
else
 DELAY=.2
fi

LOCKFILE=/tmp/lirclock
export PATH=/bin:/usr/bin:/usr/local/bin
REMOTE_NAME=$1
TRANSMITTER=$2
cmd="$3"

while [ -f $LOCKFILE ] 
do
#echo "Waiting for lock..."
  sleep .1
done

touch $LOCKFILE

irsend SET_TRANSMITTERS $TRANSMITTER
sleep .15
case $cmd in
    [0-9]*)
    for digit in $(echo $3 | sed -e 's/./& /g'); do 
        irsend SEND_ONCE $REMOTE_NAME $digit
        sleep $DELAY
    done
    ;;

    *)
        irsend SEND_ONCE $REMOTE_NAME $cmd
        ;;
esac

rm $LOCKFILE

