#!/usr/bin/perl
use strict;
use warnings;
use Socialtext::Resting::Getopt qw/get_rester/;
use Socialtext::Resting::LocalCopy;
use Getopt::Long;

sub usage {
    my $msg = shift || '';
    die <<EOT;
$msg
USAGE: $0 [--to <dir>] [--from <dir>] [--tag <tag>] <rester options>

Saves wiki content to a local directory, or from a local directory.

Either --to or --from must be specified.

--tag specifies a tag to push/pull.

EOT
}

my $r = get_rester();

my ($to, $from, @tags);
GetOptions(
    'to=s'   => \$to,
    'from=s' => \$from,
    'tag=s'  => \@tags,
) or usage;

usage unless $to or $from;
usage("$to is not a directory!\n") if $to and !-d $to;
usage("$from is not a directory!\n") if $from and !-d $from;

# if no tags are supplied, we still want to push/pull changes.
push @tags, undef unless @tags;

my $lc = Socialtext::Resting::LocalCopy->new( rester => $r );
if ($to) {
    print "Pulling content from " . $r->workspace . " into $to\n";
    for my $tag (@tags) {
        $lc->pull(dir => $to, tag => $tag);
    }
}
else {
    print "Pushing content from $from into " . $r->workspace . "\n";
    for my $tag (@tags) {
        $lc->push(dir => $from, tag => $tag);
    }
}
