#!/bin/bash
# $Id: action,v 1.2 2012/10/08 12:51:08 x10 Exp $
# Action script for x10iod
# Copyright 2002 - 2004 Perette Barella.

arg0="$(basename $0)"

action="$1"
wait=false
[ "$action" = "wait" ] && shift && wait=true && action="$1"

device="$2"
shift 2

X10CLI=/usr/local/bin/x10cli
[ -x "$X10CLI" ] || X10CLI="x10cli"

devnums=`grep -w $device ~/x10event | egrep '^[ 	]*(light|appliance)[ 	]' | awk '{print $2}'`
if [ "$devnums" = "" ]
then
	echo "$arg0: Device $device is unknown."
	exit 1
fi

if [ "$action" = "list" ]
then
	echo $devnums
	exit 0
elif [ "$action" = "whatis" ]
then
	grep -w $device ~/x10event | egrep '^[ 	]*(light|appliance)[ 	]'
	exit 0
fi

status=0
for dev in $devnums
do
	if [ "$action" = "fade" -a "$wait" = "false" ]
	then
		${X10CLI} $action $dev "$@" &
	else
		${X10CLI} $action $dev "$@" || status=$?
	fi
done
exit $status
