#!/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;
use PredictionsManager;

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_all();


=head
#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") && !($targets_manager->is_canceled($target_list[$t_count]->{ID}) == 1)) { # check if target is expired for server predictions

	# check if target is in predictions/closed_targets_server.list: if not, add it to the list
	my $isAppended = `grep \'$target_list[$t_count]->{NAME}\' predictions/closed_targets_server.list -c`;
	if($isAppended == 0){
		# append expired target to predictions/closed_targets_server.list
		system(sprintf("echo %s >> predictions/closed_targets_server.list",$target_list[$t_count]->{NAME}));
	}

	if($target_list[$t_count]->{NAME} =~ m/^[TR][0-9]{4}/){ # check if target is regular R0123 
          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/CASP13/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("chgrp casp %s", $new_dir));                
                  system(sprintf("chmod 775 %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%s", $old_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{GROUP_CODE}, $predictions[$p_count]->{MODEL}, (($predictions[$p_count]->{PFRMAT} eq 'AL')?'.pdb':'')); # old format: now in cleaned directory 'AL' predictions without extension .pdb
		   my $old_file = sprintf("%s/%s%s%s_%s", $old_dir, $predictions[$p_count]->{TARGET}, $predictions[$p_count]->{PFRMAT}, $predictions[$p_count]->{GROUP_CODE}, $predictions[$p_count]->{MODEL});

                  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 --remove-files", $LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name, $target_list[$t_count]->{NAME}));
	      system(sprintf("chgrp casp %s/%s",$LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name));
	      system(sprintf("chmod 664 %s/%s",$LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name));
            
              system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{TARBALLS_PREDICTIONS_DIR}, $tarball_name, $download_area));

          }
	}
        #/www/download_area/CASP13/server_predictions
    }     
}
=cut

# CREATE ALL (by human expired) TARBALLS
# and append expired targets to closed_targets.list
# and copy files /data/CASP13/tmp_QA/T0123.stat.txt to /data/CASP13/ASSESSORS/TARBALLS/results/qa


@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") && $targets_manager->is_expired($target_list[$t_count]->{ID}, "QA") && !($targets_manager->is_canceled($target_list[$t_count]->{ID}) == 1) ) {

         
        my $tarball_name = sprintf("%s.tar.gz", $target_list[$t_count]->{NAME});
        my $download_area = sprintf("/data/CASP13/TARBALLS/predictions/%s", $tarball_name);
        if (!(-e $download_area)) {
            my $dir = sprintf("%s/%s", $LOCAL_CONFIG->{DATA_MODELS_DIR}, $target_list[$t_count]->{NAME});
            if(!(-e $dir)) {
                system("perl /local/Projects/Perl/casp13/src/scripts/copy_4eval.pl");
            }
            
	    system(sprintf("cd %s; tar -czf %s %s", $LOCAL_CONFIG->{DATA_MODELS_DIR},$tarball_name,$target_list[$t_count]->{NAME}));
	    system(sprintf("chgrp users %s/%s",$LOCAL_CONFIG->{DATA_MODELS_DIR},$tarball_name));
	    system(sprintf("chmod 664 %s/%s",$LOCAL_CONFIG->{DATA_MODELS_DIR},$tarball_name));
            
	    system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{DATA_MODELS_DIR}, $tarball_name, $download_area));

	    # append expired targets to closed_targets.list
	    # system(sprintf("echo %s >> predictions/closed_targets.list",$target_list[$t_count]->{NAME}));

        }   

	# copy files from tmp_QA dir with statistics regarding prosa, molprobity and QA_consensus scores
	my $src_file_to_copy = sprintf("/data/CASP13/tmp_QA/%s.stat.txt", $target_list[$t_count]->{NAME});
	my $dest_file_to_copy = sprintf("/data/CASP13/ASSESSORS/TARBALLS/results/qa-molprobity/%s.serv.txt", $target_list[$t_count]->{NAME});
	if((-e $src_file_to_copy) && !(-e $dest_file_to_copy)){
		system("cp -p $src_file_to_copy $dest_file_to_copy");
	}
    }     
}
