#!/usr/bin/perl
use 5.006;
use strict;
use warnings;
our $VERSION = '0.003001';

use Getopt::Long;
use JSON;
use Metabase::User::Profile;
use Metabase::User::Secret;
use Pod::Usage;

my (%profile, $help, $output, $full_name, $email_address, $password);
my $result = GetOptions(
  'help|h'      => \$help,
  'output|o:s'  => \$output,
  'name:s'      => \$full_name,
  'email:s'     => \$email_address,
  'secret:s'    => \$password,
);

pod2usage({-verbose => 2}) if ! $result || $help;

# setup output file and confirm it doesn't exist
if ( ! defined $output ) {
  $output = "metabase_id.json";
}
if ( -f $output ) {
  die "Won't over-write existing '$output' file.  Aborting.\n";
}

# get profile information
$profile{full_name}     = $full_name      if defined $full_name;
$profile{email_address} = $email_address  if defined $email_address;
$profile{password}      = $password       if defined $password;

my @prompts = (
  full_name     => 'full name',
  email_address => 'email address',
  password      => 'password/secret',
);

$|++; # autoflush prompts
while (@prompts) {
  my ($key, $phrase) = splice(@prompts,0,2);
  next if $profile{$key};
  print "Enter $phrase\: ";
  chomp( my $answer = <STDIN> );
  $profile{$key} = $answer;
}

# create profile and secret objects
$password = delete $profile{password};
my $profile = Metabase::User::Profile->create( %profile );
my $secret = Metabase::User::Secret->new(
  resource => $profile->resource,
  content  => $password,
);

# write output
print "Writing profile to '$output'\n";
open my $fh, ">", $output;
print {$fh} JSON->new->pretty->encode([
  $profile->as_struct,
  $secret->as_struct,
]);
close $fh;
chmod 0600, $output;

__END__

=head1 NAME

metabase-profile - create a metabase profile

=head1 SYNOPSIS

  $ metabase-profile
  Enter full name: John Doe
  Enter email address: jdoe@example.com
  Enter password/secret: zqxjkh
  Writing profile to 'metabase_id.json'

=head1 USAGE

The metabase-profile program makes it easy to create a user profile for
submitting facts and reports to a Metabase server.

Valid options include:

      --email   ADDRESS   user email address eg "jd@example.com"
      --name    FULLNAME  full user name, eg "John Doe"
  -o, --output  FILENAME  output filename
      --secret  PASSWORD  password for authentication
  -h, --help              print man page

If no output file name is given, the default name 'metabase_id.json' will be
used.  If the the output filename (or default) exists, the program will abort
rather than overwrite the file.  The output file will be in JSON and contain
the user profile and the user's shared secret.

Typically, when a Metabase server first receives a report from a new user
profile, the shared secret is recorded and will be used to authenticate
subsequent submissions.  The output should not be shared publicly or
made group or world readable.

Use the resulting file according to the instructions of your Metabase
client program. You may wish to copy it across computers if you would like
to be identified consistently when submitting reports from different
locations.

=head1 AUTHOR

=over

=item * David A. Golden (DAGOLDEN)

=back

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2009-2010 by David A. Golden

Licensed under the same terms as Perl itself (the "License").
You may not use this file except in compliance with the License.
A copy of the License was distributed with this file or you may obtain a
copy of the License from http://dev.perl.org/licenses/

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

