#! /usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;
use App::Xssh;

sub getTerminalOptions {
  my ($xssh,$host) = @_;

  my $config = $xssh->readConfig();

  my $options = $config->{hosts}->{DEFAULT} || {};

  if ( my $details = $config->{hosts}->{$host} ) {
    $options = { %$options, %{$config->{hosts}->{$host}} };
  }

  if ( my $extra = delete $options->{extra} ) {
    if ( my $details = $config->{extra}->{$extra} ) {
      $options = { %$options, %{$config->{extra}->{$extra}} };
    }
  }

  $options->{host} = $host;
  return($options);
}

sub launchTerminal {
  my ($options) = @_;

  my $type = $options->{type} || "XTerm";
  my $class = "X11::Terminal::$type";
  eval "require $class";
  my $term = $class->new(%$options);
  $term->launch();
}

sub showConfig {
  my ($xssh) = @_;

  my $data = $xssh->readConfig();

  my $configString = "";
  for my $type ( reverse sort keys %{$data} ) {
    for my $entity ( sort keys %{$data->{$type}} ) {
      for my $key ( sort keys %{$data->{$type}->{$entity}} ) {
        my $value =  $data->{$type}->{$entity}->{$key};
        my $output = sprintf("%6s\t%20s\t%10s: %s\n", $type,$entity,$key,$value);
        $configString = $configString . $output;
      }
    }
  }
  return $configString;
}
sub main {
  my $sethost;
  my $setextra;
  my $showconfig;
  GetOptions(
    'sethostattr' => \$sethost,
    'setextraattr' => \$setextra,
    'showconfig' => \$showconfig,
  ) or pod2usage(1);
  
  my $xssh = App::Xssh->new();
  if ( $sethost ) {
    my ($name,$attr,$value) = @ARGV;
    if ( ! ($name && $attr && $value) ) {
      pod2usage(1);
    }
    $xssh->addToConfig(["hosts",$name,$attr],$value);
    return $xssh->saveConfig();
  }
  if ( $setextra ) {
    my ($name,$attr,$value) = @ARGV;
    if ( ! ($name && $attr && $value) ) {
      pod2usage(1);
    }
    $xssh->addToConfig(["extra",$name,$attr],$value);
    return $xssh->saveConfig();
  }
  if ( $showconfig ) {
    print showConfig($xssh);
  }

  if ( my ($host) = @ARGV ) {
    my $options = getTerminalOptions($xssh,$host);
    launchTerminal($options);;
  }
}

main() unless caller;
1;

__END__

=head1 NAME

xssh - A customisable X terminal emulator

=head1 SYNOPSIS

	xssh HOST
	xssh --sethostattr HOST ATTRIBUTE VALUE
	xssh --setextraattr NAME ATTRIBUTE VALUE
	xssh --showconfig

=head1 DESCRIPTION

This program will allow you to ssh to a remote host inside a new X Terminal window (like XTerm).  

The idea is that you can configure it in such a way that every time you connect to a particular host, the terminal window will be customised in the same manner.  For example, you may wish to configure 'prodserver' so that it the terminal window background is a redish colour - reminding you to tread very carefully...

=head1 EXAMPLES

First, you may like to define some defualts that will apply to all the remote
servers you might log in to.

	xssh --sethostattr DEFAULT scrollback 1024
	xssh --sethostattr DEFAULT foreground blue
	xssh --sethostattr DEFAULT background white

Then you may like to override those defaults for some specific hosts.

	xssh --sethostattr favourite foreground green
	xssh --sethostattr favourite background black
	xssh --sethostattr favourite geometry 128x70

You may then get bored with setting all attributes for all hosts, so you
might want to set up some "extra" attributes.

	xssh --setextraattr prod foreground black
	xssh --setextraattr prod background IndianRed

You would then define which hosts take those extra attributes.

	xssh --sethostattr important extra prod

FINALLY, you can use xssh to log in to those servers

	# Log in to the server "important" - you'll get a new xterm
	# window with a redish background, black text, 1024 lines of
	# scrollback
	xssh important
		
	# Log in to the server "unknown" - you'll get a new xterm window
	# with a # white background, blue text, 1024 lines of scrollback
	xssh unknown
	
	# Log in to the server "favourite" - you'll get a new xterm
	# window with a black background, green text, 1024 lines of
	# scrollback, and a larger terminal
	xssh favourite

=head1 FILES

=over

=item F<$HOME/.xsshrc>

This file contains all the configuration for xssh.  It is in L<Config::General> format.

=back

=head1 COPYRIGHT & LICENSE

Copyright 2010 Evan Giles <egiles@capn.org>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
