#!/bin/bash

if [ $# == 0 ]; then
  echo "Usage: $0 <device> <command> <device> <parameter>"
  echo "   command ::= on off set dim bright get status"
  echo "   device ::= e1 e2 bedroom_light etc.."
  exit -1
fi

MINBIN=/usr/local/minerva/bin
LOGSTUB=/var/log/minerva/x10control
HEYU=/usr/local/bin/heyu

# We include device to be consistent with the other bearskin commands,
# but in reality it is rarely used. This is because X10 commands do not 
# need to be sent across the network to be executed remotely.

# Generally, finddev will query the Marple devlist and find a device
# labelled '/'; this is treated as a device, and returned into $DEVICE.
# Since this is unused in the script it will cause no damage.

DEVICE=`$MINBIN/finddev x10 $*`
if [ $? == 0 ]; then
  echo $DEVICE
  exit 0;
fi

CMD=$2
X10DEVICE=$3
X10PARAM=$4

case "$CMD" in
  init)
    touch $LOGSTUB.err
    chgrp minerva $LOGSTUB.err
    chmod ug+rw   $LOGSTUB.err
    $HEYU engine >/dev/null 2>&1
    ;;

  on)
    $HEYU turn $X10DEVICE on  2>>$LOGSTUB.err
    ;;

  off)
    $HEYU turn $X10DEVICE off  2>>$LOGSTUB.err
    ;;

  allon)
    # X10DEVICE is a house code in this instance
    $HEYU lightson $X10DEVICE 2>>$LOGSTUB.err
    ;;

  alloff)
    # X10DEVICE is a house code in this instance
    $HEYU lightsoff $X10DEVICE 2>>$LOGSTUB.err
    ;;

  set)
    $HEYU dimb $X10DEVICE $X10PARAM 2>>$LOGSTUB.err
    ;;

  dim)
    $HEYU dim $X10DEVICE $X10PARAM 2>>$LOGSTUB.err
    ;;

  bright)
    $HEYU bright $X10DEVICE $X10PARAM 2>>$LOGSTUB.err
    ;;

  ison)
    $HEYU onstate $X10DEVICE  2>>$LOGSTUB.err
    ;;

  get)
    $HEYU dimlevel $X10DEVICE 2>>$LOGSTUB.err
    ;;

  status)
    $HEYU info 2>>$LOGSTUB.err
    ;;

  *)
    ;;
esac

