#!/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();


# 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
my $datestring = localtime();
print "--=--$datestring\n";

@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) ) {

	print "$target_list[$t_count]->{NAME}\n";
        my $tarball_name = sprintf("%s.tar.gz", $target_list[$t_count]->{NAME});
        my $download_area = sprintf("/data/CASP13/TARBALLS/predictions/%s", $tarball_name);
	my $targeto = $target_list[$t_count]->{NAME}."o";
	if ($target_list[$t_count]->{NAME} =~ m/^[HO]/){
		$targeto = $target_list[$t_count]->{NAME};
	}
        my $tarballo_name = sprintf("%s.tar.gz", $targeto);
        my $downloado_area = sprintf("/data/CASP13/TARBALLS/predictions/%s", $tarballo_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");
            }
            if(-e $dir) { 
		    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));
	    }
	}
        if (!(-e $downloado_area)) {
            my $diro = sprintf("%s/%s", $LOCAL_CONFIG->{DATA_QS_MODELS_DIR}, $targeto);
            if(-e $diro) {
	    system(sprintf("cd %s; tar -czf %s %s", $LOCAL_CONFIG->{DATA_QS_MODELS_DIR},$tarballo_name,$targeto));
	    system(sprintf("chgrp users %s/%s",$LOCAL_CONFIG->{DATA_QS_MODELS_DIR},$tarballo_name));
	    system(sprintf("chmod 664 %s/%s",$LOCAL_CONFIG->{DATA_QS_MODELS_DIR},$tarballo_name));
            
	    system(sprintf("mv %s/%s %s", $LOCAL_CONFIG->{DATA_QS_MODELS_DIR}, $tarballo_name, $downloado_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");
	#}
    }     
}
