#!/usr/bin/perl
#
# ccdsDownloadStep [-verbose] [-ftp-verbose]
#
# Download CCDS table dumps if new files are available
#
# Download a lastest file in the form CCDS.20080630.tar.gz to
#
#   data/download/ccds/
#
# Expects a file etc/ccds.conf which is a PERL script setting the
# variables:  $main::ccdsFtpHost, $main::ccdsFtpUser, $main::ccdsFtpPassword
#
# -verbose - print details
# -ftp-verbose - print details of interaction with ftp server
#
# $Id: ccdsDownloadStep,v 1.2 2008/07/01 06:40:45 markd Exp $
#
use strict;
use warnings;
use File::Basename;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Net::FTP;
use gbCommon;
use gbFtp;

# Constants
my $CCDS_CONF = "etc/ccds.conf";

# Get the ccds dump file name from the server
sub getDumpFileName() {
    # look for: CCDS.20080630.tar.gz
    my @ftpFiles = ftpGetDirList("/");
    my @dumpFiles;

    foreach my $dumpFile (@ftpFiles) {
        if ($dumpFile =~ /^\/CCDS\.[0-9]{8}\.tar\.gz$/) {
            push(@dumpFiles, $dumpFile);
        }
    }
    @dumpFiles = sort @dumpFiles;

    if ($#dumpFiles < 0) {
        die("can't find CCDS dump file, available files: " . join(" ", @ftpFiles));
    }
    return $dumpFiles[$#dumpFiles];
}

# Do download for the specified data dir, if it's not there.
sub doDownload($) {
    my($downloadDir) = @_;

    my $ftpDumpFile = getDumpFileName();
    my $localDumpFile = $downloadDir . "/" . basename($ftpDumpFile);
    
    if (! -e $localDumpFile) {
        if ($gbCommon::verbose) {
            prMsg("downloading CCDS dump $ftpDumpFile to $downloadDir");
        }
        makeDir($downloadDir);
        ftpGetOrCheck(0, 0, $ftpDumpFile, $localDumpFile);
        gbChmod($localDumpFile);
    } else {
        ftpGetOrCheck(1, 0, $ftpDumpFile, $localDumpFile);  # compare sizes
        if ($gbCommon::verbose) {
            prMsg("CCDS $ftpDumpFile already in $downloadDir");
        }
    }
}

# Entry point
while (($#ARGV >= 0) && ($ARGV[0] =~/^-.*/)) {
    my $opt = $ARGV[0];
    shift @ARGV;
    if ($opt eq "-verbose") {
        $gbCommon::verbose = 1;
    } elsif ($opt eq "-ftp-verbose") {
        $gbFtp::verbose = 1;
    } else {
        gbError("invalid option \"$opt\"");
    }
}

if ($#ARGV >= 0) {
    die("Wrong \# args: ccdsDownloadStep [-verbose] [-ftp-verbose]");
}

# read ftp info from file
$main::ccdsFtpHost = undef;
$main::ccdsFtpUser = undef;
$main::ccdsFtpPassword = undef;
eval(readFile($CCDS_CONF));
if (!(defined($main::ccdsFtpHost))) {
    gbError("$CCDS_CONF must \$main::ccdsFtpHost");
}
if (!(defined($main::ccdsFtpUser))) {
    gbError("$CCDS_CONF must \$main::ccdsFtpUser");
}
if (!(defined($main::ccdsFtpPassword))) {
    gbError("$CCDS_CONF must \$main::ccdsFtpPassword");
}

# use different task dir to allow running parallel with genbank
beginTask("ccdsdownload", "download");
ftpInit($main::ccdsFtpHost, $main::ccdsFtpUser, $main::ccdsFtpPassword);
ftpOpen();

my $downloadDir = "data/download/ccds";

doDownload($downloadDir);
ftpClose();

endTask();
