#!perl

use 5.10.1;
use strictures 2;

use File::Copy;
use File::Spec;

use Bot::Cobalt::Frontend::RC qw/rc_read/;
use Bot::Cobalt::Frontend::Utils qw/
  ask_yesno
  ask_question
/;

my($path_dest, $this_plugin);
my $path_rcfile = $ENV{HOME} ?
  File::Spec->catfile( $ENV{HOME}, ".cobalt2rc" )
  : ".cobalt2rc";

use Getopt::Long;
GetOptions(
  help => sub {
    print(
      "cobalt2-plugin-installcf\n\n",
      "  --plugin=DIST\n\n",
      "  --dest=PATH\n",
      "   OR:\n",
      "  --rcfile=FILE\n",
    );
    
    exit 0
  },
  
  'plugin=s'   => \$this_plugin,
  
  'rcfile=s'   => \$path_rcfile,

  'dest=s' => \$path_dest,
);

$this_plugin = $ARGV[0] unless $this_plugin;
unless ($this_plugin) {
  die "No plugin specified.\n";
}

## set up path_dest
unless ($path_dest) {
  my ($base, $etc) = rc_read($path_rcfile);

  print "Current etcdir: $etc\n",
        "No path was specified\n",
        "Attempting to write one under etcdir\n";
  
  $path_dest = ask_question(
    prompt => "Write file (under etc/plugins/)",
  );
}

write_conf( try_load_cf($this_plugin) );
print(
  "Config for $this_plugin\n",
  "Written to: $path_dest\n",
);



sub try_load_cf {
  my ($plugin) = @_;

  my $plugincf = $plugin . '::Conf' ;

  eval "require $plugincf";
  return if $@;

  die "No conf() method found for $plugincf\n"
    unless $plugincf->can('conf');

  return $plugincf->conf() 
   || die "$plugincf did not return a true value"
}

sub write_conf {
  my ($thiscf) = @_;
  
  die "write_conf not passed a conf, is the plugin name valid?" 
    unless $thiscf;

  if (-e $path_dest) {
    print "Warning! The destination file appears to exist.\n";
    print "Path: $path_dest\n";
    my $overwrite = ask_yesno(
      prompt  => "Overwrite destination path?",
      default => "n",
    );
    
    die "Exiting, destination exists.\n"
      unless $overwrite;
  }

  open my $fh, '>', $path_dest
    or die "open: $path_dest: $!";
  print $fh $thiscf;
  close $fh;
  return 1
}


## FIXME offer to try appending to plugins.conf ?
##  -> copy to .bak

__END__

=pod

=head1 NAME

cobalt2-plugin-installcf - Install example plugin confs

=head1 SYNOPSIS

  $ cobalt2-plugin-installcf --plugin=<distname> --dest=<filename>
  
=head1 DESCRIPTION

This is a simple tool for installing example plugin configuration files 
packaged with a plugin distribution.

For example:

  $ cpan Bot::Cobalt::Plugin::RSS
  $ cobalt2-plugin-installcf --plugin="Bot::Cobalt::Plugin::RSS" \
      --dest="cobalt2/etc/plugins/rss.conf"

Plugin authors should see L<Bot::Cobalt::Manual::Plugins::Dist> for 
details on including an example plugin configuration file.

=head1 AUTHOR

Jon Portnoy <avenj@cobaltirc.org>

L<http://www.cobaltirc.org>

=cut