#!/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 $TARGET = "T0398-D1";
#my $protein_pdb = '/data/CASP9/TARGETS/'.$TARGET.'.pdb';
#my $output_small_str_image_png = '/data/CASP9/PLOTS/STR/'.$TARGET.'.small.png'; #structure image
#my $images_manager = new ImagesManager();

my $target_name;
my $TARGET = GetOptions(
    "target|f=s" => \$target_name
);
    
my $target_list = 'conf/TR_targets_list';

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

#	$TARGET = $target;

# #### run this script when all results is uploaded
# $resultsupload_manager->update_rank($TARGET);
# 
# }

$TARGET = $target_name;


##############REFINEMENT RESULTS
$directory = "/data/CASP9/RESULTS/LGA/4A/DATA/$TARGET";
@FILES_LIST = ();
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_refinement_results($file_name, $directory);
	print "File #" . $count_files . "\n";
}


##############REFINEMENT TARGET RESULTS
$directory = "/data/CASP9/RESULTS/LGA/4A/DATA/$TARGET";
#@FILES_LIST = ();
#if(open(FILE_list_of_results, "/bin/ls " . $directory. "/* |")) {
#            @FILES_LIST = <FILE_list_of_results>;
#            close (FILE_list_of_results);
#}
$resultsupload_manager->upload_refinement_targets_results($TARGET, $directory);
print "File #" . $count_files . "\n";


#}


exit(0);
