#!/usr/bin/perl
my $usage =
    "gbCopyDownload [options] buildDownloadDir downloadServer downloadDir\n" .
    "\n" .
    "rsync download files from the build server to local directory.\n" .
    "Must be run in local gbRoot directory.\n" .
    "\n" .
    "Arguments:`\n" .
    "  - buildDownloadDir - download directory on this server\n" .
    "  - downloadServer - download server to rsync the files to.\n" .
    "  - downloadDir - rsync to this directory\n" .
    "Options:\n" .
    "  -rsyncPort=port - rsync port to use, or `rsh' or `ssh'. Defaults to rsh.\n" .
    "  -verbose\n";

use strict;
use warnings;
use File::Basename;
use FindBin;
use lib "$FindBin::Bin/../lib";
use gbCommon;

# Entry
my $rsyncPort = "rsh";
my $rsyncVerb = "";
while (($#ARGV >= 0) && ($ARGV[0] =~ /^-.*/)) {
    my $opt = $ARGV[0];
    shift @ARGV;
    if ($opt =~ /^-verbose=/) {
        $gbCommon::verbose = 1;
    } elsif ($opt eq "-verbose") {
        $gbCommon::verbose = 1;
        $rsyncVerb = "-vv";
    } elsif ($opt =~ /^-rsyncPort=/) {
        $rsyncPort = parseOptEq($opt);
    } else {
        gbError("invalid option \"$opt\"");
    }
}
if ($#ARGV != 2) {
    gbError("wrong # args: $usage");
}
my($buildDownloadDir, $downloadServer, $downloadDir) = @ARGV;

beginTask("downloadcp/$downloadServer", "downloadcp");

# trailing backslash required on source
runProg("rsync $rsyncVerb -e $rsyncPort --whole-file -rl --include='*.fa.gz' --times --omit-dir-times --include='*.maf.gz' --exclude='*.tmp*' ${buildDownloadDir}/ ${downloadServer}:${downloadDir}");

endTask();
