#!/usr/bin/perl -w
use strict;

#person in charge of this script
#this should be the person currently in charge of scripts for the SGN project
my $script_maintainer='Teri Solow <tms45@cornell.edu>';

#local packages to use
use db_link;
use projects;
use runtime;
use Net::SCP::Expect;

# lame config
my $path = '/home/teri/pgn_processing/scripts/dump_scripts/';

@ARGV or print "No input parameters, proceeding with default.\n";

my @arg_pairs = split (/\-/, (join ' ', @ARGV));
my %args=();
my @unigene_build_ids;
my @lib_prefixes;
foreach (@arg_pairs){
	$_ or next;
	my ($flag, @val)=split /\s+/;
	if ($flag eq 'b') {
		@unigene_build_ids=@val;
	}
	if ($flag eq 'l') {
		@lib_prefixes=@val;
	}
	$args{$flag}=$val[0];
}
my $project=$args{'p'};
my $lib=$args{'l'};
my $set=$args{'b'};

$project or die "Please specify a project (cgn, fgn, pgn) using the -p flag.\n";
$set or die "You must select the unigene build IDs with the -b flag.\n";

# ssh configuration variables
my $username = 'transfer';
my $password = 'cornell2fgp!';
my $login_identification = '/data/shared/pgn_data_processing/scripts/processing_components/data_files/fgn_psu_ssh/identification';
my $psu_machine_ip = '146.186.172.248';
my $remotepath = '/home/fgp/transfer/tracefiles/tocornell/uploads/';

# scp setup
my $scp = Net::SCP::Expect->new(host=>$psu_machine_ip,user=>$username,password=>$password,verbose=>'1',auto_yes=>'1');
$scp->login($username,$password);

# database configuration
my ($db,$usr) = @{projects::get_db_info($project)};

###########################################################################
# Main body of script
###########################################################################
my $start_time=time;

my @files_to_copy;

foreach my $id (@unigene_build_ids) {

	# process unigene file
	print "Dumping $project unigene file for $id...\n";
	my $u_command = "${path}td_unigene.pl -p $project -b $id -o /data/shared/pgn_data_processing/files_for_kerr/unigene";
	system($u_command);
	push @files_to_copy, "/data/shared/pgn_data_processing/files_for_kerr/unigene/unigene_build-$id.txt";
	
	# process unigene-est file
	print "Dumping $project unigene-est file for $id...\n";
	my $ue_command = "${path}td_unigene_est.pl -p $project -b $id -o /data/shared/pgn_data_processing/files_for_kerr/unigene_est";
	system($ue_command);
	push @files_to_copy, "/data/shared/pgn_data_processing/files_for_kerr/unigene_est/unigene-est-$id.txt";

	# check to see if these builds are the most recent for these organisms
	# if so, copy over  $lib.fasta.cap.ace and $lib.fasta.cap.singlets files
	# ######################################################################
	my $dbh = db_link::connect_db($db,$usr) or die "couldn't open database link!\n";
	
	# get organism id
	my ($organism, $lib_prefix);
	my $stm = "select u.organism_id, o.lib_prefix from unigene_build as u left join organism as o on u.organism_id=o.organism_id where unigene_build_id='$id';";
	my $sth = $dbh->prepare($stm);
	my $rv = $sth->execute;
	my $rc = $sth->bind_columns(\$organism, \$lib_prefix);
	$sth->fetch;
	$sth->finish;
	
	# find the most recent build_id for this organism
	my $newest_build;
	$stm = "select unigene_build_id from unigene_build where organism_id='$organism' order by unigene_build_id desc limit 1";
	$sth = $dbh->prepare($stm);
	$rv = $sth->execute;
	$rc = $sth->bind_columns(\$newest_build);
	$sth->fetch;
	$sth->finish;
	
	if ($id == $newest_build) {
		system("cp /data/shared/pgn_data_processing/unigene_builds/$project/$lib_prefix/$lib_prefix.fasta.cap.ace /tmp/unigene_build-$id.ace");
		push @files_to_copy, "/tmp/unigene_build-$id.ace";
		system("cp /data/shared/pgn_data_processing/unigene_builds/$project/$lib_prefix/$lib_prefix.fasta.cap.singlets /tmp/unigene_build-$id.singlets");
		push @files_to_copy, "/tmp/unigene_build-$id.singlets";
	}

	db_link::disconnect_db($dbh);
}

# process all ESTs
print "Dumping $project lib-est files for all libraries...\n";
my $est_command = "${path}td_lib_est.pl -p $project -l all -o /data/shared/pgn_data_processing/files_for_kerr/lib_est/";
system($est_command);
opendir(LIBEST, "/data/shared/pgn_data_processing/files_for_kerr/lib_est/");
my @lib_est_files = readdir(LIBEST);
closedir(LIBEST);
foreach(@lib_est_files) {
	if ($_ =~ /est-[A-Za-z]{3}[0-9]{2}\.txt/) {
		push @files_to_copy, "/data/shared/pgn_data_processing/files_for_kerr/lib_est/".$_;
	}
}

# get summary unigene file
print "Dumping $project unigene builds file...\n";
my $builds_command = "${path}dump_unigene_builds.pl -p $project -l all -o /data/shared/pgn_data_processing/files_for_kerr/";
system($builds_command);
push @files_to_copy, "/data/shared/pgn_data_processing/files_for_kerr/fgn_unigene_builds.txt";

# copy all files
foreach(@files_to_copy) {
	print "Copying $_...\n";
	$scp->scp("$_", ":$remotepath");
}

# delete all local copies
foreach(@files_to_copy) {
	if ($_ !~ /unigene_builds/) {
		print "Deleting $_...\n";
		unlink($_);
	}
}

runtime::runtime_print($start_time, "File upload");


