~ubuntu-branches/ubuntu/precise/mythexport/precise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/usr/bin/perl
# mythexport-daemon v2.1
# By: John Baab
# Email: rhpot1991@ubuntu.com
# Purpose: daemon for exporting mythtv recordings into formats used by portable devices.
# Requirements: perl and the DBI & DBD::mysql modules, MythTV perl bindings, AtomicParsley
#
# 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 strict;
use POSIX qw(setsid);
use DBI;
use DBD::mysql;
use Config::Simple;
use MythTV;
use Proc::Daemon;
use Proc::PID::File;
use Log::Dispatch;
use Log::Dispatch::File;
use Date::Format;
use File::Spec;
use File::Copy;
use XML::Writer;
use IO::File;

use lib '/usr/share/mythexport';
use lib '/usr/share/mythexport/configs';

&startDaemon;

my $debug = "";
my $level = "notice";

foreach (@ARGV){
    if ($_ =~ m/debug/) {
        $debug = 1;
    }
}

if($debug == 1){
    $level = "debug";
}

use constant LOG_DIR    => '/var/log/mythtv/';
use constant LOG_FILE   => 'mythexport.log';

our $HOSTNAME = `hostname`;
chomp $HOSTNAME;
my $log = new Log::Dispatch(
      callbacks => sub { my %h=@_; return Date::Format::time2str('%B %e %T', time)." ".$HOSTNAME." $0\[$$]: ".$h{message}."\n"; }
);
$log->add( Log::Dispatch::File->new( name      => 'file1',
                                     min_level => $level,
                                     mode      => 'append',
                                     filename  => File::Spec->catfile(LOG_DIR, LOG_FILE),
                                   )
);
$log->warning("Starting Processing:  ".time());
open STDERR, '>>', File::Spec->catfile(LOG_DIR, LOG_FILE);
my $connect = undef;
my $myth = undef;

my $tries = 5;
while ($connect == undef && --$tries > 0) {
    eval {
        $myth = new MythTV();
        # connect to database
        $connect = $myth->{'dbh'};
        1;
    } or do {
        logerror("Can't connect to MythTV: $@");
        logdebug("Sleeping 5 seconds...");
        sleep(5);
    };
}
if ($connect == undef) {
    die "Couldn't connect to MythTV.";
}


my $keep_going = 1;
$SIG{HUP}  = sub { $log->warning("Caught SIGHUP:  exiting gracefully"); $keep_going = 0; };
$SIG{INT}  = sub { $log->warning("Caught SIGINT:  exiting gracefully"); $keep_going = 0; };
$SIG{QUIT} = sub { $log->warning("Caught SIGQUIT:  exiting gracefully"); $keep_going = 0; };
$SIG{TERM} = sub { $log->warning("Caught SIGTERM:  exiting gracefully"); $keep_going = 0; };

sub startDaemon{
    Proc::Daemon::Init;
    dienice("Already running!") if Proc::PID::File->running(dir => "/var/run/mythtv",name => "mythexport");
}

sub dienice($) {
    my ($package, $filename, $line) = caller;
    $log->critical("$_[0] at line $line in $filename");
    die $_[0];
}

sub logerror($) {
    my ($package, $filename, $line) = caller;
    $log->critical("$_[0] at line $line in $filename");
}

sub logdebug($){
    my ($package, $filename, $line) = caller;
    $log->debug("$_[0] at line $line in $filename");
}

