#!/usr/bin/env perl
# PROJECT: CASAVA
# MODULE:  $RCSfile: runExport2Fastq.pl,v $
# AUTHOR:  Lukasz Szajkowski
#
# Copyright (c) 2008 Illumina
# This software is covered by the "Illumina Genome Analyzer Software
# License Agreement" and the "Illumina Source Code License Agreement",
# and certain third party copyright/licenses, and any user of this
# source file is bound by the terms therein (see accompanying files
# Illumina_Genome_Analyzer_Software_License_Agreement.pdf and
# Illumina_Source_Code_License_Agreement.pdf and third party
# copyright/license notices).
#
# The scirpt creates a workflow for export to fastq conversion
#

use warnings FATAL => 'all';
use strict;
use POSIX qw(strftime);

use IO::File;
use Sys::Hostname;
use Getopt::Long;

use lib '@CASAVA_FULL_PERL_LIBDIR@';
use Casava::Common::Log;
use Casava::Common::IOLib qw(executeCmd genId);
use Casava::PostAlignment::Sequencing::Config
  qw(loadConfiguration %CONF_APP %CONF_PROJ %runsConfig );
use Casava::TaskManager qw(executeSingleTask checkPointInit 
    executeArrayTask executeArrayTaskEx executeCheckPoint addTask2checkPoint);

my $usage       =
    "runSortCount2bin.pl [options]\n"
  . "\t--projectDir         - Destination Dir is the directory to store the sorted files\n"
  . "\t--prevTarget=PATH    - previous target (dependent - default empty)\n";

my $projectDir = "";
my $help       = 0;
my $prevTarget = "";
my $result     = GetOptions(
    "projectDir|p=s" => \$projectDir,
    "prevTarget=s"   => \$prevTarget,
    "help|h"         => \$help
);
errorExit "ERROR: Incorrect number of arguments\n$usage" if ( $projectDir eq "" );
loadConfiguration($projectDir);
#############################################################
# Configuration
my $cmd               = '';
my $timeStampFormat   = $CONF_APP{formatTimeStamp};
my $hostname          = hostname();
my $argvStr           = join ' ', @ARGV;
my $projectResultsDir = $CONF_PROJ{dirBuildExport};
my $scriptDir         = '@CASAVA_FULL_LIBEXECDIR@';
my $export2FastqCmd   = File::Spec->catfile($scriptDir, 'export2fastq.pl');
my $exportCmd       = File::Spec->catfile($scriptDir, 'NCBIexport.pl');
#############################################################
if ($help) {
    print $usage;
    exit(0);
}
printLog("Running $hostname:[$0 " . $argvStr . "] \n",4);

my $currentId = "SYNCH_EXPORT1";
if ( $prevTarget eq 'start' ) {
    $currentId = 'N/A';
}

my $taskLabel      = "export2Fastq";
my $checkPointName = "EXP2FASTQ";
my $checkPointRef  = checkPointInit( $checkPointName, $taskLabel );

my $laneIndex = 0;
for my $runIdTmp ( sort keys %{ $runsConfig{run} } ) {
    my %sets = %{ $runsConfig{run}{$runIdTmp}{set} };
    foreach my $setIdTmp ( keys %sets ) {
        my @lanes = @{ $runsConfig{run}{$runIdTmp}{set}{$setIdTmp}{lanes} };
        foreach my $laneTmp (@lanes) {
            $cmd =
                "$export2FastqCmd  -p $projectDir "
              . "-r $runIdTmp -s $setIdTmp -l $laneTmp -t texp2fastq ";
            my $taskId = "FASTQ_$laneIndex";
            executeArrayTask( $cmd, $taskId, $currentId, %{$checkPointRef} );
            $laneIndex++;
        };    # foreach
    };    # foreach
};    # foreach

executeCheckPoint( %{$checkPointRef} );
$currentId = $checkPointName;

$cmd = "$export2FastqCmd --projectDir=$projectDir --target=sraXml ";  
executeSingleTask( $cmd, 'SYNCH_XML', 'sraXml', $currentId );
$currentId = 'SYNCH_XML';
