 
#!/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

# Configs

$temp = '/tmp/';                                # INCLUDE head and tail /
$unrar_path = '/usr/bin/unrar';                 # Path to Unrar
$mplayer_path = '/usr/bin/mplayer';             # Path to mplayer
$mplayer_args = '-fs -zoom - quiet -vo vx';     # 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);

#DEBUG
print "archive: " . $archive . "\n";

# 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);

# Once unrar'd build command to execute mplayer via system
$mplayer_cmd = $mplayer_path . " " . $mplayer_args . " " . $temp . $archive;

#DEBUG
print "mplayer_cmd: " . $mplayer_cmd . "\n";

# Watch it !
system($mplayer_cmd);

# Delete it !
$rm_cmd = $rm_path . " -f " . $temp . $archive;

#DEBUG
print "rm_cmd: " . $rm_cmd;
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;
}
