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

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


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



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


if(defined ($ARGV[0])){
	my $TARGET = $ARGV[0];
	$directory = "/data/CASP13/RESULTS/TEMPLATES/$TARGET";
	if(! -d $directory){
	   print("Usage: perl upload_templates.pl [TARGET]");
	   die("Directory with template for target $TARGET doesn't exist.\n")
	}
	print $TARGET . "\n";
        @FILES_LIST = ();
	$rtm->delete_results_for_target($TARGET);	
	#exit;
        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 @targets = $results_target_manager->get_result_targets();

for(my $i = 0; $i < scalar(@targets); $i++) {
	my $TARGET = $targets[$i]->{NAME};
print $TARGET . "\n";

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