The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/local/bin/perl

use 5.006002;

use strict;
use warnings;

use Astro::Coord::ECI::TLE;
use Getopt::Long 2.33;
use Pod::Usage;

our $VERSION = '0.082';

my %opt;

GetOptions( \%opt,
    'tle!' => sub { return _set_json_opt( $_[1], 0 ) },
    'json!' => sub { return _set_json_opt( $_[1], 1 ) },
    help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

# Slurp all the input.

my $data;
{
    local $/ = undef;
    $data = <>;
}

# If -json or -tle have not been seen (or were wiped out by -nojson or
# -notle), choose a value based on the data.

exists $opt{json}
    or $opt{json} = $data !~ m/ \A \s* [[]? \s* [{] /smx;

# Parse the data into TLE objects.

my @tle = Astro::Coord::ECI::TLE->parse( $data );

if ( $opt{json} ) {

    # If JSON output is required, load the JSON module and spit out the
    # data in human-readable form.

    require JSON;
    print JSON->new()->pretty()->canonical()->utf8()->convert_blessed()
	->encode( \@tle );

} else {

    # If JSON output is not required, then we do TLE output.

    foreach my $body ( @tle ) {
	print $body->get( 'tle' );
    }
}

# Helper subroutine for GetOptions. The arguments are the option value
# found on the command line, and the value to set the {json} option to
# if the command option was true. If the command option is false, we
# delete the {json} option, as though it was never seen.

sub _set_json_opt {
    my ( $opt_val, $set_val ) = @_;
    if ( $opt_val ) {
	$opt{json} = $set_val;
    } else {
	delete $opt{json};
    }
    return;
}

__END__

=head1 TITLE

convert_tle - Convert TLE data to/from JSON

=head1 SYNOPSIS

 convert_tle data.tle
 convert_tle -help
 convert_tle -version

=head1 OPTIONS

=head2 -help

This option displays the documentation for this script. The script then
exits.

=head2 -json

This option requires the output to be in JSON format. C<-nojson>
restores the default.

=head2 -tle

This option requires the output to be in TLE format. C<-notle> restores
the default.

=head2 -version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script reads TLE data, and converts it from JSON to TLE
format, or vice versa. The JSON data is similar to (read: 'uses the same
keys as') the data provided by the Space Track REST interface. Input is
from files named on the command line, or from standard in. Output is to
standard out.

By default, JSON is converted to TLE, and TLE to JSON. Input that begins
with C<'{'> or C<'[{'> is assumed to be JSON; anything else is assumed
to be TLE.

You can force output to be TLE or JSON using the C<-tle> or C<-json>
options. If both are specified, the rightmost takes precedence. Negating
either option (as C<-notle> or C<-nojson>) has the effect of restoring
the default.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012-2017 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :