#!/usr/bin/perl -w

##################################################################
## gotcloud.pl : interface to alignment & variant calling pipelines
##################################################################
use strict;
use Getopt::Long;
use Pod::Usage;
use Cwd;
use FindBin;
use lib "$FindBin::Bin/lib";
my $dir = $FindBin::RealBin;
my $bindir = "$dir/bin";

##################################################################
## Determine the types of analysis to be done
## Commands can be
## perl gotcloud.pl align [options]    : Alignment pipeline
## perl gotcloud.pl snpcall  [options] : Snp Calling pipeline
##################################################################
if ( $#ARGV < 0 ) {
    print STDERR "ERROR: Missing command. Please see the usage below.\n";
    pod2usage(1);
}
elsif ( $ARGV[0] =~ /^([\-])*man$/ ) {
    pod2usage(-verbose => 2);
}
elsif ( $ARGV[0] =~ /^([\-])*help$/ ) {
    pod2usage(1);
}
elsif ( $ARGV[0] =~ /^([\-])*version$/ ) {
  if( -f "$dir/release_version.txt")
  {
    print "gotcloud Version: ".`cat $dir/release_version.txt`;
  }
  elsif( -f "$dir/../release_version.txt")
  {
    print "gotcloud Version: ".`cat $dir/../release_version.txt`;
  }
  else
  {
    print "gotcloud Version: Can't find this information.";
  }
}
elsif ( $ARGV[0] eq "align") {
    shift(@ARGV);
    if( -f "$bindir/align.pl" )
    {
      exec("$bindir/align.pl",@ARGV);
    }
    elsif( -f "$dir/align.pl" )
    {
      exec("$dir/align.pl",@ARGV);
    }
    else
    {
      die "ERROR: could not find align.pl"
    }
}
elsif ( $ARGV[0] eq "snpcall") {
    shift(@ARGV);
    if( -f "$bindir/umake.pl" )
    {
      $bindir = $bindir;
    }
    elsif ( -f "$dir/umake.pl" )
    {
      $bindir = $dir;
    }
    else
    {
      die "ERROR: could not find umake.pl for snp-calling"
    }
    exec("$bindir/umake.pl", "--snpcall",@ARGV);
}
elsif ( $ARGV[0] eq "vc") {
    shift(@ARGV);
    if( -f "$bindir/umake.pl" )
    {
      $bindir = $bindir;
    }
    elsif ( -f "$dir/umake.pl" )
    {
      $bindir = $dir;
    }
    else
    {
      die "ERROR: could not find umake.pl for variant-calling"
    }
    exec("$bindir/umake.pl", @ARGV);
}
elsif ( $ARGV[0] eq "ldrefine") {
    shift(@ARGV);
    if( -f "$bindir/umake.pl" )
    {
      $bindir = $bindir;
    }
    elsif ( -f "$dir/umake.pl" )
    {
      $bindir = $dir;
    }
    else
    {
      die "ERROR: could not find umake.pl for ldrefine";
    }

    system("$bindir/umake.pl", "--beagle",@ARGV);
    my $rc = ${^CHILD_ERROR_NATIVE};
    die "failed the first step of ld genotype refinement: $?" if ($rc);
    exec("$bindir/umake.pl", "--thunder",@ARGV);
  }
else {
    print STDERR "ERROR: Unknown command $ARGV[0]. Please see the usage below.\n";
    pod2usage(1);
}

__END__

=head1 NAME

gotcloud - sequencing and genotyping software pipelines that also work on the cloud

=head1 SYNOPSIS

gotcloud [command] [options] 

 Command:
   help            Print out brief help message
   man             Print the full documentation in man page style
   version         Print the gotcloud version
   align           Run the alignment pipeline
   snpcall         Run the snp calling pipeline
   ldrefine        Run the LD-aware genotype refinement pipeline
   vc              Run the variant call steps that are configured on in your configuration file

 Visit http://genome.sph.umich.edu/wiki/GotCloud for more detailed documentation

=head1 COMMANDS

=over 8

=item B<help>

Print a brief help message and exits.

=item B<man>

Prints the manual page and exits.

=item B<version>

Prints the got cloud version and exits.

=item B<align>

Run the alignment pipeline. Type 'gotcloud align -help' for more detailed information.

=item B<snpcall>

Run the snp calling pipeline. Type 'gotcloud snpcall -help' for more detailed information

=item B<ldaware>

Run the LD-aware genotype refinement pipeline pipeline. Type 'gotcloud ldaware -help' for more detailed information

=back

=head1 DESCRIPTION

B<gotcloud> is an efficient and flexible software pipeline for sequence-based
genetic analysis. It takes FASTQ, BAM, or VCF-formatted files as input and performs a wide variety of processing/analysis on them.

Visit http://genome.sph.umich.edu/wiki/GotCloud for more detailed documentation

=cut
