#!/usr/bin/perl
# get information about current site(s) configuration
# run as ansible module
# gathering facts about sites
# options:
# "name=list password=0 status=finished type=DBName site=default"
# dbname=sitemanager0
use strict;
use warnings;

use lib "/opt/webdir/lib";
use Output;
use Pool;
use bxSites;
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 $o_action  = "status";          # type of action that script must do.
                                   # status  - information about site(s) that live on this system
                                   # list    - list all sites and its info
                                   # cluster_test - test cluster and scale modules for sites
my $o_site    = undef;             # site short name
my $o_hiden   = 0;                 # hiden or not password value
my $o_request = 'all';             # output only for defined attribute
my $o_dbname  = undef;
my $o_status  = undef;
my $o_install = undef;
my $o_site_dir = undef;
my $o_site_error = undef;

# 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 ( $opt =~ /^name=(\S+)$/ ){      $o_action = $1};
      if ( $opt =~ /^site=(\S+)$/ ){      $o_site = $1};
      if ( $opt =~ /^password=(\S+)$/ ){  $o_hiden = $1 };
      if ( $opt =~ /^type=(\S+)$/ ){      $o_request = $1};
      if ( $opt =~ /^status=(\S+)$/ ){    $o_status = $1};
      if ( $opt =~ /^dbname=(\S+)$/ ){    $o_dbname = $1};
      if ( $opt =~ /^install=(\S+)$/ ){   $o_install = $1};
      if ( $opt =~ /^site_dir=(\S+)$/ ) { $o_site_dir = $1};
      if ( $opt =~ /^error=(\S+)$/ ) { $o_site_error = $1};
    }
    close $oh;
  }
}

# process request
my $sites = undef;
if ($o_action =~ /^list$/){
  my $filters = {};
  if(defined $o_site && $o_site !~ /^NOT_DEFINED$/){
    $filters->{'SiteName'} = $o_site;
  }
  if(defined $o_status && $o_status !~ /^NOT_DEFINED$/){
    $filters->{'SiteStatus'} = $o_status;
  }
  if(defined $o_dbname && $o_dbname !~ /^NOT_DEFINED$/){
    $filters->{'DBName'} = $o_dbname;
  }
  if(defined $o_install && $o_install !~ /^NOT_DEFINED$/){
    $filters->{'SiteInstall'} = $o_install;
  }
  if(defined $o_site_dir && $o_site_dir !~ /^NOT_DEFINED$/){
    $filters->{'DocumentRoot'} = $o_site_dir;
  }
  if(defined $o_site_error){
    $filters->{'error'} = $o_site_error;
  }

  my $bx = bxSites->new(filters=>$filters);
  $sites = $bx->listSite();
}elsif ($o_action =~ /^status$/){
  my $bx = bxSite->new(
    site_name => $o_site,
    site_dir  => $o_site_dir,
  );
  $sites = $bx->statusSite();
}elsif ($o_action =~ /^cluster_test$/){
  my $bx = bxSites->new();
  $sites = $bx->testClusterConfig();

}else{
  $sites = Output->new( error => 1, message => "Unknown action option. PLease use -h for help message." );
}

print $sites->printAnsible($o_request,$o_hiden,$o_status);

