#!/usr/bin/perl
############################################################
# File -            channelchange.pl
# Author -          Guardian Bob
# Date -            Tuesday, August 07, 2007
# E-Mail -          Guardian.Bob@gmail.com
# Description -     Changes channels, working around the 
#                   broken 0
# Changelog -       August 07, 2007
#                   Initial Revision
#                   August 19, 2007
#                   Adjusted the sleep (0.2->0.3) as at
#                   0.2, two 5 commands were treated as one.
#                   Starting each channel change with a
#                   ch+ to try to bring the box out of
#                   a standby mode (sometimes the box 
#                   ignores the first command sent.)
############################################################
use strict;

use constant IRCMD => "/usr/bin/irsend SEND_ONCE DCT701 ";

# 0.2 is too short, will see 551 as 51
use constant SLEEPCMD => "/bin/sleep 0.3; ";

my $pid = fork();
if (not defined $pid) {
    print "resources not avilable.\n";
} elsif ($pid == 0) {

    my $chan = shift();

# clean up chan
    $chan =~ s/^([0-9]+).*/$1/;

    my $cmd = IRCMD;

    $chan =~ s/([0-9])/ $1/g;

    $cmd .= $chan . " ok/select;" . SLEEPCMD;

    my @commands = ();
    push(@commands, $cmd);
    push(@commands, SLEEPCMD . SLEEPCMD . SLEEPCMD);
    push(@commands, IRCMD . " exit;");
    push(@commands, SLEEPCMD );
    push(@commands, $cmd);
    push(@commands, SLEEPCMD . SLEEPCMD . SLEEPCMD);
    push(@commands, IRCMD . " exit;");
    push(@commands, SLEEPCMD );
    push(@commands, IRCMD . " exit;");

    my $send = "";
    foreach my $command (@commands) {
	$send .= $command;
    }
    print $send,"\n";
    exec $send;
    exit(0);
} 



