#!/usr/bin/perl -w
#
# Outputs information about the storage used by recordings in the Deleted and
# LiveTV recording groups and Watched (but not Deleted/LiveTV) recordings.
#
# Automatically detects database settings.
#
# Includes
use Getopt::Long;
use MythTV;
# Some variables we'll use here
our ($livetv_count, $deleted_count, $watched_count);
our ($livetv_space, $deleted_space, $watched_space);
our ($plain_text, $text_format, $usage);
our ($dheading, $dtext_format);
our ($status_text_format, $dstatus_text_format);
# Initialize counts/space used variables
$livetv_count = $deleted_count = $watched_count = 0;
$livetv_space = $deleted_space = $watched_space = 0;
# Default status output heading
$dheading='Available Storage Used by:\n';
# Default format of plain-text output
$dtext_format=' LiveTV : {LIVETV_SPACE} in {LIVETV_COUNT}'.
' recordings\n'.
' Deleted shows: {DELETED_SPACE} in {DELETED_COUNT}'.
' recordings\n'.
' Watched shows: {WATCHED_SPACE} in {WATCHED_COUNT}'.
' recordings\n';
# Default format of status output display text
$dstatus_text_format='
'.
'LiveTV | {LIVETV_SPACE} | '.
'{LIVETV_COUNT} recordings |
'.
'Deleted Shows | {DELETED_SPACE} | '.
'{DELETED_COUNT} recordings |
'.
'Watched Shows | {WATCHED_SPACE} | '.
'{WATCHED_COUNT} recordings |
'.
'
';
# Provide default values for GetOptions
$heading = $dheading;
$text_format = $dtext_format;
$status_text_format = $dstatus_text_format;
# Load the cli options
GetOptions('heading=s' => \$heading,
'plain_text' => \$plain_text,
'text_format=s' => \$text_format,
'status_text_format=s' => \$status_text_format,
'usage|help' => \$usage
);
# Print usage
if ($usage) {
print <backend_rows('QUERY_RECORDINGS Delete');
our $show;
my $filesize;
foreach my $row (@{$rows{'rows'}}) {
$show = new MythTV::Program(@$row);
$filesize = ($show->{'fs_high'} + ($show->{'fs_low'} < 0)) * 4294967296 + $show->{'fs_low'};
# Deleted recordings
if ($show->{'recgroup'} eq 'Deleted')
{
$deleted_count++;
$deleted_space += $filesize;
}
# LiveTV recordings
elsif ($show->{'recgroup'} eq 'LiveTV')
{
$livetv_count++;
$livetv_space += $filesize;
}
# Watched (but not Deleted/LiveTV) recordings
elsif ($show->{'is_watched'})
{
$watched_count++;
$watched_space += $filesize;
}
}
# Print the information in the desired format
if (defined($plain_text)) {
text_print();
}
else {
status_print();
}
# Format the storage space
sub format_storage {
my $value = shift;
my $count = 0;
# This should cover it (and then some)
my @units = ('bytes',
'kiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB'
);
my $temp_value;
while (($temp_value = ($value / 1024)) > 1)
{
$value = $temp_value;
$count++;
}
if ($value == 0) {
$temp_value = 0;
}
else
{
$temp_value = sprintf("%.2f", $value);
}
return "$temp_value $units[$count]";
}
# Format output
sub format_output {
my $text = shift;
my $value;
$value = format_storage($livetv_space);
$text =~ s/{LIVETV_SPACE}/$value/g;
$text =~ s/{LIVETV_COUNT}/$livetv_count/g;
$value = format_storage($deleted_space);
$text =~ s/{DELETED_SPACE}/$value/g;
$text =~ s/{DELETED_COUNT}/$deleted_count/g;
$value = format_storage($watched_space);
$text =~ s/{WATCHED_SPACE}/$value/g;
$text =~ s/{WATCHED_COUNT}/$watched_count/g;
$text =~ s/\\r/\r/g;
$text =~ s/\\n/\n/g;
return $text;
}
# Print the output for use in the backend status page.
sub status_print {
my $text = format_output($status_text_format);
print("$heading");
print("$text");
}
# Print the output in plain text format
sub text_print {
my $text = format_output($text_format);
print("$heading");
print("$text");
}