#!/usr/bin/perl
# manage ansible configs for host
# hosts - add|remove host from group
# host_vars - add|remove|modify variables in personal host file
# group_vars/group add|remove|modify variables in group file
# ex.
# - update host, add it to group mysql and set var1 and var2
#   hostname=vm1 group=mysql state=present|absent|update var1=val1 var2=val1 ... 
# - update group, set val1 and val2
#   group=mysql state=update var1=val1 var2=val2
#
# state:
# add     - create record in the hosts file and update|create personal file
# remove  - remove record in the hosts file ( if group not defined => from all group )
# update  - update host variables in personal file
use strict;
use warnings;

use lib "/opt/webdir/lib";
use Output;
use Pool;
use Host;
use Data::Dumper;
use Getopt::Long;
use File::Basename qw( dirname basename );

# program options
my $prog_name = basename $0;
my $prog_dir  = dirname $0;

my %options = (
  hostname  => undef,
  group     => undef,
  state     => 'update',
);
my @options = keys %options;

# get command line options
my $opt_file = $ARGV[0];

# if change opts via module options
if ( $opt_file ){
  open (my $oh, $opt_file) or die "Cannot open $opt_file: $!";
  my $lines = <$oh>;
  if (defined $lines){
    my @opts = split(/\s+/, $lines);
    foreach my $opt (@opts){
      # if we need update option from file
      if ($opt =~ /^([^=]+)=(\S+)$/){
        my $key = $1;
        my $val = $2;
        $val =~ s/^['"]//;
        $val =~ s/['"]$//;

        $options{$key} = $val;
      # if we need delete option from file
      }elsif($opt =~ /^([^=]+)$/){
        $options{$1} = undef;
      }
    }
    close $oh;
  }
}

# test options
my $bx_return = undef;
# update hosts info
if ( $options{'hostname'} ){
  if ( $options{'state'} =~ /^(update|add)$/ ){
    my $bx_host = Host->new(host => $options{'hostname'});
    $bx_return = $bx_host->updateHost(\%options);
  }elsif ($options{'state'} =~ /^create$/ ){
    if ( not defined $options{'ip'} ){
      $bx_return = Output->new(
        error => 1,
        message => "ip= must be set for state=".$options{'state'},
      );
    }else{
      #print "hostname=",$options{'hostname'}," ip=",$options{'ip'},"\n";
      my $bx_host = Host->new(host => $options{'hostname'}, ip => $options{'ip'});
      #print Dumper($bx_host);
      $bx_return = $bx_host->updateHost(\%options);
    }
  }elsif ( $options{'state'} =~ /^(delete|remove|absent)$/ ){
    my $bx_host = Host->new(host => $options{'hostname'});
    $bx_return = $bx_host->deleteHost(\%options);
  }else{
    $bx_return = Output->new(
      error => 1,
      message => "Unknown value for option state=".$options{'state'}
    );
  }
# update group info
}elsif ( !$options{'hostname'} && $options{'group'} ){
  my $bx_pool = Pool->new();
  $bx_return = $bx_pool->update_group_vars(\%options);
}else{
  $bx_return = Output->new(
    error => 1,
    message => "hostname= or group= must be set for state=".$options{'state'}
  );
}

print $bx_return->printAnsible;

