#! /usr/bin/perl
#
# 
my $defaultdir="./";
my $default_dvgrabOptions = '--format dv2 --buffers 250  --opendml --size 0 ';
my $dvgrabCommandLine = 
'"(dvgrab ${default_dvgrabOptions} --card ${CARD} --duration ${RECTIME} ${STORE}capture-${DATE}-C${CARD}- \&)"';

# 
if(grep(/^(\-h|\-\-help)$/, @ARGV))
{
    print STDOUT << "ENDOFHELPTEXT";
Grab two multiple videostreams on IEEE1394 cards

use:
multidvgrab.pl [dvgrab options] -c<n>=PATH_CARD_<n> --card<n>[=PATH_CARD_<n>] recordings-time

recordings-time: time of recording, in dvgrab format
--card<n>=PATH_CARD_<n>: Use card <n> with the directory where the recording of camera <n> is stored
                         Default is $defaultdir
--c<n>=PATH_CARD_<n>:    Synonym of --card<n>=PATH_CARD_<n>
[dvgrab options]: Other dvgrab options

The default dvgrab options are:
dvgrab $default_dvgrabOptions \\
 --card \${CARD} --duration \${recordings-time} \${STORE}capture-\${DATE}-C\${CARD}-

If dvgrab options are given, the default options are all dropped.

See also:
dvgrab

ENDOFHELPTEXT

    exit;
};


# Get options
my $RecordingTime ="2sec";
$RecordingTime = pop(@ARGV) if $ARGV[-1] =~ /^[\d\.\:]+(h|min|s|sec|ms)?$/;

my @dvgrabOptions = ();
my %CardStorage = ();
foreach my $Option (@ARGV)
{
	if($Option =~ m!^(\-c|\-\-card)([\d]+)[\=]?!)
	{
		my $CurrentCardNumber = $2;
		my $CurrentCardStorage = $';
		# Default is the current working directory
		$CurrentCardStorage = "$defaultdir" unless $CurrentCardStorage =~ /\S/;
		# Store cards and numbers
		$CardStorage{$CurrentCardNumber} = $CurrentCardStorage;
	}
	else
	{
		push(@dvgrabOptions, $Option);
	};
};

unless(keys(%CardStorage))
{
	$CardStorage{0} = "/eris/Video/";
	$CardStorage{1} = "/hera/Video/";
};

# Get current date
my $DATE=`date`;
$DATE =~ s/\s/_/g;

my @CommandLines = ();
foreach my $CARD (keys(%CardStorage))
{
	my $STORE = $CardStorage{$CARD};
	my $RECTIME = $RecordingTime;
	my $currentCommandLine = eval($dvgrabCommandLine);
	push(@CommandLines, $currentCommandLine);
};

# Construct a single, long command line
my $MasterCommandLine = join(";\\\n", @CommandLines);
$MasterCommandLine .= "\n";

`$MasterCommandLine >>Journal.txt`;

#
