#!/usr/bin/env perl -w
# IBM(c) 2009 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
#   xCAT post script for AIX nodes
#   This script will send command "syncfiles" to the xcatd on 
#   management node or service node to initiate the sync file
#   operation by xdcp command
#
#####################################################

use XML::Simple;

my $useSocketSSL=eval { require IO::Socket::SSL; };
if ($useSocketSSL) {
        require IO::Socket::SSL;
}

my $port = "3001";
my $remote = IO::Socket::SSL->new( Proto => "tcp", PeerAddr => $ENV{MASTER}, PeerPort  => $port, );
unless ($remote) {
  `logger -t xCAT "startsyncfiles: Cannot connect to host $ENV{MASTER}"`;
  exit 0;
}

# Send Syncing File request to the xcatd
print $remote "<xcatrequest>\n";
print $remote "   <command>syncfiles</command>\n";
print $remote "</xcatrequest>\n";


my $response='';
my $rsp;
my $rc;
while (<$remote>) {
  $response .= $_;
  if ($response =~ m/<\/xcatresponse>/) {
    $rsp = eval { XMLin($response,SuppressEmpty=>undef,ForceArray=>1) };
    if ($rsp->{serverdone}) {
      close $remote;
      exit !$rc;
    }

    if ($rsp->{errorcode}) {
      $rc = $rsp->{errorcode}[0];
    } elsif ($rsp->{error}) {
      $rc = $rsp->{error}[0];
    }
    $response='';
  }
}


close $remote;
exit 0;

