#!/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();
my $create_with_name = "T0556";
#
#SERVERS TARBALLS
for(my $t_count = 0; $t_count < scalar(@target_list); $t_count++) {
    if($targets_manager->is_expired($target_list[$t_count]->{ID}, "server") and $target_list[$t_count]->{NAME} = $create_with_name) {
        my @predictions = $predictions_manager->tarballs_predictions($target_list[$t_count]->{NAME}, "server");
        
        my $tarball_name = sprintf("%s.3D.srv.tar.gz", $target_list[$t_count]->{NAME});
        
        my $download_area = sprintf("/www/download_area/CASPROL/server_predictions/%s", $tarball_name);
        if (!(-e $download_area)) {
            my $new_dir = sprintf("%s/%s", $LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME});
            my $old_dir = sprintf("%s/%s", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME});
            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]->{GROUP}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{MODEL}, (($predictions[$p_count]->{PFRMAT} eq 'AL')?'.pdb':''));
                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))        
            }
            
            system(sprintf("cd %s; tar -czf %s %s", $LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name, $target_list[$t_count]->{NAME}));
            
            system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name, $download_area));
        }
        
    }     
}


#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") and $target_list[$t_count]->{NAME} = $create_with_name) {
        my @predictions = $predictions_manager->tarballs_predictions($target_list[$t_count]->{NAME});
        
        my $tarball_name = sprintf("%s.tar.gz", $target_list[$t_count]->{NAME});
        
        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}, $target_list[$t_count]->{NAME});
            my $old_dir = sprintf("%s/%s", $LOCAL_CONFIG->{CLEAN_PREDICTIONS_DIR}, $target_list[$t_count]->{NAME});
            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}, $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":""));
                               
                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, $target_list[$t_count]->{NAME}));
            
            system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{TARBALLS_ALL_PREDICTIONS_DIR}, $tarball_name, $download_area));
        }
    }     
}

#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";

