#!/usr/bin/perl
# mythexport_addjob v2.2.3
# By: John Baab
# Email: rhpot1991@ubuntu.com
# Purpose: Script for creating a mythexport job.
# Requirements: perl and the DBI & DBD::mysql modules, MythTV perl bindings
#
# License:
#
# This Package is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# On Debian & Ubuntu systems, a complete copy of the GPL can be found under
# /usr/share/common-licenses/GPL-3, or (at your option) any later version

use DBI;
use DBD::mysql;
use MythTV;
use Config::Simple;
use strict;

my $connect = undef;
my $debug = 0;

# Set default values
my $cfg = new Config::Simple();
$cfg->read('/etc/mythtv/mythexport/mythexport.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport.cfg");
my $exportdir = $cfg->param("dir");
$exportdir =~ s/\/$//;

my ($starttime, $chanid, $config, $deleteperiod, $podcastname) = "";

my $usage = "\nHow to use mythexport : \n"
    ."\nchanid = Channel ID associated with the recording to export.\n"
    ."starttime = Recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format.\n"
    ."config = Name of configuration settings.\n"
    ."\nExample: mythexport_add starttime=20060803205900 chanid=1006 config=ipod\n";

# get this script's ARGS
my $num = $#ARGV + 1;

# if user hasn't passed enough arguments, die and print the usage info
if ($num <= 2) {
    die "$usage";
}

# Get all the arguments
foreach (@ARGV){
    if ($_ =~ m/debug/) {
        $debug = 1;
    }
    elsif ($_ =~ m/starttime/) {
        $starttime = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/chanid/) {
        $chanid = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/config/) {
        $config = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/deleteperiod/) {
        $deleteperiod = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/podcastname/) {
        $podcastname = (split(/\=/,$_))[1];
    }
}

my $myth = new MythTV();
# connect to database
$connect = $myth->{'dbh'};

my $query = "SELECT title, subtitle from recorded where chanid=? and starttime=?";
my $query_handle = $connect->prepare($query);
$query_handle->execute($chanid,$starttime) || die("Unable to query recordings table");

my ($title, $subtitle) = $query_handle->fetchrow_array;

my $param = "starttime=$starttime&chanid=$chanid&config=$config&deleteperiod=$deleteperiod&podcastname=$podcastname"; 

# add export job
$query = "insert into mythexport_job_queue(type,param,id,description) values ('export',?,NULL,?)";
$query_handle = $connect->prepare($query);
$query_handle->execute($param,"$title:$subtitle") || die "Unable to query mythexport_job_queue table";

my $id = $query_handle->{mysql_insertid};

my $lockfile = "$exportdir/mythexport.$id";

# create a lock file and wait for it to be deleted by the daemon before we complete the user job
system("touch $lockfile");
while (-e $lockfile) {sleep(60);}
