#!/usr/bin/perl -w

# usage : perl split files
# Sarah Maman - 04/12/2012
# 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 $input_vcf = $ARGV[0];
my $output_file_1 = $ARGV[1];
my $output_file_2 = $ARGV[2];
my $output_file_3 = $ARGV[3];
my $output_file_4 = $ARGV[4];
my $output_file_5 = $ARGV[5];
my $cmd = '';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec"; 



my $system_output = `wc -l $input_vcf`;
my ($nbligne) = ( $system_output =~ /\s*(\d+)\s*/ );
print $nbligne . "\n";
my $nb = int(($nbligne / 5) + 0.99) ;
print $nb . "\n" ;



#commande split
$cmd = "cd /galaxydata/database/files/workspace/; mkdir $ALL/; chmod -R 777 $ALL; cp -a $input_vcf $ALL/.;cd $ALL; split -l $nb $input_vcf";
#chmod a+rw $ALL;
system $cmd;

my $all2 = "/galaxydata/database/files/workspace/${ALL}";

if (! -e "$all2/xaa"){print STDERR "Echec du split xaa\n";}
else{`(cp -a \"$all2/xaa\" $output_file_1) >& ./split1.log 2>&1`}


if (! -e "$all2/xab"){print STDERR "Echec du split xab\n";}
else{`(cp -a \"$all2/xab\" $output_file_2) >& ./split2.log 2>&1`}


if (! -e "$all2/xac"){print STDERR "Echec du split xac\n";}
else{`(cp -a \"$all2/xac\" $output_file_3) >& ./split3.log 2>&1`}


if (! -e "$all2/xad"){print STDERR "Echec du split xad\n";}
else{`(cp -a \"$all2/xad\" $output_file_4) >& ./split4.log 2>&1`}

if (! -e "$all2/xae"){print STDERR "Echec du split xae\n";}
else{`(cp -a \"$all2/xae\" $output_file_5) >& ./split5.log 2>&1`}

