#!/usr/bin/perl
# mythtv_parsexml.pl
# cacti @ dont send me any spam @ e2r4.com
# 2007-11-20 Initial Release
# 2007-11-27 minor cleanup and added the port number as an optional argument
# 2008-02-06 BUGFIX: script would fail to see commercial flagging job if job
#	     queue count == 1
# 2008-02-24 added transcoding jobs thanks to a contributor

use LWP::Simple;
use XML::Simple;

if ( @ARGV == 0 ) {
    print "usage: mythtv_parsexml.pl <host> [port]\n";
    exit;
}

my $port = 6544;
$port = $ARGV[1] if ( $ARGV[1] );

$xml = get("http://$ARGV[0]:$port/xml");
die "Failed to get http://$ARGV[0]:$port/xml" unless defined $xml;
$ref = XMLin($xml);

$commflag = $encoders = $encoding = $commflag = $transcode = 0;

$count = $ref->{Encoders}->{count};
print "encoders:" . $count;

foreach $encoder ( keys( %{ $ref->{Encoders}->{Encoder} } ) ) {
    my $enc = $ref->{Encoders}->{Encoder}->{$encoder};
    $encoding++ if exists( $enc->{Program} );
}
print " encoding:" . $encoding;

$count = $ref->{JobQueue}->{count};
if ( $count > 1 ) {
    foreach $job ( keys( %{ $ref->{JobQueue}->{Job} } ) ) {
        $j = $ref->{JobQueue}->{Job}->{$job};
        $commflag++ if ( $j->{status} == 4 && $j->{type} == 2 );
        $transcode++ if ( $j->{status} == 4 && $j->{type} == 1 );
    }
}
else {
    $j = $ref->{JobQueue}->{Job};
    $commflag++ if ( $j->{status} == 4 && $j->{type} == 2 );
    $transcode++ if ( $j->{status} == 4 && $j->{type} == 1 );
}
print " commflag:" . $commflag;
print " transcode:" . $transcode;
print "\n";
