#!/usr/bin/perl -w 
# usage : perl save files

use strict; 
use File::Basename; 

my $username            = $ARGV[0]; 
my $repertoire          = $ARGV[1]; 
my $out                 = $ARGV[2]; 
my $cmd                 = '';
my $cmd2                = '';
my $cmd2bis             = '';
my $list2='';my $list2bis='';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec";

$cmd2 = "(ls -ltrahR /work/$username/ > ${ALL}_list_${username}_work;) >& ./cp2_out_save.log 2>&1";
system $cmd2;
$list2 =`more ${ALL}_list_${username}_work;`;
print STDOUT "$list2 \n";

$cmd2bis = "(ls -ltrahR /work/$username/$repertoire/ > ${ALL}_list_${username}_work2;) >& ./cp2_out_save.log 2>&1";
system $cmd2bis;
$list2bis =`more ${ALL}_list_${username}_work2;`;
print STDOUT "$list2bis \n";

#creation d'un fichier html
open (HTML, ">", "results.html") or die "Cannot create results.html";
print HTML ("<br><br><br><br><br><br>");
print HTML "$username,";
print HTML ("<br><br>");
#print HTML "To visualize your /work, please click on information icone (i), then on stdout.Voici la liste de vos fichiers et repertoires contenus dans votre /work/$username/ :\n";
print HTML "To visualize your /work, please click on information icone (i)'Tool Standard Output' in your dataset, then on 'stdout'.\n";
print HTML ("<br><br>");


#Recuperation du rapport html dans Galaxy
if (! -e "results.html"){print STDERR "Pas de fichier html produit \n";}
else {$cmd = "(mv results.html $out) >& ./cp_out_save.log 2>&1";
system $cmd;}
close( HTML );
`rm ${ALL}_list_${username}_work`;
