#! /usr/bin/env perl
use Getopt::Long;
use Cwd;
use Time::Local;
use strict;
use warnings;

my $projecthome = "/home/bcheung/rundata";
my $destination = $projecthome . "/";

my %projects = ( );

$projects{eeg_real}  = 1;
$projects{eeg_sim}  = 1;
$projects{eeg_simNL}  = 1;
$projects{eeg_simNLP}  = 1;
$projects{eeg_shuffle}  = 1;



my ($help, $source, $scenario, $project);

GetOptions
(
		'source=s' => \$source, #ENVVARS assumed and added to say run22744_8_5_2008
        'help' => \$help,
		'project=s' => \$project,
		'scenario=s' => \$scenario
);

if(defined $help) {
	help();
	exit(0);
}

if(!(defined $project)) {
	$project = "eeg_sim";
}

if(!(defined $source)) {
	print "Need directory to process!\n";
	die "Must use --source=xxxxxxxxx\n";
} else {
	if(!(-d "$source")) {
		print "Current files and folders here:\n";
		system("ls -l");
		die "Target dirrectory to process does not exist<$source>\n";
	}
}


if(!(defined $scenario)) {
	print "Need SCENARIO so we can name the new dataset\n";
	die "Must use --scenario=xxxxxxxxx\n";
}

if(!(defined $project)) {
	print "Need PROJECT so we can place the new dataset\n";
	die "Must use --project=xxxxxxxxx\n";
} else {
	if(!(exists $projects{$project})) {
		print "Project choses:\n";
		foreach my $choice (keys %projects) {
			print "Project Choice: $choice\n";
		}
		die "Chosen project not in the list:<$project>\n";
	}
}

$destination = $destination . $project . "/" . "dataset_$scenario";

#if(-d $destination) {
	#die "Dataset <$destination> already exists, chose new scenario <$scenario>\n";
#}

system("mkdir $destination");
if(!(-d $destination)) {
	die "Failed to create new dataset<$destination>\n";
}

my $cwd = getcwd();

chdir($source);

my $dir = ".";

opendir DH, $dir || die "Failed to pen dir $dir: !$\n";
foreach my $file (readdir DH)
{
	next if $file =~ /^\.\.?$/;
	print "looking at <$file>\n";
	if($file =~ /^.*num_subprocess.*$/) {
		print "Moving parallelism count file<$file>\n";
		system("cp $file $destination");
	} elsif($file =~ /^.*output_(\d*).mat$/) {
		mkdir("$destination/$1");
		system("cp $file $destination/$1/input.mat");
	} elsif($file =~ /^para_file.*\.mat$/) {
		print "Moving parameter file<$file>\n";
		system("cp $file $destination/param_file.mat");
	}
}
closedir(DH);
chdir($cwd);

print "Bye\n";
exit(0);

sub help {

		print "Usage:

./createdataset.pl --source=eeg_sim/sim_4ROIs_loc_mismatch --scenario=sim_4ROIs_loc_mismatch



Options:
                [-h/--help]          See this
                [-p/--project=s]     What project is it for
                [-s/--scenario       What scenario to name it
                [-t/--source]        What directory to process
		 \n";

}
