package Bot::Cobalt::Frontend::RC;
$Bot::Cobalt::Frontend::RC::VERSION = '0.021003';
use strictures 2;

use Carp;

use Cwd        ();
use File::Spec ();

use Try::Tiny;

use Bot::Cobalt::Serializer;

use parent 'Exporter::Tiny';

our @EXPORT_OK = qw/

sub rc_read {
  my ($rcfile) = @_;
  croak "rc_read needs a rcfile path" unless $rcfile;

  my $generic_crappy_err = sub {
      "Errors reported during rcfile parse\n",
      "You may have an old, incompatible, or broken rcfile.\n",
      "Path: $rcfile\n",
      "Try running cobalt2-installer\n" ;

  my $rc_err;
  my $rc_h = try {
  } catch {
    $rc_err = $_;
  } or croak "Could not rc_read(); readfile said $rc_err";
  unless ($rc_h && ref $rc_h eq 'HASH') {
    croak "rc_read ($rcfile) expected to receive a hash"

  my ($BASE, $ETC, $VAR) = @$rc_h{'BASE', 'ETC', 'VAR'};
  unless ($BASE && $ETC && $VAR) {
    warn "rc_read; could not find BASE, ETC, VAR\n";
    warn "BASE: $BASE\nETC: $ETC\nVAR: $VAR\n";
    croak "Cannot continue without a valid rcfile"
  return ($BASE, $ETC, $VAR)

sub rc_write {
  my ($rcfile, $basepath) = @_;
  croak "rc_write needs rc file path and base directory path"
    unless $rcfile and $basepath;

  unless ( File::Spec->file_name_is_absolute($basepath) ) {
    my $homedir = $ENV{HOME} || Cwd::cwd();
    $basepath = File::Spec->catdir( $homedir, $basepath );

  my $rc_h = {
    BASE => $basepath,
    ETC  => File::Spec->catdir( $basepath, 'etc' ),
    VAR  => File::Spec->catdir( $basepath, 'var' ),

    $rcfile, $rc_h




=head1 NAME

Bot::Cobalt::Frontend::RC - Read and write instance RC files


  use Bot::Cobalt::Frontend::RC qw/ rc_read rc_write /;
  my ($base, $etc, $var) = rc_read($rcfile_path);
  rc_write($rcfile_path, $base_path);


L<Bot::Cobalt> RC files are small per-instance YAML configuration files 
used by the default frontends to determine the location of configuration 
('etc') and dynamic data ('var') directories.

An example RC file might look like:

  BASE: /home/cobalt/cobalt2
  ETC: /home/cobalt/cobalt2/etc
  VAR: /home/cobalt/cobalt2/var

=head2 rc_read

rc_read() takes the path to a preexisting RC file and returns a list of 
three elements; the base directory path, 'etc' path, and 'var' path, 

=head2 rc_write

rc_write() takes the path to the destination RC file and a base 
directory path from which 'etc' and 'var' directories are constructed.

Returns the actual (parsed) base path on success, croaks on failure.

=head1 AUTHOR

Jon Portnoy <>