#!/usr/bin/perl -w

# usage : perl sm_compress.pl <input_compressed> <extension> <uncompressed_file>
# 25/10/2013


# Copyright (C) 2013 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 $input_uncompressed = $ARGV[0];
my $extension          = $ARGV[1];
my $compressext        = $ARGV[2];
my $output_comp        = $ARGV[3];

my $myfile = ${input_uncompressed}.".".$extension ;

if ($compressext eq "tar"){
#Tar: les donnees sous ce format portent l extension .tar, pour decompresser ce type de donnees il faut taper en ligne de commande:
#tar xvf nom_du_fichier.tar
#mv file.dat file.extension
#
`tar -cvf $myfile;`;
}
if ($compressext eq "gz"){
#Gzip: les fichiers compressees en Gzip possedent l extension .gz, pour decompresser ces fichiers il faut taper en ligne de commande:
#gunzipgunzip nom_du_fichier.gz
`gzip $myfile;`;
}
if ($compressext eq "bzip2"){
#Bzip2: les fichiers compressees en Bzip2 possedent l extension .bz2, pour decompresser ces fichiers il faut taper en ligne de commande:
#bzip2 -d nom_du_fichier.bz2
`bzip2 $myfile;`;
}
if ($compressext eq ".tar.gz"){
#Tar/GZip (on parle generalement de Tarball): les donnees compressees en TAR et en GZIP portent l extension .tar.gz
#tar zxvf nom_du_fichier.tar.gz
`tar -zcvf $myfile;`;
}
if ($compressext eq ".tar.bz2"){
#Tar/BZip2: les donnees compressees en Tar et en Bz2 portent l extension .tar.bz2
#tar jxvf nom_du_fichier.tar.bz2
`tar -jcvf $myfile;`;
}

#Recuperation des fichiers par Galaxy
my $file=glob("$input_uncompressed*$compressext"); 
if (! -e "$file"){print STDERR "Decompressed file not found. \n";}else{`cp -a $file $output_uncomp`;}
