#!/usr/bin/perl

use strict;
use warnings;

my $list_file = "/data/CASP13/MODELS/parent_models.txt";

#my @plugins = qw/lga_sda lga_sia mammoth DaliLite LVR/;
my @plugins = qw//;

my @domains = qw/D1 D2 D3 D4 D5 D6 D12 D13 D14 D15 D16 D23 D24 D25 D26 D34 D35 D36 D45 D46 D56/;
#my @domains = qw/D1 D2 D12/;

my $exlude_targets = "T0652 T0656 T0657 T0686 T0739 T0751 T0719";
#my $include_targets = "T0649 T0651 T0653 T0654";
my $include_targets = "T0713";

my @models = ();
my @targets = ();
my @models_1 = ();
my %hash1;
my %hash2;

open FILE, "< $list_file";
while(defined(my $line = <FILE>)){
  chomp $line;
  if ( $line =~ m/(^T[0-9]{4})(TS[0-9]{3}_[1-9])_?/ ) {
	my $target = $1;
	my $model_1 = $1.$2;
#	if($exlude_targets =~ m/$target/){
	if($include_targets !~ m/$target/){
	   next;
	}
	push (@models, $line);
	$hash1{$target} = 1;
	$hash2{$model_1} = 1;
  }
}
close(FILE);

@targets = sort keys %hash1;
@models_1 = sort keys %hash2;

# delete results files
foreach my $model_1 (@models_1){
#   print (sprintf("find /data/CASP13/RESULTS/ -name %s | xargs --no-run-if-empty echo \n",$model_1));
#   system (sprintf("find /data/CASP13/RESULTS/ -name %s* | xargs --no-run-if-empty rm -rf ",$model_1));
}

foreach my $target (@targets){
   print "$target\n";
}
#exit;

my %hash_caca;

foreach my $model (@models){
   my $target = substr($model,0,5);
	if(!defined($hash_caca{$target})){
	    system ("./rsync_upload.sh $target caca ");
	    print ("./rsync_upload.sh $target caca \n");
	    $hash_caca{$target} = 1;
	}
   foreach my $domain (@domains){
	if(!defined($hash_caca{"$target-$domain"}) && $domain =~ m/D[1-6]$/ && -e "/local/CASP13/TARGETS/$target-$domain.pdb"){
                    print ("./rsync_upload.sh $target-$domain caca \n");
                    system ("./rsync_upload.sh $target-$domain caca ");
                    $hash_caca{"$target-$domain"} = 1;
	}  
   }
   foreach my $plugin (@plugins){
	if (-e "/local/CASP13/TARGETS/$target.pdb" && -e "/local/CASP13/MODELS/$target/$model"){
	     system("./rsync_upload.sh $target $plugin $model ");
	     print ("./rsync_upload.sh $target $plugin $model \n");
	}
	if($target eq 'T0678' || $target eq 'T0744'){next;} # these targets should be run  from admin web-site
	foreach my $domain (@domains){
	    if(-e "/local/CASP13/TARGETS/$target-$domain.pdb" && -e "/local/CASP13/MODELS/$target-$domain/$model-$domain"){
		if($domain =~ m/D[1-6]{2}/ && $plugin ne "lga_sda") {next;}
		system("./rsync_upload.sh $target-$domain $plugin $model-$domain ");
		print ("./rsync_upload.sh $target-$domain $plugin $model-$domain \n");
	    }
	}
   }
}

exit;

=cut
foreach my $target (@targets){
    printf ("find /data/CASP13/RESULTS/CACA/ -name $target |xargs --no-run-if-empty echo  \n");
   system(sprintf ("find /data/CASP13/RESULTS/CACA/ -name $target* |xargs --no-run-if-empty echo  "));
#    print ("./rsync_upload.sh $target caca \n");
#    system ("./rsync_upload.sh $target caca ");
}

exit;
=cut
