#!/usr/bin/perl
# findCallParallel.pl                   wkretzsch@gmail.com
#                                       29 Aug 2013

use warnings;
use strict;
$|=1;
use Data::Dumper;

use File::Path qw(make_path);
use File::Basename;
use Env qw(HOME);

use Getopt::Std;
my %args;
getopts( 'drj:', \%args );
my $DEBUG = $args{d} || 1;

use DM;
my $dryRun = $args{r} ? 0 : 1;
my $numJobs = $args{j} || 1;
my $dm = DM->new( dryRun => $dryRun, numJobs => $numJobs );

my $wavList = 'wav.list';
open (my $fh, '<', $wavList) or die "could not open $wavList";
my @wavs;
while(<$fh>){
    chomp;
    push @wavs, $_;
}

# make symbolic links for files with spaces in them
for my $wav (@wavs){
    next unless $wav =~ m/\s/;
    my $origWav = $wav;
    $wav =~ s/\s/_/g;
    symlink $origWav, $wav unless -e $wav;
}

for my $wav (@wavs){
    next unless $wav =~ m/\.wav$/;
    my ($name, $path, $suffix) = fileparse($wav, '.wav');
    my $target = "${path}$name/matrix.out.npy";
    $dm->addRule($target, $wav, "export PYTHONPATH=/data/lib/python2.7 && time python2.7 findcall2.7.1.py -p 100 $wav");
}

$dm->execute();

__END__

=head1 NAME

findCallParallel.pl

=head1 SYNOPSIS
   

=head1 DESCRIPTION
Stub documentation for findCallParallel.pl, 
created by template.el.

It looks like the author of this script was negligent
enough to leave the stub unedited.


=head1 AUTHOR

Warren Winfried Kretzschmar, E<lt>wkretzsch@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2013 by Warren Winfried Kretzschmar

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.2 or,
at your option, any later version of Perl 5 you may have available.

=head1 BUGS

None reported... yet.

=cut
