#!/usr/bin/perl
# mythimport v2.0
# By: John Baab
# Email: john.baab@gmail.com
# Purpose: Imports recordings into an On The Go backend.
# Requirements: 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 MythTV;
use File::Copy;
use strict;

my ($input, $output, $cleanup) = "";
my $usage = "\nHow to use mythimport : \n"
    ."\ninput = Input directory where exported recordings and sql live.  These are generated using MythExport.  This option is mandatory.\n"
    ."output = Output directory, use this if you want to transfer the recordings to a different directory.\n"
    ."cleanup = Specify what files to delete upon success (available options: all, recordings, sql).  Do not speficy to leave files in place.\n"
    ."\nExample: mythimport input=/mnt/usb/mythtv/ output=/var/lib/mythtv/recordings/ cleanup=all\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 <= 0) {
    die "$usage";
}

foreach (@ARGV){
    if ($_ =~ m/input/) {
        $input = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/output/) {
        $output = (split(/\=/,$_))[1];
    }
    elsif ($_ =~ m/cleanup/) {
        $cleanup = (split(/\=/,$_))[1];
    }
}

#remove trailing /
$input =~ s/\/$//;
$output =~ s/\/$//;

my $myth = new MythTV();

#get mysql info
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'};

chdir $input || die "ERROR: input directory is not accessable.\n";

#build mysql command
my $command = "mysql -h$dbhost -u$dbuser -p$dbpass -P$dbport $dbname < mythimport.sql"; 

#test that the directory has the correct permissions
if($output ne ""){	
    -w $output || die "ERROR: $output is not writeable.\n";
}

system ("$command") == 0 || die "ERROR: MySQL command failed.\n";

if($output ne ""){
    copy("*.mpg", "$output/.");
    copy("*.nuv","$output/.");
    print "Make sure that $output is in your MythTV storage groups, otherwise MythTV will not be able to find your files.";
}

if($cleanup eq "all"){
    unlink("*");
}
elsif($cleanup eq "recordings"){
    unlink("*.mpg");
    unlink("*.nuv"); 
}
elsif($cleanup eq "sql"){
    unlink("mythimport.sql");
}
