# Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
package Agent;

use Logging;
use XmlNode;
use Transformer;

use AgentConfig;
use Dumper; # used for getCpanelVersion only
use ContentDumper;
use Storage::Storage;


my $agentWorkDir;

#
# Agent.pm module implements Agent interface
#

my $contentDumper = ContentDumper->new( Storage::Storage::createFileStorage($do_gzip, getWorkDir()) );
#
# Begin interface methods
#
sub setWorkDir {
  my $workDir = shift;
  $agentWorkDir = $workDir;
  return $agentWorkDir;
}

sub setShallowDumpMode {
  Transformer::setShallowDumpMode();
}

sub setConvertSubdomainMode {
  my $mode = shift;
  Transformer::setConvertSubdomainMode($mode);
}

sub setConvertAliasMode {
  my $mode = shift;
  Transformer::setConvertAliasMode($mode);
}

sub getContentTransportDescription{
  return $contentDumper->getContentTransportDescription();
}

sub getWorkDir {
  return $agentWorkDir || AgentConfig::cwd();;
}

sub getAgentName {
  return 'cPanel';
}

sub getAgentVersion {
  return '10.2.0';
}

sub getAgentStatus {
  my $statusNode = XmlNode->new('agent-status');

  my $wrongPlatformItem;
  if ( !defined AgentConfig::iconvBin() ) {
    $item = XmlNode->new( 'wrong-platform' );
    $item->setText( 'no iconv found on the source host' );
  }
  elsif ( !defined Dumper::getCpanelVersion() ) {
    $item = XmlNode->new( 'wrong-platform' );
    $item->setText( '' );
  }

  $statusNode->addChild($item) if defined $wrongPlatformItem;
  return $statusNode;
}

sub getResellers {
  # resellers migration is not supported now

  # should return an array of reseller identifiers, that could be a number, name or guid. Should be unique through migration dump.
  return Transformer::getResellers();
}

sub getClients {
  my $owner = shift; # reseller that owns the clients returned. Could be 'undef' for default reseller ('root' or 'admin')

  # clients migration is not supported now

  # should return an array of clients identifiers, that could be a number, name or guid. Should be unique through migration dump.
  return Transformer::getClients($owner);
}

sub getAccountEmail {
  my $accountId = shift;

  return Dumper::getAccountEmail($accountId);
}

sub getAccountSuspensionStatus {
  my $accountId = shift;
  my $suspensionStatusFile = "/var/cpanel/suspendinfo/$accountId";
  Logging::debug("Retrieving client suspension status from $suspensionStatusFile");
  my $status = -f $suspensionStatusFile ? 'disabled':'enabled';
  return $status;
}

sub getDomains {
  my $owner = shift; # reseller or client that owns the domains returned. Could be 'undef' for default reseller or client ('root' or 'admin')

  # Should return an array of clients identifiers, that could be a number, name or guid. Should be unique through migration dump.
  return Transformer::getDomains($owner);
}

sub getDomain {
  my ($id, $recursive, $content, $do_gzip, $contentOptions) = @_; # domain identifier from 'getDomains' result, flag to return node with children (for optimization purporses), flag to dump content, flag to compress content, content options

  my $clientNode = Transformer::getClientNode4Domain( $id );

  my $myDomainNode;

  if ( $clientNode ) {
    my $domainsNode = $clientNode->getChild( 'domains' );
    if ( defined $domainsNode ) {
      my @domainNodes = $domainsNode->getChildren( 'domain' );
      foreach my $domainNode (@domainNodes) {
        if ($domainNode->getAttribute( 'name' ) eq $id ) {
          $myDomainNode = $domainNode;
          last;
        }
      }

      if ( defined $myDomainNode ) {
        if( $recursive ) {
            return $myDomainNode;
        }
        return $myDomainNode->copy('no_child');
      }
    }
  }
  return;
}

sub _getDomainChild {
  my $id = shift; # domain identifier from 'getDomains' result
  my $child = shift;

  my $domainNode = getDomain( $id, 'recursive');
  return unless ( defined ( $domainNode ) ); 

  my $childNode = $domainNode->getChild( $child );
  return unless ( defined ( $childNode ) ); 
  
  return $childNode->copy();
}

sub getDomainContent {
  return;
}

sub getDomainPreferences {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'preferences');
  return $childNode;
}

sub getDomainProperties {
  _getDomainChild( shift, 'properties');
}

sub getDomainLimitsAndPermissions {
  _getDomainChild( shift, 'limits-and-permissions');
}

sub getDomainMailsystem {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'mailsystem');
  return $childNode;
}

sub getDomainDatabases {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'databases');
  return $childNode;
}

sub getDomainOdbcDsn {
  return;
}

sub getDomainColdfusionDsn {
  return;
}

sub getDomainMaillists {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'maillists');
  return $childNode;
}

sub getDomainTraffic {
  _getDomainChild( shift, 'traffic');
}

sub getDomainCertificates {
  _getDomainChild( shift, 'certificates');
}

sub getDomainTomcat {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'tomcat');
  return $childNode;
}

sub getDomainDomainuser {
  _getDomainChild( shift, 'domainuser');
}

sub getDomainShosting {
  _getDomainChild( shift, 'shosting');
}

sub getDomainFhosting {
  _getDomainChild( shift, 'fhosting');
}

sub getDomainPhosting {
  my ($id, $content, $do_gzip) = @_; # domain identifier from 'getDomains' result, flag to dump content, flag to compress content

  my $childNode = _getDomainChild( $id, 'phosting');
  return $childNode;
}
#
# End of interface methods
#


1;
