#!/usr/bin/perl
#
# lsSnpPdbDownloadStep [-verbose] db ...
#
# Download lsSnpPdb tables directy from 
#
#   data/download/lsSnpPdb
#
# Expects .hg.conf file to define a sql profile:
#
#    lssnp.host=karchin-hn02.icm.jhu.edu
#    lssnp.user=ucsc
#    lssnp.password=XXXX
#
# -verbose - print details
#
# $Id: lsSnpPdbDownloadStep,v 1.1 2009/09/15 06:52:29 markd Exp $
#
use strict;
use warnings;
use File::Basename;
use FindBin;
use lib "$FindBin::Bin/../lib";
use gbCommon;
my $downloadDir = "data/download/lsSnpPdb";

# Do download for the specified data dir, if it's not there.
sub doDownload($) {
    my($db) = @_;
    my $dbDownloadDir = "$downloadDir/$db";
    my $tabFile = "$dbDownloadDir/lsSnpPdb." . isoTimeStamp() . ".tab.gz";
    if (-e $tabFile) {
        gbError("download file already exists: $tabFile");
    }
    makeDir($dbDownloadDir);
    my $tmpTabFile = "$tabFile.tmp";
    setupHgConf();
    runPipe("hgLsSnpPdbLoad fetch lssnp:LSSNP stdout | gzip -c >$tmpTabFile");
    renameFile($tmpTabFile, $tabFile);
}

# Entry point
while (($#ARGV >= 0) && ($ARGV[0] =~/^-.*/)) {
    my $opt = $ARGV[0];
    shift @ARGV;
    if ($opt eq "-verbose") {
        $gbCommon::verbose = 1;
    } else {
        gbError("invalid option \"$opt\"");
    }
}
if ($#ARGV < 0) {
    die("Wrong \# args: lsSnpPdbDownloadStep [-verbose] db ..");
}
my @dbs = @ARGV;

# use different task dir to allow running parallel with genbank
beginTask("lsSnpPdbDownload", "download");

foreach my $db (@dbs) {
    doDownload($db);
}
endTask();
