#!/usr/bin/env perl
# PROJECT: CASAVA
# MODULE:  $RCSfile: makeGStudioBAMIndex.pl,v $
# AUTHOR:  Chris Saunders
#
# Copyright 2009 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).

# Simple wrapper around GS linear index utility.

use warnings FATAL => 'all';
use strict;

use lib '/home/psgendb/local/pkg/CASAVA_v1.8.2-build/lib/CASAVA-1.8.2/perl';
use Sys::Hostname;
use Getopt::Long;

use Casava::Common::Log qw(errorExit logWarning printLog);
use Casava::Common::IOLib qw(executeCmd);

my $argvStr     = join ' ', @ARGV;
my $cmdline     = "$0 $argvStr";
my $scriptName = (File::Spec->splitpath($0))[2];

my $bamPath = "";
my $help    = 0;

my $usage       =<<END;
$scriptName [options]
\t--bam      - BAM file to index
\t-h|--help  - print this message
END


my $result = GetOptions(
    "bam=s" => \$bamPath,
    "help|h" => \$help
);

if ((not $result) or $help) {
    errorExit "\n\n$usage";
}

errorExit "ERROR: Incorrect number of arguments\n$usage"
  unless ( $bamPath ne "" );

errorExit "ERROR: Unrecognized arguments: " . join(" ",@ARGV) . "\n" if(@ARGV);

{
    my $hostname          = hostname();
    printLog( "Running $hostname:[$cmdline]\n", 0);
}

my $libexecDir      = '/home/psgendb/local/pkg/CASAVA_v1.8.2-build/libexec/CASAVA-1.8.2';
my $indexBin        = File::Spec->catfile( $libexecDir , 'makeGStudioBAMIndex' );


if(not -e $bamPath) {
    logWarning("WARNING: BAM file does not exist: $bamPath\n");
    exit(0);
}

executeCmd("$indexBin $bamPath",5);

printLog( "$0 Finished\n", 0);
