#!/usr/bin/perl
use POSIX qw(ceil floor);
use URI::Escape;
$IP = $ARGV[0];
$MAK = $ARGV[1];
$anchor = 0;

$shellScript = "curl -s 'https://" . $IP .  ":443/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes&AnchorOffset=" . $anchor . "' -k --digest -u tivo:" . $MAK;

$file =  `$shellScript`;

$file =~ m/<TotalItems>(.*?)<\/TotalItems>/g;
$TotalItems = $1;

$file =~ s/&amp;/&/g;
$file =~ s/&gt;/>/g;
$file =~ s/&lt;/</g;
$file =~ s/&quot;/"/g;
$file =~ s/&apos;/'/g;
$file =~ s/\\n/-/g;

$space_expires_soon = $space_expired = $space_save_until = $space_copyrighted = $space_in_progress = $space_suggestion = $space_regular = 0;
$total_shows = 0;
$final_result = "";

while ($anchor < $TotalItems) {
    @shows = split(/<Item>/, $file);
    $count = 0;
    foreach (@shows) {
	$count += 1;
	$total_shows += 1;
	$current_size = 0;
	$show_type = "regular";
	$output = "";
	$show = $_;
	$show =~ m/<Url>http.*id=(.*)<\/Url>/;
	if ($1 > 0) {
	    $id = $1;
	    if ($show =~ m/<Title>(.*?)<\/Title>/) {
		$showtitle = $1;
	    }
	    else {
		$showtitle="";
		}
		$output = $output . "$showtitle\n";
		$urlencodedtitle =uri_escape($showtitle);
		$output = $output . "$urlencodedtitle\n";
	    
		if ($show =~ m/<EpisodeTitle>(.*?)<\/EpisodeTitle>/) {
		$output = $output . "$1\n";
	    }
	    else {
		$output = $output . "\n";
	    }
	    if ($show =~ m/<CaptureDate>0x(.*?)<\/CaptureDate>/) {
		$timestamp = hex($1);
		($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($timestamp+15);
		$year = $year + 1900;
		$mon = $mon + 1;
		$output = $output . sprintf ("%04d-%02d-%02d %02d:%02d\n", $year, $mon, $mday, $hour, $min);
	    }
	    else {
		$output = $output . "\n";
	    }
	    if ($show =~ m/<Duration>(.*?)<\/Duration>/) {
		$duration = $1 + 3000;
		$duration = floor($duration / 60000);
		$hours = floor($duration / 60);
		$min = $duration - ($hours * 60);
		if ($hours == 0) {$hours = "0";}
		if ($min == 0) {$min = "00";}
		elsif ($min < 10) {$min = "0" . $min;}
		$output = $output . "$hours:$min\n";
	    }
	    else {
		$output = $output . "\n";
	    }
	    if ($show =~ m/<SourceSize>(.*?)<\/SourceSize>/) {
		$current_size = $1 / 1024 / 1024;
		$output = $output . "$current_size\n";
	    }
	    else {
		$output = $output . "\n";
	    }
	    if ($show =~ m/<ProgramId>(.*?)<\/ProgramId>/) {
		$output = $output . "$1\n";
	    }
	    else {
		$output = $output . "\n";
	    }

		if ($show =~ m/<SeriesId>(.*?)<\/SeriesId>/) {
		$output = $output . "$1\n";
	    }
	    else {
		$output = $output . "\n";
	    }

		if ($show =~ m/<SourceStation>(.*?)<\/SourceStation>/) {
		$output = $output . "$1\n";
	    }
	    else {
		$output = $output . "\n";
	    }

		if ($show =~ m/<HighDefinition>Y.*<\/HighDefinition>/) {
		$output = $output . "HD\n";
	    }
	    else {
		$output = $output . "SD\n";
	    }

	    $output = $output . "$id\n";

	    $flags = 0;
	    if ($show =~ m/<Url>urn:tivo:image:suggestion-recording<\/Url>/) {
			$output = $output . "Y\n";
		    }
		    else {
			$output = $output . "N\n";
		    }
		if ($show =~ m/<Url>urn:tivo:image:expired-recording<\/Url>/) {
				$output = $output . "Y\n";
			    }
			    else {
				$output = $output . "N\n";
			    }
		if ($show =~ m/<Url>urn:tivo:image:expires-soon-recording<\/Url>/) {
				$output = $output . "Y\n";
			    }
			    else {
				$output = $output . "N\n";
			    }
		if ($show =~ m/<Url>urn:tivo:image:save-until-i-delete-recording<\/Url>/) {
				$output = $output . "Y\n";
			    }
			    else {
				$output = $output . "N\n";
			    }
		if ($show =~ m/<CopyProtected>Yes<\/CopyProtected>/) {
				$output = $output . "Y\n";
			    }
			    else {
				$output = $output . "N\n";
			    }
		if ($show =~ m/<InProgress>Yes<\/InProgress>/) {
				$output = $output . "Y\n";
			    }
			    else {
				$output = $output . "N\n";
			    }
				$final_result = $final_result . $output;
		
	   
	}
    }
    $anchor += $count;
    if ($anchor < $TotalItems) {
	$shellScript = "curl -s 'https://" . $IP . ":443/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse=Yes&AnchorOffset=" . $anchor . "' -k --digest -u tivo:" . $MAK;
	$file =  `$shellScript`;
	$count = 0;
    }
}

    print "$final_result";
