#!/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 ImagesManager;
use ResultsManager;
use ResultsTargetManager;
use ResultsUploadManager;

use Logger;

my $SCRIPTNAME = 'upload_results.pl';

#my $logger = new Logger();

my $directory = "";
my @FILES_LIST = ();
my $count_files = 0;


my $results_manager = new ResultsManager();
my $results_target_manager = new ResultsTargetManager();
my $resultsupload_manager = new ResultsUploadManager();
my $rtm = new ResultsTemplatesManager();

my $TARGET = "T0398-D1";
my $protein_pdb = '/data/CASP13/TARGETS/'.$TARGET.'.pdb';
my $output_small_str_image_png = '/data/CASP13/PLOTS/STR/'.$TARGET.'.small.png'; #structure image
my $images_manager = new ImagesManager();


# # # # 
# # # # my $target_list = 'conf/targets_list_all';
# # # # 
# # # # open (TL, $target_list) || die "Cannot open input file $target_list: $!";
# # # # while(my $target = <TL>) {
# # # # 	chomp($target);
# # # # 	if ($target =~ /^#/){next;}
# # # # 
# # # # 	$TARGET = $target;


# }



############## BEFORE TEMPLATES
# we have to rename all results in correct format



my $tar_templates_dir = '/data/CASP13/TARBALLS/templates';
my $res_templates_dir = '/data/CASP13/RESULTS/TEMPLATES';

my @TAR_FILES_LIST = ();

if(open(TAR_FILE_list_of_results, "/bin/ls " . $tar_templates_dir. "/* |")) {
            @TAR_FILES_LIST = <TAR_FILE_list_of_results>;
            close (TAR_FILE_list_of_results);
}
$count_files = 0;
foreach my $tar_line (@TAR_FILES_LIST) {
    	$count_files ++;	
	$tar_line =~ /$tar_templates_dir\/(.*)/;
	my $file_name = $1;
	#T0415.tar.gz
	$file_name =~ /(.{5})(.*).tar.gz/;	
	my $target_name = $1;
	my $range = $2;
	print $file_name . "   TARGET:" . $1 . "   RANGE: " . $2 . "   REAL _RANGE: " . $rtm->get_domain_by_specific_range($target_name, $range);
	
	if ($range eq '') {
		#tar -C /usr -xvf /data/package.tar
		system('mkdir ' . $res_templates_dir . '/' . $target_name);
		system("tar -C $res_templates_dir/$target_name -xvf $tar_line");

		$TARGET = $target_name;

		##############TEMPLATES (5A LGA PREDICTIONS)
		$directory = "/data/CASP13/RESULTS/TEMPLATES/$TARGET";
		@FILES_LIST = ();
		#my $targets_manager = new TargetsManager();
		#my $target_id = $targets_manager->get_id_by_name($TARGET);
		if(open(FILE_list_of_results, "/bin/ls " . $directory. "/* |")) {
			@FILES_LIST = <FILE_list_of_results>;
			close (FILE_list_of_results);
		}
		$count_files = 0;
		foreach my $line (@FILES_LIST) {
			$count_files ++;	
			$line =~ /$directory\/(.*)/;
			my $file_name = $1;
			$resultsupload_manager->upload_templates($TARGET, $file_name, $directory);
			print "File #" . $count_files . "  " . $directory ."/".$file_name. "\n";
		}

	}
	print "File #" . $count_files . "\n";
}

exit(0);



















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