#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use MIME::Parser;
use MIME::Entity;
use MIME::Body;
use Getopt::Long;

use lib qw(Core);
use lib qw(Classes);

use LocalConfiguration;

use StatusManager;
use TargetsManager;
use PredictionsManager;
use Logger;
use SplitByParent;

my $SCRIPTNAME = 'create_tarballs_PFRMAT.pl';

my $pfrmat;

if ($#ARGV < 0) {
      print "\nPlease specify PFRMAT to create cleaned tarballs. Usage: $FindBin::Script  RR\n\n";
      exit;
} else {
	$pfrmat = $ARGV[0];
}


my $targets_manager = new TargetsManager();
my $status_manager = new StatusManager();
my $predictions_manager = new PredictionsManager();


my $split_by_parent = new SplitByParent();

my @group_list = $status_manager->info_group_list();
my @target_list = $status_manager->info_public_target_list();

	#CREATE ALL (by human expired) TARBALLS
	@group_list = $status_manager->info_all_group_list();
	for(my $t_count = 0; $t_count < scalar(@target_list); $t_count++) {
    	if($targets_manager->is_expired($target_list[$t_count]->{ID}, "human")) {
        my @predictions = $predictions_manager->tarballs_predictions_selected('','', $pfrmat);
        

print scalar(@predictions) . "\n\n";

        my $tarball_name = sprintf("%s.tar.gz", $pfrmat);
        
        my $download_area = sprintf("/data/CASPROL/TARBALLS/predictions/%s", $tarball_name);
        if (!(-e $download_area)) {
            my $new_dir = sprintf("%s/%s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, $pfrmat);
            if(!(-e $new_dir)) {
                system(sprintf("mkdir %s", $new_dir));
                system(sprintf("chmod 775 %s", $new_dir));                
            }
            
                
            for(my $p_count = 0; $p_count < scalar(@predictions); $p_count++) {
		
		my $old_dir = sprintf("%s/%s", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $predictions[$p_count]->{TARGET});            
                
		my $new_file = sprintf("%s/%s%s%s_%s", $new_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{GROUP_CODE}, $predictions[$p_count]->{MODEL});
                
                my $old_file = sprintf("%s/%s_%s%s%s", $old_dir, $predictions[$p_count]->{GROUP}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{MODEL}, (($predictions[$p_count]->{PFRMAT} eq "AL")?".pdb":""));
                

print sprintf("cp \"%s\" %s", $old_file, $new_file)."\n\n";


                system(sprintf("cp \"%s\" %s", $old_file, $new_file));
                system(sprintf("chmod a+r %s", $new_file));
                print $old_file . "\n";
		print $new_file . "\n";

                #split by PARAMETERS
                $split_by_parent->process($new_file);

            }
            
            system(sprintf("cd %s; tar -czf %s %s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, $tarball_name, $pfrmat));
            
            system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, $tarball_name, $download_area));
        }
    }     
}
