#!/usr/bin/perl
use strict;
use warnings;
use Socialtext::EditPage;
use Getopt::Long;

my %opts;
my %edit_opts;
GetOptions(
    'username|u=s' => \$opts{username},
    'password|p=s' => \$opts{password},
    'server|s=s'   => \$opts{server},
    'workspace|w=s'=> \$opts{workspace},
    'pull-includes'=> \$opts{pull_includes},
    'latest-tag=s' => \$opts{latest_tag},
    'o|output=s'   => \$edit_opts{output},
    'template=s'   => \$edit_opts{template},
) or usage();

my $page = shift;
my $edit = Socialtext::EditPage->new(%opts);
if ($page) {
    $edit->edit_page( page => $page, %edit_opts );
}
elsif ($opts{latest_tag}) {
    $edit->edit_last_page( tag => $opts{latest_tag}, %edit_opts );
}
else {
    usage();
}

exit;


sub usage {
    my $rc_file = $Socialtext::Resting::DefaultRester::CONFIG_FILE;
    die <<EOT;
$0 page_name

Required options:
 --username      Specify the username to connect with
 --password      Specify the password to connect with
 --server        Which Socialtext server to connect to
 --workspace     Which workspace to post to

Other options:
 --pull-includes Inline content from {include} wafls in the 
                 page and then extraclude it when saving
 --latest-tag    Opens the newest page tagged with <tag>
 --template=foo  Uses content from page 'foo' as a template
                 for new pages.

Config:
Put the above options into $rc_file like this:

  username = some_user\@foobar.com
  password = your_pass
  workpace = corp
  server   = https://www2.socialtext.net/
EOT
}


