#!/usr/bin/env perl

use v5.40;
use feature 'signatures';

use File::Basename qw(basename);
use Getopt::Long qw(GetOptions);
use TOON;

my $me = basename($0);

my $pretty    = 0;
my $canonical = 0;
my $help      = 0;
my $version   = 0;

GetOptions(
  'pretty!'    => \$pretty,
  'canonical!' => \$canonical,
  'help!'      => \$help,
  'version|v'  => \$version,
) or usage();

usage() if $help;

if ($version) {
  say "$me version $TOON::VERSION";
  exit;
}

local $/;
my $input = <>;
my $toon  = TOON->new(
  pretty    => $pretty,
  canonical => $canonical,
);

my $data = $toon->decode($input);
print $toon->encode($data), "\n";

sub usage {
  print <<"USAGE";
Usage: $me [--pretty] [--canonical] [--version] [file ...]

Reads TOON from STDIN or files and writes normalised TOON to STDOUT.
USAGE
  exit;
}

__END__

=head1 NAME

toon_pp - read and normalise TOON data

=head1 SYNOPSIS

  toon_pp [--pretty] [--canonical] [--version] [file ...]

=head1 DESCRIPTION

C<toon_pp> reads TOON (Token-Oriented Object Notation) from standard
input or from one or more files named on the command line, parses it,
and writes the normalised TOON representation to standard output.

=head1 OPTIONS

=over 4

=item --pretty

Format the output with newlines and indentation to make it easier to
read. By default the output is compact.

=item --canonical

Sort object keys alphabetically in the output.

=item --help

Display a brief usage message and exit.

=item --version

=item -v

Display the version number and exit.

=back

=head1 AUTHOR

Dave Cross <dave@perlhacks.com>

=cut