sub createXML($){
    my @params = split('&',$_[0]);
    my ($exportdir, @starttime, @chanid, $config, $sql_where) = "";

    foreach (@params){
        if ($_ =~ m/exportdir/) {
            $exportdir = (split(/\=/,$_))[1];
        }
        elsif ($_ =~ m/starttime/) {
            @starttime = split('\|',(split(/\=/,$_))[1]);
        }
        elsif ($_ =~ m/chanid/) {
            @chanid = split('\|',(split(/\=/,$_))[1]);
        }
        elsif ($_ =~ m/config/) {
            $config = (split(/\=/,$_))[1];
        }
    }
    
    # debugging logging
    logdebug("exportdir = $exportdir");
    logdebug("starttime = @starttime");
    logdebug("chanid = @chanid");
    logdebug("config = $config");
    
    $exportdir =~ s/\/$//;
    
    my $export_cfg = new Config::Simple();
    $export_cfg->read('/etc/mythtv/mythexport/mythexport_settings.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport_settings.cfg");

    require "$config.pm";

    my $object = new $config();
    my $extension = $object->Extension;
    
    #my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
    #my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';
    
    #test that the directory has the correct permissions
    -w $exportdir || logerror("ERROR: Directory $exportdir is not writeable.\n");
    my $output = new IO::File(">$exportdir/mythimport.xml");
    
    # create sql where clause
    foreach my $i (0..scalar(@chanid)-1)
    {
        $sql_where .= "(rec.chanid=\'$chanid[$i]\' and rec.starttime=\'$starttime[$i]\') or ";
    }
    $sql_where =~ s/\sor\s$//;
    
    print $output "<?xml-stylesheet type=\"text/xsl\" href=\"mythimport.xslt\"?>";
    my $writer = new XML::Writer(OUTPUT => $output);
    $writer->startTag("channel");
    
    my $query = "SELECT rec.title, rec.subtitle, rec.description, pg.syndicatedepisodenumber, pg.showtype, rec.programid, rec.basename, rec.chanid, rec.starttime
        FROM recorded rec LEFT JOIN program pg ON pg.starttime = rec.starttime AND pg.chanid = rec.chanid WHERE $sql_where";
    my $query_handle = $connect->prepare($query);
    logdebug("query = $query");
    $query_handle->execute() || logerror("Unable to query mythexport table");

    #if ($extension eq ""){
    #    if ($exportdevice eq "mp3"){
     #       $extension .= ".mp3";
      #  }
       # elsif ($exportdevice eq "archos"){
        #    $extension .= ".avi";
        #}
        #else{
        #    $extension .= ".mp4";
        #}
    #}
    
    $log->notice("Creating xml file");
    while ( my ($title,$subtitle,$description,$syndicatedepisodenumber,$showtype,$programid,$basename,$currentchanid,$currentstarttime) = $query_handle->fetchrow_array() ) {
    
        # FIND OUT THE CHANNEL NAME
        my $subquery = "SELECT callsign FROM channel WHERE chanid=?";
        my $subquery_handle = $connect->prepare($subquery);
        $subquery_handle->execute($currentchanid) || logerror("ERROR: Unable to query settings table");

        my $channame = $subquery_handle->fetchrow_array;

        # replace non-word characters in channame with dashes
        $channame =~ s/\W+/-/g;

        # replace non-word characters in title with underscores
        $title =~ s/\W+/_/g;
        # replace non-word characters in subtitle with underscores
        $subtitle =~ s/\W+/_/g;

        # Remove non alphanumeric chars from $starttime & $endtime
        $currentstarttime =~ s/[|^\W|\s|-|]//g;
        my $newfilename = $channame."-".$title."-".$subtitle."-".$currentstarttime;
        $newfilename =~ s/\..*?$//;
        
        $writer->startTag("item");
        
        $writer->startTag("title");
        $writer->characters("$title - $subtitle");
        $writer->endTag("title");
        
        $writer->startTag("description");
        $writer->characters("$description");
        $writer->endTag("description");
        
        $writer->startTag("link");

        $writer->characters("$newfilename.$extension");
        $writer->endTag("link");
        
        $writer->startTag("guid");
        $writer->characters("$newfilename");
        $writer->endTag("guid");
        
        $writer->endTag("item");

        $log->notice("Creating mysql dump");
        export("starttime=$currentstarttime&chanid=currentchanid&config=$config&otg=true");
    }
    $writer->endTag("channel");
    $writer->end();
    $output->close();
    
    copy("/usr/share/mythtv/mythimport.xslt","$exportdir/mythimport.xslt") || logerror("ERROR: Cannot copy /usr/share/mythtv/mythimport.xslt to $exportdir/mythimport.xslt");
}

sub createSQL($){
    my @params = split('&',$_[0]);
    my ($exportdir, @starttime, @chanid, $config, $sql_where) = "";
    
    foreach (@params){
        if ($_ =~ m/exportdir/) {
            $exportdir = (split(/\=/,$_))[1];
        }
        elsif ($_ =~ m/starttime/) {
            @starttime = split('\|',(split(/\=/,$_))[1]);
        }
        elsif ($_ =~ m/chanid/) {
            @chanid = split('\|',(split(/\=/,$_))[1]);
        }
        elsif ($_ =~ m/config/) {
            $config = (split(/\=/,$_))[1];
        }
    }
    
    # debugging logging
    logdebug("exportdir = $exportdir");
    logdebug("starttime = @starttime");
    logdebug("chanid = @chanid");
    logdebug("config = $config");
    
    $exportdir =~ s/\/$//;
    
    my $dbuser = $myth->{'db_user'};
    my $dbpass = $myth->{'db_pass'};
    my $dbhost = $myth->{'db_host'};
    my $dbname = $myth->{'db_name'};
    my $dbport = $myth->{'db_port'}; 
    
    # debugging logging
    logdebug("dbuser = $dbuser");
    logdebug("dbpass = $dbpass");
    logdebug("dbhost = $dbhost");
    logdebug("dbname = $dbname");
    logdebug("dbport = $dbport");

    #test that the directory has the correct permissions
    -w $exportdir || logerror("ERROR: Directory $exportdir is not writeable.\n");
    
    # create sql where clause
    foreach my $i (0..scalar(@chanid)-1)
    {
        $sql_where .= "(chanid=\'$chanid[$i]\' and starttime=\'$starttime[$i]\') or ";
    }
    
    $sql_where =~ s/\sor\s$//;

    my $command = "mysqldump -h$dbhost -u$dbuser -p$dbpass -P$dbport $dbname recorded recordedseek recordedrating recordedprogram recordedmarkup recordedcredits --where=\"$sql_where\" --no-create-db --no-create-info > $exportdir/mythimport.sql 2>&1";
    
    $log->notice("Creating mysql dump");
    logdebug("mysqldump command = $command");
    system($command) == 0 || logerror("ERROR: $command failed.");
    
    my $query = "SELECT basename FROM recorded where $sql_where";
    my $query_handle = $connect->prepare($query);
    logdebug("query = $query");
    $query_handle->execute()  || logerror("Unable to query mythexport table");

    my $schemaVer = $myth->backend_setting('DBSchemaVer');

    #copy files
    while ( my $basename = $query_handle->fetchrow_array() ) {
        my $dir = 'UNKNOWN';
        if ($schemaVer < 1171)
        {
            $dir = $myth->backend_setting('RecordFilePrefix');
        }
        else
        {
            my $storagegroup = new MythTV::StorageGroup();
            $dir = $storagegroup->FindRecordingDir($basename);
        }
        $log->notice("Copying $dir/$basename to $exportdir/$basename");
        copy("$dir/$basename","$exportdir/$basename") || logerror("Unable to copy $dir/$basename to $exportdir/$basename");
    }
}

sub export($){
    my @params = split("&",$_[0]);
    my ($debug,$starttime,$chanid,$config,$deleteperiod,$otg,$podcastname) = "";
    
    foreach (@params){
        if ($_ =~ 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/otg/) {
            $otg = (split(/\=/,$_))[1];
        }
        elsif ($_ =~ m/podcastname/) {
            $podcastname = (split(/\=/,$_))[1];
        }
    }

    #my $export_cfg = new Config::Simple();
    #$export_cfg->read('/etc/mythtv/mythexport/mythexport_settings.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport_settings.cfg");
    #my $delete_period = 30;

    #my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
    #my $exportcodec = $export_cfg->param("$block.codec") unless ref $export_cfg->param("$block.codec") eq 'ARRAY';
    #$delete_period = $export_cfg->param("$block.deletePeriod") unless ref $export_cfg->param("$block.deletePeriod") eq 'ARRAY';
    #my $podcast_name = $export_cfg->param("$block.podcastName") unless ref $export_cfg->param("$block.podcastName") eq 'ARRAY';
    #my $args = $export_cfg->param("$block.ffmpegArgs") unless ref $export_cfg->param("$block.ffmpegArgs") eq 'ARRAY';
    #my $removeCommercials = $export_cfg->param("$block.removeCommercials") unless ref $export_cfg->param("$block.removeCommercials") eq 'ARRAY';
    #my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';

    #my @args = split('\|',$args);
    
    my ($title, $subtitle, $description, $syndicatedepisodenumber, $showtype, $programid, $basename, $airdate) = "";
    
    # 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/\/$//;

    # debugging logging
    logdebug("exportdir = $exportdir");
    logdebug("starttime = $starttime");
    logdebug("chanid = $chanid");
    logdebug("config = $config");

    #test that the directory has the correct permissions
    -w $exportdir || logerror("ERROR: Directory $exportdir is not writeable.\n");

    my $query = "SELECT rec.title, rec.subtitle, rec.description, pg.syndicatedepisodenumber, pg.showtype, rec.programid, rec.basename, rec.starttime
    FROM recorded rec LEFT JOIN program pg ON pg.starttime = rec.starttime AND pg.chanid = rec.chanid
    WHERE rec.chanid=? AND rec.starttime=?";
    #print $query;
    my $query_handle = $connect->prepare($query);
    logdebug("query = $query");
    $query_handle->execute($chanid,$starttime) || logerror("ERROR: Cannot connect to database \n");

    $query_handle->bind_columns(undef, \$title, \$subtitle, \$description, \$syndicatedepisodenumber, \$showtype, \$programid, \$basename, \$airdate);
    $query_handle->fetch();

    my $schemaVer = $myth->backend_setting('DBSchemaVer');
    # Storage Groups were added in DBSchemaVer 1171
    # FIND WHERE THE RECORDINGS LIVE
    my $dir = 'UNKNOWN';
    if ($schemaVer < 1171)
    {
        logdebug("Using compatibility mode");
        $dir = $myth->backend_setting('RecordFilePrefix');
    }
    else
    {
        logdebug("Going into new mode\n");
        my $storagegroup = new MythTV::StorageGroup();
        $dir = $storagegroup->FindRecordingDir($basename);
    }

    # FIND OUT THE CHANNEL NAME
    $query = "SELECT callsign FROM channel WHERE chanid=?";
    $query_handle = $connect->prepare($query);
    logdebug("query = $query");
    $query_handle->execute($chanid) || logerror("ERROR: Unable to query channel table");

    my $channame = $query_handle->fetchrow_array;

    # replace non-word characters in channame with dashes
    $channame =~ s/\W+/-/g;

    # replace non-word characters in title with underscores
    my $title_old = $title;
    $title =~ s/\W+/_/g;
    # replace non-word characters in subtitle with underscores
    my $subtitle_old = $subtitle;
    $subtitle =~ s/\W+/_/g;

    # Remove non alphanumeric chars from $starttime & $endtime
    my $newstarttime = $starttime;
    $newstarttime =~ s/[|^\W|\s|-|]//g;
    my $filename = $dir."/".$basename;
    my $newfilename = $exportdir."/".$channame."-".$title."-".$subtitle."-".$newstarttime;
    $newfilename =~ s/\..*?$//;

    $syndicatedepisodenumber =~ m/^.*?(\d*)(\d{2})$/;
    my $seasonnumber = $1;
    my $episodenumber = $2;

    # Trim any characters over 63 (59+4 for the extentsion), 
    # it seems iTunes does not like files with lengths this long.
    $newfilename =~ s/^(.*\/(.{1,59})).*$/$1/g;
    my $webfilename = "$2";

    if ($debug) {
        print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n";
    }

    # move to the export directory incase any logs get written
    chdir $exportdir;

    #my $symlinkCmd = "";
    #my $APcommand = "";

    # move this to the new modules???
    #my $transcodeTemp;
    #if ($removeCommercials){
        # First generate the cutlist from the flagged commercials
    #    logdebug("command = nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1");
    #    system("nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1") == 0 || logerror("ERROR: error generating cutlist for commercial removal");

        # Now transcode using that cutlist
    #    logdebug("command = nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1");
    #    system("nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1") == 0 || logerror("ERROR: error removing commercials");

    #    $transcodeTemp = "$filename.tmp";
    #    $filename = $transcodeTemp;
    #}

    #if ($exportdevice eq "symlink"){
    #    $symlinkCmd = "ln -s $filename $newfilename";
    #}
    #elsif ($extension ne ""){
    #    $newfilename .= $extension;
    #    $webfilename .= $extension;
    #}
    #elsif ($exportdevice eq "mp3"){
    #    $newfilename .= ".mp3";
    #    $webfilename .= ".mp3";
    #}
    #elsif ($exportdevice eq "archos"){
    #    $newfilename .= ".avi";
    #    $webfilename .= ".avi";
    #}
    #else{
    #    $newfilename .= ".mp4";
    #    $webfilename .= ".mp4";
    #}
    
    #foreach (@args){
    #    if($_ =~ m/\/dev\/null/){
    #        $_ = "nice -n19 ffmpeg -i $filename $_";
    #    }
    #    else{
    #        $_ = "nice -n19 ffmpeg -i $filename $_ '$newfilename' 2>&1";
    #    }
    #}

    # Run the commands
    #foreach (@args){
    #    $log->notice("Exporting $newfilename");
    #    logdebug("command = $_");
    #    system($_) == 0|| logerror("ERROR: $_ FAILED/");
    #}

    #if ($removeCommercials){
    #    unlink($transcodeTemp) || logerror("ERROR: cannot remove $transcodeTemp.");
    #}

    require "$config.pm";

    my $object = new $config($filename, $newfilename);
    my $extension = $object->Extension();
    $object->export();

    my $returnCode = $object->checkOutput();

    # only continue if encoding didn't fail
    if ($returnCode == 1){

        if (!$debug){
            # clean up any log files left behind by ffmpeg
            system "rm *.log";
        }

        # Update mp4 info using AtomicParsley
        # need to fix this so movies actually show up as movies.
        #if ($newfilename eq ".mp4" && $webfilename eq ".mp4"){
        #    $APcommand = "AtomicParsley '$newfilename' --genre \"TV Shows\" --stik \"TV Show\" --TVNetwork $channame --TVShowName \"$title_old\" --TVEpisode \"$programid\" --TVEpisodeNum $episodenumber --TVSeason $seasonnumber --description \"$description\" --title \"$subtitle_old\" 2>&1";
        #}

        #if($APcommand){
        #   logdebug("command = $APcommand");
        #   system($APcommand) == 0 || logerror("ERROR: $APcommand FAILED");  
        #   system "rm $newfilename";
        #   system "mv $exportdir/*temp* $newfilename";
        #}
        #if($symlinkCmd){
        #    system($symlinkCmd) == 0 || logerror("ERROR: $symlinkCmd FAILED");    
        #}
        #(-e $newfilename) || logerror("ERROR: $newfilename is not available");    
        
        if($otg ne "true"){
            # Save Data
            $query = "INSERT into mythexport VALUES(NULL,?,?,?,?,NOW(),DATE_ADD(NOW(),INTERVAL ? DAY),?,?)";
            $query_handle = $connect->prepare($query);
            logdebug("query = $query");
            $query_handle->execute("$webfilename$extension",$title_old,$subtitle,$description,$deleteperiod,$airdate,$podcastname) || logerror("ERROR: Unable to update table.");
        }
    }
    else{
        logerror("ERROR: No resulting file from ffmpeg, most likely your ffmpeg failed.  Enable debugging and test by hand.");
    }
}

sub delete($){
    my $param = $_[0];

    my $cfg = new Config::Simple();
    $cfg->read('/etc/mythtv/mythexport/mythexport.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport.cfg");

    my $dir = $cfg->param("dir");
    $dir =~ s/\/$//;
    
    my $delete_query = "SELECT file FROM mythexport where id=?";
    my $delete_query_handle = $connect->prepare($delete_query);
    logdebug("query = $delete_query");
    $delete_query_handle->execute($param) || logerror("ERROR: Unable to query mythexport table");

    my $file = $delete_query_handle->fetchrow_array();
    my $location = "$dir\/$file";
    
    $log->notice("Deleting $file");
    logdebug("file = $location");
    unlink($location) || logerror("ERROR: Unable to find file: $location");

    my $query = "delete from mythexport where id=?";
    my $query_handle = $connect->prepare($query);
    logdebug("query = $query");
    $query_handle->execute($param) || logerror("Unable to delete from mythexport table");
}


# our infinite loop
while($keep_going) {
        my $query = "select id, type, param from mythexport_job_queue order by id";
        my $query_handle = $connect->prepare($query);
        logdebug("query = $query");
        $query_handle->execute() || logerror("Unable to query mythexport_job_queue table");
        
        while(my ($id, $type, $param) = $query_handle->fetchrow_array()){
            if($type eq "delete"){
                &delete($param);
            }
            elsif($type eq "export"){
                &export($param);
            }
            elsif($type eq "otg-lightweight"){
                createXML($param);
            }
            elsif($type eq "otg-full"){
                createSQL($param);
            }
            else{
                logerror("ERROR: job type unknown.");
            }
            
            my $job_query = "delete from mythexport_job_queue where id=?";
            my $job_query_handle = $connect->prepare($job_query);
            logdebug("query = $job_query");
            $job_query_handle->execute($id) || logerror("Unable to delete from mythexport_job_queue table");
        }
   
    # wait for 60 seconds
    sleep(60);
}

$log->warning("Stopping Processing:  ".time());