#!/usr/bin/perl -w
#
# Automatic unrar + mplayer play for use with mythtv
#
# bradleyf - 20070315
#
# ToDo:
#
# . Check to make sure we're only passed one variable
# . When executing unrar e function, should check return code
# Declarations
my $rard; # Variable to hold the (full?) path to rar file
my $unrard; # Variable to hold the full path to unrar file
my $archive; # Variable to hold the name of the media stored in $rard
my $temp; # Variable hold path to temp folder
my $unrar_path; # Location of unrar binary - tested with 2.6 Beta (i think)
my $unrar_cmd; # Complete command to be executed for the unrar system call
my $mplayer_path; # Path to mplayer
my $mplayer_args; # Arguments to proceed mplayer system call
my $mplayer_cmd; # Complete command to be execute for the myplaer system call
my $rm_path; # Path to 'rm' - used to delete watched media
my $rm_cmd; # Complete command to be execute for the rm $temp . $archive cmd
my $subtitle_file;
my $sub_param;
my $rar_method;
# Configs
$temp = '/tmp/'; # INCLUDE head and tail /
$unrar_path = '/usr/bin/unrar'; # Path to Unrar
$subdetect_path = '/home/zukex/bin/subtitledetect.sh'; # Path to subdetect
$mplayer_path = '/usr/bin/mplayer'; # Path to mplayer
$mplayer_args = '-fs -zoom -quiet'; # mplayer arguments
$rm_path = '/bin/rm';
# main sub
# Create logfile
create_logfile();
# mplayer passes file as a argument
# we tell mplayer to only pass this one.
$rard = $ARGV[0];
# Find out the actual filename and remove trailing \n
$archive = `$unrar_path lb $rard`;
chomp($archive);
$rar_method = 0;
if ($archive =~ /(xor|notv|lol|0tv|2hd|dot|caph|tpz|river|pdtv-dt|fqm|saints|wat|vtm|nedivx|dmd|^p-|mvs|sph|^fl-|flair|mdp|hls|txf|-profile|dimension)/i) {
$rar_method = 1;
}
if ($archive =~ /(fov|-med|med-)/i) {
$rar_method = 0;
}
#DEBUG
print "rar method: " . $rar_method . "\n";
$subtitle_file = `$subdetect_path $rard $archive`;
chomp($subtitle_file);
if (length ($subtitle_file) < 5) {
$sub_param = "";
} else {
if ($subtitle_file =~ /idx$/) {
$subtitle_file =~ s/.idx$//;
$sub_param = "-vobsub " . $subtitle_file;
} else {
$sub_param = "-sub " . $subtitle_file;
}
}
####
my $groups = `groups`;
chomp($groups);
print "groups: " . $groups . "\n";
#DEBUG
print "archive: " . $archive . "\n";
if ($rar_method == 0) {
# Build unrar complete command for system call
$unrar_cmd = $unrar_path . " e " . $rard . " " . $temp;
#DEBUG
print "unrar_cmd: " . $unrar_cmd . "\n";
# Use system to unrar to temp dir
system($unrar_cmd);
} else {
system("killall rarfs");
$temp = '/tmp/rarfs/';
# Build unrar complete command for system call
$unrar_cmd = "mount.fuse rarfs#" . $rard . " " . $temp;
#DEBUG
print "unrar_cmd: " . $unrar_cmd . "\n";
# Use system to mount to temp dir
system($unrar_cmd);
}
# Once unrar'd build command to execute mplayer via system
$mplayer_cmd = $mplayer_path . " " . $mplayer_args . " " . $sub_param . " " . $temp . $archive;
#DEBUG
print "mplayer_cmd: " . $mplayer_cmd . "\n";
# Watch it !
system($mplayer_cmd);
if ($rar_method == 0) {
# Delete it !
$rm_cmd = $rm_path . " -f " . $temp . $archive;
#DEBUG
print "rm_cmd: " . $rm_cmd;
system($rm_cmd);
} else {
# unmount it !
$rm_cmd = "fusermount -u " . $temp;
#DEBUG
print "rm_cmd: " . $rm_cmd . "\n";
system($rm_cmd);
}
#******************************************************************************
#
# Function : create_logfile
#
# Description : Redirect STDOUT & STDERR to named logfile
#
# Params : none
#
# Returns : none
#
#******************************************************************************
sub create_logfile
{
my (
$logfile, # final logfile name
$filename, # filename sans extension
$filext, # filename extension
$filedir # dir for logfile
);
# Create filename
$logfile = "/tmp/mplayer-rar.log";
# Redirect STDOUT & STDERR to named logfile
open( STDOUT, ">$logfile") or
die "\nUnable to open logfile $logfile: $!\n";
open( STDERR, ">>&STDOUT");
# Set autoflush on
select (STDERR); $| = 1;
select (STDOUT); $| = 1;
}