#!/usr/bin/perl
#
# This script is aimed to help in jobfile-creation without usage of the 
# jobfile-editor. This script scans the recordings and creates a 
# jobfile which may be used as a template for manual editing.
# 
# PLEASE check the jobfile TWICE before using it with vdr2iso.
# 		this script is written by ...: geronimo a-t vdrportal d.o_t de
# 
# COPYRIGHT:	(c) 2005-2006 Creative Commons by-nc-sa
#					some rights reserved. For details on copyright see:
#					http://creativecommons.org/licenses/by-nc-sa
#
# DISCLAIMER:	NO WARANTY AT ALL. Use at your own risk.
#
# vi: ts=3
#
use strict;
use Data::Dumper;
use V2I::Config;
use V2I::Job;

our $debug = 0;
my  $appConfig;
my  $recsPerDisk = 4;
my  $firstRecording = 1;
my  $numberOfDisks = 99;
my  $fileName;
my  $recBase = '/video0';

sub setup {
	@ARGV = ['-?'] if not @ARGV;
	foreach (@ARGV){
		SWITCH: {  
			/^--?job=(.+)$/				and do { $fileName = $1;       last SWITCH; };
			/^--?first=(.+)$/				and do { $firstRecording = $1; last SWITCH; };
			/^--?recsPerDisk=(.+)$/		and do { $recsPerDisk = $1;    last SWITCH; };
			/^--?numberOfDisks=(.+)$/	and do { $numberOfDisks = $1;  last SWITCH; };
			/^--?recBase=(.+)$/			and do { $recBase = $1;  		 last SWITCH; };
   	   die <<USAGE_END;

   INVALID PARAMETER found!			

   createSimpleJob - is a script to create a jobfile for vdr2iso
from recordings by splitting the list of recordings into DVD-entries.
The script-behaviour may be changed by editing vdr2iso (in your system config 
directory) and by using commandline parameters.
These parameters are known:
   -job=<filename>       the filename for the creeated job. 
                         If not given, job will be printed to STDOUT.
   -first=#              number of the first recording (useful for consecutive runs)
   -recsPerDisk=#        number of recordings for each DVD
   -numberOfDisks=#      number of DVD's to create
   -recBase=<directory>  directory is root of all relevant recordings

USAGE_END
        	}
		}
	$appConfig = V2I::Config->new(-iniFN=>'');
	$appConfig->getConfig->{'recBase'} = $recBase;
	my $theme = $appConfig->getThemeManager->getTheme($appConfig->getTheme);
	my $missing = $theme->check;
	
	if(%$missing){
		while(my($key, $val) = each(%$missing)){
			print('invalid configuration for property <'.$key.'> - value ['.$val.'] NOT found!'."\n");	  
			}
		exit;
		}
}

#
# main ...
#
setup;
select((select(STDOUT), $| = 1)[0]);
print("\n\n".'current settings:'."\n");
print("\t".'job-filename .....: '.$fileName."\n");
print("\t".'firstRecording ...: '.$firstRecording."\n");
print("\t".'recs / disk ......: '.$recsPerDisk."\n");
print("\t".'max disks ........: '.$numberOfDisks."\n");
print("\t".'base of recordings: '.$recBase."\n");
my $response = '';

print("\n");
while(not $response =~ /^(?:y|n)$/i){
	print("\r".'ok to start processing? (Y/n)');
	$response = <STDIN>;
	$response = 'y' if $response =~ /^$/;
	}
if($response =~ /^y$/i){
	my $job = V2I::Job->createSimple($appConfig
						 					# the default ordering of the recordings is respect to 
											# timestamp of the recording.
											# If you like to change the order, just change the next
											# function (sort-helper-function).
											# see perl documentation on details.
						 					 , sub{ $V2I::Job::a->{'ts'} cmp $V2I::Job::b->{'ts'} }
											 , $recsPerDisk
											 , $firstRecording
											 , $numberOfDisks);
	if($fileName){
		$job->setFilename($fileName);
		$job->save;
		}
	else{
		print($job->toXml."\n");	  
		}
	}
