#!/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 = 'qa_upload_results_from_list.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 = "";
my $target_list = 'conf/QA_1';
my $isparams = GetOptions(
	"target|t=s"         => \$TARGET,
	"list|l=s"         => \$target_list	
);




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

	$TARGET = $target;

	##############QA PREDICTIONS 
	$directory = "/data/CASP13/predictions/qa_predictions_cleaned/$TARGET";

	$logger->info($SCRIPTNAME, sprintf("Start to upload TARGET!: %s", $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_qa($file_name, $directory);
		print "File #" . $count_files . "\n";
	}
}


exit(0);
