#!/usr/bin/env perl

=head1 LICENSE

Copyright (c) 2007-2011 Illumina, Inc.

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).

This file is part of the Consensus Assessment of Sequence And VAriation
(CASAVA) software package.

=cut

=head1 NAME

configureAlignment.pl

Create the analysis-specific configuration file for the Makefile.

=cut

=head1 DESCRIPTION

Uses the content of the config.xml to generate the initialization of all the
Makefile variables required for the analysis.

=cut

=head1 AUTHOR

Come Raczy

=head1 SYNOPSIS

    $ configureAlignment.pl --sample-sheet <sample-sheet> --config-file <config-file>

Runs configureAlignment.pl on the CSV <sample-sheet> and XML config file and generate
the resulting Makefile configuration on the standard output.

If no <sample-sheet> is provided, the "SampleSheet.csv" file in the current directory will be used.

If no <config-file> is provided, the "config.xml" file in the current directory will be used.

=head1 DETAIL

The top level operations are:

* parse the sample sheet
* generate the list of projects
* generate the list of samples for each project
* generate the list of lanes for each sample
* generate the list of barcodes for each lane
* load the config file
* generate the global variables

Example output:<<EOF;

projects := DNA_Project_A RNA_Project_B Control Unknown_indexes

DNA_Project_A_samples := NA10831 NA10859
RNA_Project_B_samples := NA10860 NA10861
Control_samples := PhiX
Unknown_indexes_samples := Unknown

DNA_Project_A_NA10831_lanes := 2 3
DNA_Project_A_NA10859_lanes := 2
RNA_Project_B_NA10860_lanes := 3
RNA_Project_B_NA10861_lanes := 3
Control_PhiX_lanes := 1
Unknown_indexes_Unknown_lanes := 2 3

DNA_Project_A_NA10831_2_barcodes := ATCACG
DNA_Project_A_NA10831_3_barcodes := ATCACG
DNA_Project_A_NA10859_2_barcodes := CGATGT
RNA_Project_B_NA10860_3_barcodes := TTAGGC
RNA_Project_B_NA10861_3_barcodes := CGATGT
Control_PhiX_1_barcodes := empty
Unknown_indexes_Unknown_2_barcodes := unknown
Unknown_indexes_Unknown_3_barcodes := unknown

EXPT_DIR := /absolute/path/to/EXPT_DIR

All output is on STDOUT.

EOF

=cut

use warnings "all";
use strict;
use Getopt::Long;
use Pod::Usage;
my $ENV_REF = \%ENV; # protect from CMake substitution
$ENV_REF->{"PATH"} = $1 if $ENV_REF->{"PATH"} =~ /^(.*)$/; # untaint the path

my $man = 0;
my $help = 0;
my $sampleSheet = "SampleSheet.csv";
my $configFile = "config.xml";

GetOptions('help|?' => \$help, man => \$man, 'sample-sheet=s' => \$sampleSheet, 'config-file=s' => \$configFile) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-verbose => 2,  -input => $1) if ($man and $0 =~ /(.*)/);
pod2usage("$0: Too many files given.\n")  if (0 < @ARGV);

use lib '@CASAVA_FULL_PERL_LIBDIR@';

use Casava::Alignment;
use Casava::Common::Log qw(initLog);


initLog( undef, 3, 0, 0, 0);
Casava::Alignment::configureAnalysis($sampleSheet, $configFile);

1;

__END__
