#!/usr/bin/perl

use Cwd;
use Proc::Reliable;
use strict;
use warnings;

my @folders = grep { /[A-Z]/ } grep { -d $_ } < ?? >;

for my $folder (@folders) {
    print STDERR "Training [$folder]";
    if (-f "$folder/$folder.txt.bz2") {
        print STDERR "\n";
        my ($name) = grep { /^[a-z]/ } map { s!$folder/!!; $_ } <$folder/*>;
        print STDERR " ! language name: $name\n";
        print STDERR " ! language code: $folder\n";

        open F, ">$folder.batch";
        print F template($folder, $name);
        close F;

        my $proc = Proc::Reliable->new();
        my ($stdout, $stderr, $status, undef) = $proc->run("qsub $folder.batch");


    } else {
        print STDERR " - skipped (no training data)\n";
    }
}

sub template {
    my $lang = shift;
    my $name = shift;
    my $cwd = cwd();
    return <<"EOT";
#!/bin/sh
#PBS -l walltime=10:00:00
#PBS -l nodes=1:ppn=1
#

cd $cwd/$lang;
bunzip2 $lang.txt.bz2
make-lingua-identify-language -u $lang $name $lang.txt
bzip2 $lang.txt
EOT
}
