#!/usr/bin/perl
use strict;
use warnings;

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 Logger;
use SplitByParent;

my $SCRIPTNAME = 'create_tarballs.pl';

#my $logger = new Logger();

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
# # # # # # #id = 108 this is T0472 target
# # # # # # #id = 126 this is T0488 target
# # # # # # @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(126, "server")) {
# # # # # #         my @predictions = $predictions_manager->tarballs_predictions('T0488', "server");
# # # # # #         
# # # # # #         my $tarball_name = sprintf("%s.tar.gz", 'T0488');
# # # # # #         
# # # # # #         my $download_area = sprintf("/data/CASP9/TARBALLS/predictions/%s", $tarball_name);
# # # # # #         if (!(-e $download_area)) {
# # # # # #             my $new_dir = sprintf("%s/%s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, 'T0488');
# # # # # #             my $old_dir = sprintf("%s/%s", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, 'T0488');
# # # # # #             if(!(-e $new_dir)) {
# # # # # #                 system(sprintf("mkdir %s", $new_dir));
# # # # # #                 system(sprintf("chmod a+rwx %s", $new_dir));                
# # # # # #             }
# # # # # #             
# # # # # #                 
# # # # # #             for(my $p_count = 0; $p_count < scalar(@predictions); $p_count++) {            
# # # # # #                 my $new_file = sprintf("%s/%s%s%s_%s", $new_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictviktorions[$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":""));
# # # # # #                                
# # # # # #                 system(sprintf("cp %s %s", $old_file, $new_file));
# # # # # #                 system(sprintf("chmod a+r %s", $new_file));
# # # # # #                 
# # # # # #                 #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, 'T0488'));
# # # # # #             
# # # # # #             system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, $tarball_name, $download_area));
# # # # # #   #      }
# # # # # #         #/www/download_area/CASP9/server_predictions        
# # # # # #  #   }     
# # # # # # }
# # # # # # 
# # # # # # 



my $pfrmat = 'QA';

#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/CASP9/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 a+rwx %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));
        #}
        #/www/download_area/CASP9/server_predictions        
    #}     
#}






#my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);        
#my $date = sprintf("%s\/%s\/%s  %s:%s",($mon+1), $mday, $hour, $min, $sec);
#
#
#print $date."\n";

