#!/usr/bin/perl -w
###########################################
# myx10.cgi - CGI frontend for myx10
# Mike Schilli, 2007 (m@perlmeister.com)
###########################################
use strict;
use CGI qw(:all);
use Log::Log4perl qw(:easy);
use YAML qw(LoadFile);
use Template;

print header();

my $action = param("action");
my $device = param("device");

if(!defined $device) {
  my $devices = LoadFile("/etc/x10.conf");

  my $tpl = Template->new();
  $tpl->process("myx10.tmpl", { 
    devices => $devices, 
  } ) or die $tpl->error();
  exit 0;
}

if(!defined $action or
   $action !~ /^(on|off|status)$/) {
  print "Error: No/Invalid action\n";
  exit 0;
}

if(!defined $device or $device =~ /\W/) {
  print "Error: use a proper 'device'\n";
  exit 0;
}

system "sudo", "/usr/bin/myx10", 
       $device, param("action");
