#!/usr/bin/perl -w

use strict;

# Basic usage message if there are not enough parameters.
die "Usage: x10 <on|off> <Device #>.\n" unless scalar(@ARGV) == 2;

# Untaint and check the action.
$ARGV[0] =~ /^(on|off)/i;
my $action = $1;
die "Invalid action $action." unless lc($action) eq 'on' ||
                                     lc($action) eq 'off';

# Change the action into the proper parameter for the X10 controller.
$action = (lc($action) eq 'on' ? '-n' : '-f');

# Untaint and check the device #.
$ARGV[1] =~ /^(\d+)$/;
my $device = $1;
die "Invalid device # $device" unless $device =~ /^\d\d?$/ &&
                                      $device >= 0 &&
                                      $device <= 15;

# Clear the PATH and IFS environment variables for saftey.
$ENV{'PATH'} = '';
$ENV{'IFS'} = '';

# Run the X10 controller.
system('/usr/bin/br','-c','n',$action,$device)
