use strict;
use warnings;
use File::Basename qw<basename dirname>;
use FindBin;
use File::Spec;
use Getopt::Long;

# perl /home/xiangyang/genome_bacth_retrive/genome_bacth/bash_fastANI.pl $query $split_number threadnumber $email $type_analysis

# perl /home/ubuntu/Bio-Server/TypeStrain_ANI/bash_fastANI.pl -u /home/ubuntu/Bio-Server/upload_dir/file -s 20 -m 30 -e 397310815@qq.com -t type_analysis

my %options = (
    'upload_file'                                  => undef,  
    'spilt_number'                                 => "4",
    'number_threads'                               => "3",
    'email'                                        => '397310815@qq.com',
    'type_analysis'                                => undef,
    'help'                                         => undef
);
 
GetOptions(
    'u|upload_file=s'                              => \$options{upload_file},    
    's|spilt_number=i'                             => \$options{spilt_number},     
    'm|number_threads=s'                           => \$options{number_threads}, 
    'e|email=s'                                    => \$options{email}, 
    't|type_analysis=s'                            => \$options{type_analysis},
    'h|help'                                       => \$options{help}

);

#my $now_time = localtime;

my $home_directory = $FindBin::Bin; 
my $f = $options{upload_file};  #/home/xiangyang/moxiu/upload_dir/$file
my $fn = basename $f;

my $spilt_number = $options{spilt_number};  

my $number_threads = $options{number_threads};
my $email = $options{email};
my $type = $options{type_analysis};

my $workplace = dirname($home_directory)."/workplace_$fn";
mkdir $workplace;

my $upload_dir = dirname $f;
opendir DH, $upload_dir;
my $num_files =  grep { -f "$upload_dir/$_" } readdir(DH);
closedir DH;
$num_files = $num_files-1;
my $website_suffix = $fn;
$website_suffix =~ s/\..*//g;
my $website = "https://www.microbialgenomic.cn:8101/downloadFile?fuuid=$website_suffix";
system("perl $home_directory/sendEmail0.pl -n $num_files -w $website -t $options{email}");
print "\$","\tBefore running your request, there are a total of $num_files tasks waiting for our service to handle.\n";

#frp is used to build Internal network penetration(内网穿透）
#system("sshpass -p '1' scp -P 6001 $f xiangyang\@124.223.154.39:/home/xiangyang/moxiu/upload_dir"); #Hp 
#using ssh approach to transfer files into Dell, which is instead by wget approach
#system("sshpass -p '1' scp -P 6001 $f xiangyang\@124.223.154.39:/home/xiangyang/moxiu/upload_dir"); #Dell 

system("cp $f -t /home/ubuntu/Bio-server/temp"); #copy file to temp dir
my $cmd = "perl /home/xiangyang/moxiu/TypeStrain_ANI/bash_fastANI.pl -u /home/xiangyang/moxiu/upload_dir/$fn -s $spilt_number -m $number_threads -e $email -t $type -d $workplace";
#system("sshpass -p '1' ssh xiangyang\@124.223.154.39 -p 6001 '$cmd'"); #Hp 
system("sshpass -p '1' ssh xiangyang\@124.223.154.39 -p 6000 '$cmd'"); #Dell

#############################################################
#system ("perl $home_directory/sendEmail.pl -f $workplace/out_result.zip -t $email");
system("rm -rf /home/ubuntu/Bio-server/temp/$f"); #delete file in temp dir
system("perl $home_directory/sendEmail1.pl -f $workplace/out_result.zip -w $website -t $email");

print "$workplace/out_result.zip\n";


