#!/usr/bin/perl -w

# usage : perl join.pl
# Sarah Maman - 1 aout 2012 - Mis en place pour le traitement des mi-RNAs
# Copyright (C) 2012 INRA
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


use strict;
use File::Basename;


my $output = $ARGV[0];
my $inputs = @ARGV[1..$#ARGV];
#pour connaître le path vers le script join.pl
my $JOIN = '/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_miRNA_annotations/join.pl';
my $cmd = '';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."secMATRICE";


#print "\n ****** inputs : $inputs \n"; donne le nombre de fichiers entrants
#print "\n ****** @ de ARGV : @ARGV/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_miRNA_annotations/join.pl \n"; 
#donne la liste des fichiers (nom complet avec extension)

#TEST
#$cmd = "ls @ARGV";
#system $cmd;
#print "\n ****COMMANDE**\n $cmd \n";

$cmd = "($JOIN @ARGV[1..$#ARGV] > $ALL.csv) >& ./join.log 2>&1";
system $cmd;
#print "\n ****COMMANDE**\n $cmd \n";

if (! -e "$ALL.csv")
{
print STDERR "Echec lors de la generation de la matrice (fichier au format csv)\n";
}
else
{
$cmd = "(cp -a $ALL.csv $output) >& ./joincp.log 2>&1";
system $cmd;
}
