#!perl

our $DATE = '2019-07-26'; # DATE
our $VERSION = '0.009'; # VERSION

use 5.010001;
use strict;
use warnings;
use Log::ger;

use App::sshwrap::hostcolor qw(get_history_entry add_history_entry);
use AppLib::sshwrap;
use Getopt::Long qw(:config bundling no_ignore_case);
use XTerm::Util qw(get_term_bgcolor set_term_bgcolor);

my $userhost;
sub parse_cmdline {
    local @ARGV = @ARGV;
    local $SIG{__WARN__} = sub {};

    my %go_spec = map { $_ => sub {} } keys %AppLib::sshwrap::optspec;
    delete $go_spec{'{arg}'};
    $go_spec{'<>'} = sub {
        $userhost //= $_[0];
        #log_trace "Setting userhost to %s", $userhost;
    };
    GetOptions(%go_spec);
}

my $origcolor;
my $env;
sub init {
    parse_cmdline();
    $origcolor = get_term_bgcolor();
    $env = $ENV{SSHWRAP_HOSTCOLOR_AUTO} // '';
}

sub set_hostcolor {
    return unless defined $userhost;
    my $color = get_history_entry($userhost);
    if (defined $color) {
        log_info "Using history color '$color' for $userhost";
    } elsif ($env eq 'random-dark') {
        require Color::RGB::Util;
        $color = Color::RGB::Util::rand_rgb_color('000000', '3f3f3f');
        log_info "Assigning random dark color '$color' for $userhost";
    } elsif ($env eq 'random-light') {
        require Color::RGB::Util;
        $color = Color::RGB::Util::rand_rgb_color('c0c0c0', 'ffffff');
        log_info "Assigning random light color '$color' for $userhost";
    }
    set_term_bgcolor($color) if defined $color;
}

sub get_hostcolor {
    return unless defined $userhost;

    my $color = get_term_bgcolor();
    if ($color) {
        log_info "Remembering host color $userhost -> $color";
        add_history_entry($userhost, $color);
    }
    if ($origcolor) {
        set_term_bgcolor($origcolor);
    }
}

my $exitcode;
sub run_ssh {
    my @cmd = ('ssh', @ARGV);
    log_info "Executing ssh: %s", \@cmd;
    system {$cmd[0]} @cmd;
    $exitcode = $?;
}

### main

log_info "start";
init;
set_hostcolor;
run_ssh;
get_hostcolor;
log_info "end";
exit $exitcode;

# ABSTRACT: SSH wrapper script to remember the terminal background you use for each host
# PODNAME: sshwrap-hostcolor

__END__

=pod

=encoding UTF-8

=head1 NAME

sshwrap-hostcolor - SSH wrapper script to remember the terminal background you use for each host

=head1 VERSION

This document describes version 0.009 of sshwrap-hostcolor (from Perl distribution App-sshwrap-hostcolor), released on 2019-07-26.

=head1 SYNOPSIS

Use like you would B<ssh>:

 % sshwrap-hostcolor -v ujang@host1.example

For convenience, alias this script to B<ssh>. In your Unix shell:

 alias ssh=sshwrap-hostcolor

then you can use it like this:

 % ssh -v ujang@host1.example

After you ssh to the remote host, change the background color of your terminal
(e.g. to dark blue). Upon exit, the script will retrieve the terminal's current
background color and record this line to F<~/.sshwrap-hostcolor.history>:

 ujang@host1.example     00002b

Next time you ssh to the same C<user@host> combination, the script will set the
terminal's background color to dark blue.

As a bonus, you can also activate the completion script L<_sshwrap-hostcolor>:

 complete -C _sshwrap-hostcolor ssh

this will let you complete C<user@host> and immediately change the background
color to the remembered one.

And as another bonus, if you set C<SSHWRAP_HOSTCOLOR_AUTO> to C<random-dark> (or
C<random-light>), the script will automatically assign a random color for each
unrecognized user+host.

=head1 DESCRIPTION

=head1 FAQ

=head2 The script doesn't work!

To get and set terminal background color programmatically, an xterm-compatible
terminal is required. Please see the list here: L<XTerm::Util>.

=head1 ENVIRONMENT

=head2 SSHWRAP_HOSTCOLOR_AUTO

String. Can be set to C<random-dark> to automatically set the color of a new
user+host with a random color between "000000" and "3f3f3f". Can be set
toC<random-light> to automatically set the color of a new user+host with a
random color between "c0c0c0" and "ffffff".

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-sshwrap-hostcolor>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-sshwrap-hostcolor>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-sshwrap-hostcolor>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

B<ssh>

Some video demonstrations: L<one|https://www.youtube.com/watch?v=Upz1ZtiJk1Y>,
L<two|https://www.youtube.com/watch?v=IyJKea6Sv8U>,
L<three|https://www.youtube.com/watch?v=Upz1ZtiJk1Y>.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2019, 2018 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
