#!/usr/bin/env perl

use 5.006002;

use strict;
use warnings;

use Getopt::Long 2.33 qw{ :config auto_version };
use Astro::SpaceTrack;
use Pod::Usage;

our $VERSION = '0.180';

my %opt;

GetOptions( \%opt,
    qw{ basic! hide! },
    help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

if ( defined $opt{hide} ) {
    hide( $opt{hide} );
    exit;
}

my $st;
eval {
    # Default identity to true
    local $ENV{SPACETRACK_IDENTITY} = 1;
    $st = Astro::SpaceTrack->new();
} or die "Unable to instantiate Astro::SpaceTrack\n";

my %info;
foreach my $key ( qw{ username password } ) {
    defined( $info{$key} = $st->getv( $key ) )
	or die "Unable to determine $key\n";
}

my $delim = $opt{basic} ? ':' : '/';

print "SPACETRACK_USER=$info{username}$delim$info{password}\n";

sub hide {
    my ( $hide ) = @_;
    my $id_file = Astro::SpaceTrack->__identity_file_name();
    my $hidden_file = "$id_file-hidden";
    my ( $from, $to, $name ) = $hide ? ( $id_file, $hidden_file, 'ID' ) : (
	$hidden_file, $id_file, 'Hidden' );
    -f $from
	or die "$name file $from not found\n";
    rename $from, $to
	or die "Failed to rename $name file $from to $to: $!\n";
    warn "$name file $from renamed to $to\n";
}
__END__

=head1 TITLE

spacetrack-identity - Report the user's Space Track identity

=head1 SYNOPSIS

 spacetrack-identity
 spacetrack-identity -help
 spacetrack-identity -version

or, if you have any of a number of Unix shells,

 export eval `spacetrack-identity`

=head1 OPTIONS

=head2 -basic

If asserted, this option causes the output to be punctuated with a colon
(C<':'>, think HTTP basic authentication) rather then a slash (C<'/'>,
think Oracle). Both are understood by
L<Astro::SpaceTrack|Astro::SpaceTrack>.

The default is C<-nobasic>.

=head2 -help

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

=head2 -hide

If this Boolean option is asserted, the identity file, if any, is
renamed so that it will not be found by
L<Astro::SpaceTrack|Astro::SpaceTrack>.

If it is negated (as C<-nohide>) it will be renamed back.

Either way, the script then exits.

=head2 -version

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

=head1 DETAILS

This Perl script reports the user's Space Track identity. On success,
the output is

 SPACETRACK_USER=username/password

unless C<-basic> is specified, in which case you get a colon (C<':'>)
rather than a slash (C<'/'>).

On failure, the script dies.

=head1 AUTHOR

Tom Wyant (wyant at cpan dot org)

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2016-2026 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 files F<LICENSE-Artistic> and F<LICENSE-GPL>.

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 :
